0

我幾乎是這樣的:https://github.com/RyanHurst/TvProgramRecyclerView。水平recyclerviews作爲垂直回收視圖的項目。每個水平回收站視圖滾動都是同步的(只有可見項目)。水平recyclerview垂直recyclerview滾動性能內

Horizo​​tal滾動非常流暢,但垂直滾動非常糟糕。在水平滾動的位置0滾動時,它運行得非常好,但是最多的是向右滾動到最慢的位置。我試圖不在onBinViewHolder中設置回收站適配器,併爲每個水平回收站使用一個普通的recyclerviewpool,而不是任何東西。

回答

0

嘗試在父RecyclerView

+0

我曾嘗試過之前 –

2

設置setNestedScrollingEnabled(假)對於外部垂直RecyclerView,使用該擴展的類:

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,使用該擴展的類:

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 */ 
    } 
} 

你可以找到有關這些類在這裏做什麼的正確解釋:http://nerds.headout.com/fix-horizontal-scrolling-in-your-android-app/

+0

很好的參考,你的答案是錯誤的 - 雖然都是外部垂直Recycler視圖。 – Mick

+0

@Mick我從這篇文章中借來的,爲我的用例做了一些修改。 IIRC,內部recyclerview的滾動存在一個小問題。我在我的應用程序中使用上面的兩個類,它工作得很好。 – milindbableshwar

相關問題