2017-09-19 7 views
-1

RecyclerViewのスクロールを上下にスクロールするにはどうすればよいですか?androidのrecyclerviewのScrollViewを監視する

以下のコードはスクロールを検出する正しい方法ですか?

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

答えて

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

あなたはscrollviewのrecyclerviewスクロール内Recyclerviewを使用している場合は、いつかrecyclerview becauseofスムーズではありませんデフォルトのスクロールを持っています。スクロールする代わりに、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)を使用します。アクティビティクラス内。

関連する問題