【问题标题】:Add SearchView to RecyclerView List将 SearchView 添加到 RecyclerView 列表
【发布时间】:2018-09-01 18:11:30
【问题描述】:

我有一个 recyclerview 填充了来自 API 的列表。我正在使用 RxBinding 来搜索和过滤列表。当前的问题在于我的布局。我希望我的搜索栏字段位于顶部,列表位于其下方。下面是我的列表被过滤的示例,但您可以看到搜索栏文本和列表项重叠

下面是布局文件。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/listRecycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/searchView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start|center_vertical"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:inputType="text" />


        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_gravity="end|center_vertical"
                android:src="@android:drawable/ic_menu_search" />

        </FrameLayout>
    </LinearLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </FrameLayout>

</FrameLayout>

最终我希望搜索栏也可以滚动,这样当用户向下滚动列表时,搜索栏就会被旋转出来。但是,我认为这将涉及我创建了一些 CommonInterface 的列表,我的 SearchBar 和 ListItems 都必须实现。

【问题讨论】:

    标签: android android-layout android-recyclerview android-linearlayout recyclerview-layout


    【解决方案1】:

    像这样使用RelativeLayout,让recyclerView在editText下面,并添加一个scrollView,这样滚动就发生在整个布局上:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/listRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/edit"
                />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit">
    
                <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/searchView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start|center_vertical"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:inputType="text" />
    
    
                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_gravity="end|center_vertical"
                        android:src="@android:drawable/ic_menu_search" />
    
                </FrameLayout>
            </LinearLayout>
    
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" />
            </FrameLayout>
    
        </RelativeLayout>
    </ScrollView>
    </RelativeLayout>
    

    然后在你的 RecyclerView 中添加这个:

    recyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

    • 我编辑了答案,之前是针对 listView 而不是针对 recyclerView。
    • 我现在不在我的电脑前。但是从您提出的布局来看,这是有道理的,我相信这会奏效。我很快就能测试
    【解决方案2】:

    你可以试试这个布局

    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/atehensGray"
    
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/chrome_grey"
        android:fitsSystemWindows="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
            <SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@drawable/rounded_corners"
                android:gravity="top"
                android:hapticFeedbackEnabled="true"
                android:orientation="horizontal"
                android:visibility="visible"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="1.0" />
    
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/white"
        android:id="@+id/recyclerView_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />
    
      </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2022-12-10
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2018-09-18
      相关资源
      最近更新 更多