将SearchView添加到RecyclerView列表

问题描述 投票:0回答:2

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

ListView

现在下面是布局文件。

<?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都必须实现。

enter image description here

android android-layout android-recyclerview android-linearlayout recyclerview-layout
2个回答
0
投票

像这样使用RelativeLayout,以便recyclelerView在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 in Java中添加:

recyclerView.setNestedScrollingEnabled(false);

0
投票

你可以尝试这种布局

<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>
© www.soinside.com 2019 - 2024. All rights reserved.