在SwipeRefreshLayout内滚动时隐藏和显示自定义视图

时间:2018-04-23 12:47:05

标签: android android-recyclerview swiperefreshlayout

我试图在SwipeRefreshLayout内的滚动功能上实现隐藏/显示SearchView。

我希望在用户滚动到底部并隐藏时显示SearchView - 滚动到顶部时。 此外,我想保持刷卡刷新行为。

尝试了许多例子,但没有看到接近结果。

我的xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout  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">

    <include layout="@layout/loading_bar"/>
    <include layout="@layout/network_error"/>
    <include layout="@layout/empty_materials_coinlist"/>

    <RelativeLayout
        android:id="@+id/coinlist_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/coinlist_item_bg">

        <RelativeLayout
            android:id="@+id/custom_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="exitUntilCollapsed">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="toolbar_title"
                android:textColor="@color/toolbar_text_color"
                android:textSize="18sp"
                android:typeface="sans"
                android:layout_gravity="center" />
        </RelativeLayout>

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/custom_toolbar">

            <android.widget.SearchView
                android:id="@+id/sv_coin"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:iconifiedByDefault="false"
                app:layout_scrollFlags="scroll|enterAlways|snap"/>

            <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/swiperefresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/currenciesRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/coinlist_item_bg"
                    android:scrollbars="vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

            </android.support.v4.widget.SwipeRefreshLayout>

        </android.support.design.widget.CoordinatorLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

试试这个

    myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) {
                //check for scroll down
                int itemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();

                if(itemPosition==list.size()){// your last item your recyclerview
                 //   hide your SearchView here
                }
            } else {
                //check for scroll up

                int itemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();

                if(itemPosition==0){
                    //   show your SearchView here
                }
            }
        }
    });