android - 在彼此之上实现多个视图的正确方法是什么?
问题描述
一点上下文:我正在尝试实现一个 SearchView,当调用 onQueryTextChange 时,它会显示一个 ProgressBar,当查询完成时,它会隐藏进度条并根据查询的结果显示另一个视图。如果查询没有返回任何内容,它会显示一个文本视图,显示“无结果”,如果有数据,它会填充 recyclerView。
我的问题是,实现这一点的正确方法是什么?我是否将它们全部堆叠在一起,并根据查询结果使每个可见/不可见?或者我是否使用 viewflipper 显示没有结果的片段和包含 recyclerview 的片段?或者最后还有另一种我不知道的方法?
这是我的搜索片段的样子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@color/colorWhite">
<android.support.v7.widget.SearchView
android:id="@+id/fragsearch_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:queryBackground="@android:color/transparent"
app:queryHint="Search"
app:iconifiedByDefault="false"
android:background="@color/backgroundColor"/>
<View
android:id="@+id/fragsearch_divider1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor"
android:layout_below="@+id/fragsearch_searchView"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/fragsearch_recycler"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_below="@+id/fragsearch_divider1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragsearch_progress_layout"
android:background="@color/colorWhite"
android:visibility="invisible"
android:layout_below="@+id/fragsearch_divider1"
android:gravity="center">
<ProgressBar
android:id="@+id/fragsearch_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/fragsearch_textview_noresults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fragsearch_divider1"
android:padding="12dp"
android:text="No Results Found"
android:visibility="invisible"/>
</RelativeLayout>
现在我只是将它们全部堆叠在一起并改变每个的可见性。有没有更好的方法来做到这一点?
解决方案
我喜欢为此使用 ViewFlipper。这样,您可以将各种视图状态封装在单独的布局中,并在设计时验证是否显示了正确的布局。比在不同视图上设置可见性要好得多。
<ViewFlipper>
<ViewGroup1>
</ViewGroup1>
<ViewGroup2>
</ViewGroup2>
</ViewFlipper>
要在 ViewFlipper 调用的不同子项之间切换setDisplayedChild(index)
,
推荐阅读
- python-3.x - 从 MongoDB 集合中的一组文档中创建一个列表
- scientific-computing - 使用 nextflow 创建奇点容器
- javascript - 如何在html中使用滚动条来动态分配按钮?
- java - JDBC 查找旧表名
- docker - Docker - dockerfile 中的多个卷
- sql-server - 我可以在 Azure Sql 数据库和 Sql Server 2012(本地服务器)之间创建复制吗?
- c# - 我怎样才能做出正确的 if/else if/else 语句?
- javascript - 在 Javascript 中使用正则表达式返回 URL 参数值
- c++ - 枚举类型的 Visual Studio 2017 构建工具问题
- spring - 如何从属性文件中转义包含单引号的文本?