在android中监视recyclerview的滚动视图

在android中监视recyclerview的滚动视图,android,android-recyclerview,Android,Android Recyclerview,我如何监控RecyclerView的滚动,无论它是向上滚动还是向下滚动 下面的代码是检测滚动的正确方法吗 MessageRecyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); 用这个 用法: recyclerView.a

我如何监控
RecyclerView
的滚动,无论它是向上滚动还是向下滚动

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

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

用法:

    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);
        }
    });
用于此:

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
            }
        }
    });

如果在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>

并使用您的RecyclerView.setNestedScrollingEnabled(false);在活动课上