卧式recyclerview内的卧式recyclerview滚动性能 [英] Horizontal recyclerview inside vertical recyclerview scrolling perfomance

查看:107
本文介绍了卧式recyclerview内的卧式recyclerview滚动性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎拥有: https://github.com/RyanHurst/TvProgramRecyclerView .水平recyclerviews作为垂直recyclerview的一项.每个水平的recyclerview滚动同步(仅可见项目).

I have almost this: https://github.com/RyanHurst/TvProgramRecyclerView. Horizontal recyclerviews as an item of a vertical recyclerview. Every horizontal recyclerview scroll is syncronized (only visible items).

水平滚动非常平滑,但是垂直滚动确实很糟糕.当在水平滚动上滚动到位置0时,它运行得很好,但是向右滚动的次数最多,则变得最滞后.我试过不要在onBinViewHolder中设置回收器适配器,而对每个卧式回收器都不要使用通用的recyclerviewpool.

Horizotal scrolling is very smooth, but vertical scrolling is really bad. When scrolls in the position 0 on horizontal scroll, it runs very well, but the most you scroll to the right the most laggy it becomes. I tried not to setting the recycler adapter in onBinViewHolder and using a common recyclerviewpool for every horizontal recycler and nothing.

推荐答案

对于外部垂直RecyclerView,请使用以下扩展类:

For the outer vertical RecyclerView, use this extended class:

public class BetterRecyclerView extends RecyclerView{
  private static final int INVALID_POINTER = -1;
  private int mScrollPointerId = INVALID_POINTER;
  private int mInitialTouchX, mInitialTouchY;
  private int mTouchSlop;
  public BetterRecyclerView(Context context) {
    this(context, null);
  }

  public BetterRecyclerView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public BetterRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    final ViewConfiguration vc = ViewConfiguration.get(getContext());
    mTouchSlop = vc.getScaledTouchSlop();
  }

  @Override
  public void setScrollingTouchSlop(int slopConstant) {
    super.setScrollingTouchSlop(slopConstant);
    final ViewConfiguration vc = ViewConfiguration.get(getContext());
    switch (slopConstant) {
      case TOUCH_SLOP_DEFAULT:
        mTouchSlop = vc.getScaledTouchSlop();
        break;
      case TOUCH_SLOP_PAGING:
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(vc);
        break;
      default:
        break;
    }
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent e) {
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = (int) (e.getY() + 0.5f);
        return super.onInterceptTouchEvent(e);

      case MotionEventCompat.ACTION_POINTER_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        return super.onInterceptTouchEvent(e);

      case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
        if (index < 0) {
          return false;
        }

        final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
        final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
        if (getScrollState() != SCROLL_STATE_DRAGGING) {
          final int dx = x - mInitialTouchX;
          final int dy = y - mInitialTouchY;
          final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
          final boolean canScrollVertically = getLayoutManager().canScrollVertically();
          boolean startScroll = false;
          if (canScrollHorizontally && Math.abs(dx) > mTouchSlop && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) {
            startScroll = true;
          }
          if (canScrollVertically && Math.abs(dy) > mTouchSlop && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) {
            startScroll = true;
          }
          return startScroll && super.onInterceptTouchEvent(e);
        }
        return super.onInterceptTouchEvent(e);
      }

      default:
        return super.onInterceptTouchEvent(e);
    }
  }
}

对于内部水平RecyclerView,请使用以下扩展类:

For the inner horizontal RecyclerView, use this extended class:

public class FeedRootRecyclerView extends BetterRecyclerView{  
  public FeedRootRecyclerView(Context context) {
    this(context, null);
  }

  public FeedRootRecyclerView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public FeedRootRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
 ``   /* do nothing */
  }
}

这篇关于卧式recyclerview内的卧式recyclerview滚动性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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