RecyclerView 和 NestedScrolling

2017-04-06  本文已影响0人  Mr_468

RecyclerView
http://www.jianshu.com/p/9ddfdffee5d3
http://www.jianshu.com/p/4b8d6e5004d5
http://www.jianshu.com/p/898479f103b6
http://www.jianshu.com/p/f7f8814bd09a
http://www.jianshu.com/p/6c78a5a07db5

NestedScrolling

http://blog.csdn.net/lmj623565791/article/details/52204039
http://www.race604.com/android-nested-scrolling/

RecyclerView 嵌套使用RecyclerView 并被CoordinatorLayout包裹时,与SwipeRefreshLayout冲突且滑动卡顿,是因为事件分发时内部的RecyclerView的onTouchEvent方法接收到cancel事件,将SwipeRefreshLayout的NestedScrollingChildHelper中mNestedScrollingParent 值置为null,导致nestedScrolling事件无法正确传递。

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild{
    
      @Override
       public boolean onTouchEvent(MotionEvent e) {
            case MotionEvent.ACTION_CANCEL: {
            cancelTouch();
       }

      private void cancelTouch() {
            resetTouch();
            setScrollState(SCROLL_STATE_IDLE);
      }

       private void resetTouch() {
           if (mVelocityTracker != null) {
               mVelocityTracker.clear();
            }
           stopNestedScroll();
           releaseGlows();
      }

      @Override
      public void stopNestedScroll() {
           getScrollingChildHelper().stopNestedScroll();
      }
}

public class NestedScrollingChildHelper {
     public void stopNestedScroll() {
          if (mNestedScrollingParent != null) {
              ViewParentCompat.onStopNestedScroll(mNestedScrollingParent, mView);
              mNestedScrollingParent = null;
          }
     }
 }

可以自定义内部RecyclerView重写onTouch方法

 public class CustomCancelRecyclerView extends RecyclerView {
      
      @Override
      public boolean onTouchEvent(MotionEvent e) {
            return e.getAction() != MotionEvent.ACTION_CANCEL && super.onTouchEvent(e);
      }
 }

也可以给内部RecyclerView设置onTouchListener

recyclerView.setOnTouchListener(new View.OnTouchListener() {
      @Override
       public boolean onTouch(View v, MotionEvent event) {
             return event.getAction() == MotionEvent.ACTION_CANCEL;
       }
});
上一篇下一篇

猜你喜欢

热点阅读