在Android中监控ScrollView for recyclerview

时间:2017-09-19 11:07:55

标签: android android-recyclerview

我如何监控RecyclerView滚动是否向上或向下滚动?

下面的代码是否是检测滚动的正确方法?

MessageRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
});

3 个答案:

答案 0 :(得分:0)

使用OnScrollListener

用法:

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

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

答案 1 :(得分:0)

使用OnScrollListener

mMessageRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
            if (dy > 0) {
                //scroll up
            } else {
                //scroll down
            }
        }
    });

答案 2 :(得分:0)

如果您在scrollview Recyclerview内部使用Recyclerview,滚动将无法顺利进行,因为recyclerview具有默认滚动。而不是滚动使用NestedScrollView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_back">
  <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <RelativeLayout
                android:id="@+id/toprel"
                android:layout_width="match_parent"
                android:layout_height="140dp">

                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_alignParentTop="true"/>

                <com.viewpagerindicator.CirclePageIndicator
                    android:id="@+id/indicator"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:gravity="bottom"
                    android:padding="10dip"
                    app:centered="true"
                    app:fillColor="@color/text_black_light"
                    app:pageColor="#ffffff"/>
            </RelativeLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/homecat_rec"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top"
                android:layout_marginTop="@dimen/margin_4"/>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

并使用yourrecyclerview.setNestedScrollingEnabled(false);在Activity类中。