Android 开发技术分享Android

Android 记RecyclerView顶部带搜索功能的一次简

2021-03-08  本文已影响0人  可乐_JS
预期效果:

RecyclerView顶部带搜索框(LLSearch)。默认LLSearch处于可见状态,手指向上滑动时,LLSearch不可见;手指向下滑动时,LLSearch可见。

实现思路:

1.在XML中,RV和LLSearch外层使用FrameLayout包裹,LLSearch置于RV上层,RV添加与LLSearch同等高度的top
2.在Java类中,给RV添加滑动监听(OnScrollListener),重写onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)方法,其中,当dy>0时,表示手指上滑,内容下移,此时,隐藏LLSearch(添加不添加动画,看个人偏好);当dy<0时,表示手指下滑,内容上移,此时,显示LLSearch;

效果图:
RecyclerView顶部带搜索框效果图.gif
附代码:

XML:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:paddingTop="@dimen/dp_58"
            android:overScrollMode="never"
            android:layout_height="match_parent" />

        <LinearLayout
            android:id="@+id/ll_search"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_33"
            android:layout_marginHorizontal="@dimen/dp_15"
            android:layout_marginTop="@dimen/dp_15"
            android:background="@drawable/bg_f5f6f7_r_5"
            android:gravity="center_vertical"
            android:padding="@dimen/dp_10">

            <EditText
                android:id="@+id/et_search"
                android:layout_width="@dimen/dp_0"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:hint="请输入搜索关键词"
                android:imeOptions="actionSearch"
                android:inputType="text"
                android:textColor="@color/color_333333"
                android:textSize="@dimen/sp_12" />

            <ImageView
                android:id="@+id/iv_clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_clear"
                android:visibility="gone" />
        </LinearLayout>
    </FrameLayout>

Java类:

        mBinding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
                if (lm instanceof LinearLayoutManager) {
                    int i = ((LinearLayoutManager) lm).findFirstCompletelyVisibleItemPosition();
                    if (dy < 0) {//手指下滑
                        if (i >= 0) {//position>=0,设置为可见
                            setLLSearchVisible(true);
                        }
                    } else if (dy > 0) {//手指上滑
                        if (i > 1) {//position>1,设置为不可见可见
                            setLLSearchVisible(false);
                        }
                    }
                }
            }
        });

    private void setLLSearchVisible(boolean visible) {
        if (visible) {
            if (mBinding.llSearch.getVisibility() == View.VISIBLE) {
                return;
            }
            mBinding.llSearch.setVisibility(View.VISIBLE);
            mBinding.rv.setPadding(0, SizeUtils.dp2px(58), 0, 0);
        } else {
            if (mBinding.llSearch.getVisibility() == View.GONE) {
                return;
            }
            mBinding.llSearch.setVisibility(View.GONE);
            mBinding.rv.setPadding(0, SizeUtils.dp2px(0), 0, 0);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读