SwipeRefreshLayout阻止水平滚动的RecyclerView [英] SwipeRefreshLayout blocking horizontally scrolled RecyclerView

查看:86
本文介绍了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".

Now, when I swipe the recyclerView left or right and the swipe angle is not perfectly horizontal, the SwipeRefreshLayout jumps in and takes over the scrolling control. That leads to annoying visual "hiccups" inside the recyclerView.

如果禁用了SwipeRefreshLayout,就可以了.

If the SwipeRefreshLayout is disabled, all is fine.

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

So, how can I deactivate the SwipeRefreshLayout's scrolling control over the RecyclerView's area?

推荐答案

按照此讨论SRL和Horizo​​ntalScrollView ,我为SwipeRefreshLayout创建了对应版本:

As per this discussion about SRL and HorizontalScrollView, I created the counterpart for the 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中的用法:

and usage in XML:

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

   <tags/>

</com.commons.android.OnlyVerticalSwipeRefreshLayout>

这篇关于SwipeRefreshLayout阻止水平滚动的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆