其他分享
首页 > 其他分享> > SwipeRefreshLayout阻止水平滚动的RecyclerView

SwipeRefreshLayout阻止水平滚动的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>

onCreate()的内容:

layoutManager = new LinearLayoutManager( this );
layoutManager.setOrientation( LinearLayoutManager.HORIZONTAL );
topTopicRecyclerView.setLayoutManager( layoutManager );

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

如果禁用SwipeRefreshLayout,则一切正常.

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

解决方法:

按照this discussion about SRL and HorizontalScrollView,我为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 );
  }
}

和在XML中的用法:

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

   <tags/>

</com.commons.android.OnlyVerticalSwipeRefreshLayout>

标签:android,android-recyclerview,swiperefreshlayout
来源: https://codeday.me/bug/20191011/1895951.html