Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 实现多个视图的正确方法是什么?_Android - Fatal编程技术网

Android 实现多个视图的正确方法是什么?

Android 实现多个视图的正确方法是什么?,android,Android,一点上下文:我正在尝试实现一个SearchView,当调用onQueryTextChange时,它会显示一个进度条,当查询完成时,它会隐藏进度条并根据查询结果显示另一个视图。如果查询不返回任何内容,则会显示一个文本视图,并显示“No Results”,如果有数据,则填充recyclerView 我的问题是,正确的实施方式是什么? 我是否将它们都堆叠在一起,并根据查询结果使每一个都可见/不可见?还是使用viewflipper显示无结果的片段和包含recyclerview的片段?或者最后还有另一种我

一点上下文:我正在尝试实现一个SearchView,当调用onQueryTextChange时,它会显示一个进度条,当查询完成时,它会隐藏进度条并根据查询结果显示另一个视图。如果查询不返回任何内容,则会显示一个文本视图,并显示“No Results”,如果有数据,则填充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>


现在,我只是把它们叠在一起,改变它们的可见性。有更好的方法吗?

我已经使用
Constraintlayout
在工具栏中考虑了searchview:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/padding_8dp">

    <ProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/recycler_company_details"/>

    <TextView
            android:id="@+id/txt_empty_data"
            android:text="No data available!!"
            android:layout_width="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:textColor="@android:color/holo_red_dark"
            android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>

考虑到工具栏内的searchview,我使用了
Constraintlayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/padding_8dp">

    <ProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/recycler_company_details"/>

    <TextView
            android:id="@+id/txt_empty_data"
            android:text="No data available!!"
            android:layout_width="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:textColor="@android:color/holo_red_dark"
            android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>


考虑使用framelayout实现此目的,我们还可以轻松设置子布局重力,它可以得到完美的渲染。Relativelayout也有帮助,但我们应该避免使用被称为“遗留”的视图。

考虑使用framelayout实现此目的,我们还可以轻松设置子布局重力,它可以得到完美的渲染。Relativelayout也有帮助,但我们应该避免使用被称为“遗留”的视图。

我喜欢使用ViewFlipper。通过这种方式,您可以将各种视图状态封装在单独的布局中,并在设计时验证是否显示了正确的布局。比在不同视图上设置可见性好得多

<ViewFlipper>

    <ViewGroup1>
    </ViewGroup1>

    <ViewGroup2>
    </ViewGroup2>

</ViewFlipper>


要在ViewFlipper调用的不同子级之间切换,请使用ViewFlipper。通过这种方式,您可以将各种视图状态封装在单独的布局中,并在设计时验证是否显示了正确的布局。比在不同视图上设置可见性好得多

<ViewFlipper>

    <ViewGroup1>
    </ViewGroup1>

    <ViewGroup2>
    </ViewGroup2>

</ViewFlipper>


要在ViewFlipper调用的不同子级之间切换
setDisplayedChild(index)

FrameLayout用于此目的。它对于将视图排列在彼此的顶部非常有用。在您的情况下,请将RelativeLayout替换为FrameLayout。

FrameLayout用于此目的。它对于将视图排列在彼此的顶部非常有用。在您的情况下,请将RelativeLayout替换为FrameLayout。

尝试在内部使用searchview工具栏,如果它是可能的,因为它将消耗更少的屏幕空间!可以,谢谢你让我知道,如果可能的话,试着在工具栏内使用searchview,因为它会占用更少的屏幕空间!好的,谢谢你让我知道