【问题标题】:make layout slide down/out of the view when scrolling down in Recyclerview?在 Recyclerview 中向下滚动时使布局向下/滑出视图?
【发布时间】:2015-11-24 08:07:30
【问题描述】:

我正在尝试实现包含项目的 Recyclerview,并且我在屏幕底部有一个包含 3 个按钮的布局,我想在我开始滚动 Recyclerview 时将其消失或向下滑动。我进行了很多搜索,但找不到任何关于此的内容...我知道如何实现折叠工具栏布局,但是对于底部的布局有什么技巧吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/c2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

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




        <com.github.clans.fab.FloatingActionMenu
            android:id="@+id/menu2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            fab:menu_backgroundColor="#ccffffff"
            fab:menu_fab_label="Menu label"
            fab:menu_icon="@drawable/filter2"
            fab:menu_labels_ellipsize="end"
            fab:menu_labels_singleLine="true">

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/fab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/aa"
                fab:fab_label="Menu item 1"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/fab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/aa"
                fab:fab_label="Menu item 2"
                fab:fab_size="mini" />

            <com.github.clans.fab.FloatingActionButton
                android:id="@+id/fab3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/aa"
                fab:fab_label="Menu item 3"
                fab:fab_size="mini" />

        </com.github.clans.fab.FloatingActionMenu>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#e7e7e7"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="3">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/allbrands"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="All Brands"
                android:textSize="10dp" />


        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="2">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.8">

                <ImageView
                    android:id="@+id/brandsearch"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_centerInParent="true"
                    android:src="@drawable/search01"

                    />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.2">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center|top"
                    android:text="Search Brands"
                    android:textSize="10dp" />


            </RelativeLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/allposts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="All Posts"
                android:textSize="10dp" />
        </RelativeLayout>


    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    创建一个滚动监听器并添加到RecyclerView

    public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
    
        public boolean mControlsVisible = true;
        private int HIDE_THRESHOLD = 50;
        private int mScrolledDistance = 0;
    
        public HidingScrollListener(int threshold) {
            this.HIDE_THRESHOLD = threshold;
        }
    
        private HidingScrollListener() {
        }
    
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
    
            int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
            if (firstVisibleItem == 0) {
                if (!mControlsVisible) {
                    onShow();
                    mControlsVisible = true;
                }
            } else {
            if (mScrolledDistance > HIDE_THRESHOLD && mControlsVisible) {
                onHide();
                mControlsVisible = false;
                mScrolledDistance = 0;
            } else if (mScrolledDistance < -HIDE_THRESHOLD && !mControlsVisible) {
                onShow();
                mControlsVisible = true;
                mScrolledDistance = 0;
            }
            }
            if ((mControlsVisible && dy > 0) || (!mControlsVisible && dy < 0)) {
                mScrolledDistance += dy;
            }
        }
    
        public void resetScrollDistance() {
            mControlsVisible = true;
            mScrolledDistance = 0;
        }
    
        public abstract void onHide();
    
        public abstract void onShow();
    }
    

    初始化监听器

    int threshold = 50;
            listener = new HidingScrollListener(threshold) {
                @Override
                public void onHide() {
                    hideViews();
                }
    
                @Override
                public void onShow() {
                    showViews();
                }
            };
    
    yourListView.addOnScrollListener(listener);
    

    实现显示和隐藏视图方法

    public void showViews() {
            listener.resetScrollDistance();
            view.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).setDuration(ANIMATION_DURATION);
        }
    
        public void hideViews() {
            view.animate().translationY(view.getHeight()).setInterpolator(new AccelerateInterpolator(2)).setDuration(ANIMATION_DURATION);
    
        }
    

    单独调整 translationY 值

    【讨论】:

    • 在 Activity 或 Fragment 中使用 FindViewById 并初始化视图....这里的视图代表您希望隐藏的按钮
    • 其实我不擅长编程............我必须在哪里初始化监听器和下一个方法
    • 在活动 onCreate 中初始化监听器
    • 其实我需要隐藏一个位于底部的线性布局
    • 它隐藏了按钮,但它没有隐藏我的按钮所在的布局......帮我解决这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多