当前位置:  开发笔记 > Android > 正文

SwipeRefreshLayout阻止水平滚动的RecyclerView

如何解决《SwipeRefreshLayout阻止水平滚动的RecyclerView》经验,为你挑选了1个好方法。

我的设置很简单:



     


内容onCreate():

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

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

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

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



1> injecteer..:

根据关于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 );
  }
}

和在XML中的用法:



   


推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有