SwipeRefreshLayout阻止水平滚动的RecyclerView

inj*_*eer 3 android swiperefreshlayout android-recyclerview

我的设置很简单:

<android.support.v4.widget.SwipeRefreshLayout
     android:id="@+id/swiperefresh"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

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

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

内容onCreate():

layoutManager = new LinearLayoutManager( this );
layoutManager.setOrientation( LinearLayoutManager.HORIZONTAL );
topTopicRecyclerView.setLayoutManager( layoutManager );
Run Code Online (Sandbox Code Playgroud)

现在,当我向左或向右滑动recyclelerView并且滑动角度不是完全水平时,SwipeRefreshLayout会跳入并接管滚动控件.这会导致recyclerView内部出现恼人的视觉"打嗝".

如果禁用SwipeRefreshLayout,一切都很好.

那么,如何在RecyclerView的区域上停用SwipeRefreshLayout的滚动控件?

inj*_*eer 11

根据关于SRL和Horizo​​ntalScrollView的讨论,我创建了对应的SwipeRefreshLayout:

public class OnlyVerticalSwipeRefreshLayout extends SwipeRefreshLayout {

  private int touchSlop;
  private float prevX;
  private boolean declined;

  public OnlyVerticalSwipeRefreshLayout( Context context, AttributeSet attrs ) {
    super( context, attrs );
    touchSlop = ViewConfiguration.get( context ).getScaledTouchSlop();
  }

  @Override
  public boolean onInterceptTouchEvent( MotionEvent event ) {
    switch( event.getAction() ){
      case MotionEvent.ACTION_DOWN:
        prevX = MotionEvent.obtain( event ).getX();
        declined = false; // New action
        break;

      case MotionEvent.ACTION_MOVE:
        final float eventX = event.getX();
        float xDiff = Math.abs( eventX - prevX );
        if( declined || xDiff > touchSlop ){
          declined = true; // Memorize
          return false;
        }
        break;
    }
    return super.onInterceptTouchEvent( event );
  }
}
Run Code Online (Sandbox Code Playgroud)

和在XML中的用法:

<com.commons.android.OnlyVerticalSwipeRefreshLayout
     android:id="@+id/swiperefresh"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

   <tags/>

</com.commons.android.OnlyVerticalSwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)


ViT*_*al- 8

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

            if(newState == SCROLL_STATE_DRAGGING) 
                mSwipeToRefresh.setEnabled(false);

            if(newState == SCROLL_STATE_IDLE) 
                mSwipeToRefresh.setEnabled(true);
        }
    });
Run Code Online (Sandbox Code Playgroud)