2016-07-22 89 views
1

今天我有一个很奇怪的问题:我有一个RecyclerView,水平显示一些缩略图,我用smoothScrollToPosition导航到我需要的项目,但我注意到一个问题:它会不滚动到最后一个项目。经过进一步的调试,我发现我的回收站的儿童数量比显示的少!我看到五个项目,每次显示三个项目。但儿童数(得到recyclerView.getChildCount)返回4而不是5,我不能得到getChildAt(4)最后一个孩子。更怪,adapter.getItemCount()回报5,和recyclerView.getAdapter().getItemCount()回报5 ...RecyclerView儿童数量少于儿童人数

在检查,recyclerView.getLayoutManager().getItemCount()回报5,和'recyclerView..getLayoutManager()。getChildCount()返回4 ...

我需要两件东西:首先,我需要我的recyclerView滚动到最后一个项目,第二:我需要能够获取所有孩子,因为我在用户与项目进行交互时对其进行了一些修改。

一些代码:

// Scrolling and editing the item inside the recyclerView 
// It doesn't scroll to position 4, even having a item there... 
mLayoutManager.smoothScrollToPosition(recyclerView, null, position); 

// the code bellow returns null if position = 4, even if the recyclerView adapter has a item at index 4, and it is visible... 
ImageView iv = (ImageView) recyclerView.getChildAt(position); 
if (iv != null) { 
    iv.setPadding(10, 10, 10, 10); 
    iv.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); 
    ImageView ivo = (ImageView) recyclerView.getChildAt(position - 1); 
    if (position - 1 == 0) { 
     ivo.setPadding(10, 10, 10, 10); 
    } else { 
     ivo.setPadding(0, 10, 10, 10); 
    } 
    ivo.setBackgroundColor(getResources().getColor(R.color.transparent)); 
} 

smoothScrollToPosition实现:

public class SnappingLinearLayoutManager extends LinearLayoutManager { 

    private static final float MILLISECONDS_PER_INCH = 150f; 

    public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { 
     super(context, orientation, reverseLayout); 
    } 

    @Override 
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, 
             int position) { 
     RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext()){ 
      @Override 
      public PointF computeScrollVectorForPosition(int targetPosition) { 
       return new PointF(0, 1); 
      } 

      //This returns the milliseconds it takes to scroll one pixel. 
      @Override 
      protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { 
       return MILLISECONDS_PER_INCH/displayMetrics.densityDpi; 
      } 
     }; 
     smoothScroller.setTargetPosition(position); 
     startSmoothScroll(smoothScroller); 
    } 

    private class TopSnappedSmoothScroller extends LinearSmoothScroller { 
     public TopSnappedSmoothScroller(Context context) { 
      super(context); 

     } 

     @Override 
     public PointF computeScrollVectorForPosition(int targetPosition) { 
      return SnappingLinearLayoutManager.this 
        .computeScrollVectorForPosition(targetPosition); 
     } 

     @Override 
     protected int getVerticalSnapPreference() { 
      return SNAP_TO_ANY; 
     } 
    } 
} 
+2

RecycleView不包含适配器中的所有项目。它只能显示视图的视图+在回收站视图列表的开始和结尾处有一些额外的视图,因此它可以使其更快。也许这是问题所在。 – Rafal

回答

1

1)滚动到列表中的一个位置,你可以使用scrollToPosition(position)。你传递的位置将是最后一个项目像这样的指标:

recyclerView.smoothScrollToPosition(adapter.getItemCount()) 

我能够得到这种方法与V7 RecyclerView和Android的LinearLayoutManager工作。

的build.gradle

... 
dependencies { 
    ... 
    compile 'com.android.support:appcompat-v7:23.4.0' 
    compile 'com.android.support:recyclerview-v7:23.4.0' 
    ... 
} 

RecyclerView布局代码:

<android.support.v7.widget.RecyclerView android:id="@+id/item_list" 
    android:name="fbalashov.spikeapp.ItemListFragment" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layoutManager="LinearLayoutManager" /> 

然后,当我想滚动到列表顶部我刚刚打电话给我scrollToBottom方法:

private void scrollToBottom() { 
    recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount()); 
} 

这会使RecyclerView平滑滚动到列表中的最后一个项目。

2)Rafal声明,RecyclerView(和ListView)只绘制屏幕上可见的项目+上面和下面的一个项目或两个缓冲区,使滚动看起来不错。在您滚动时,它将“回收”视图并使用适配器中的数据填充视图。因此,从顶部滚动的视图将在列表底部重新使用,数据形成下一个项目。

因此,您无法同时获取适配器中所有项目的所有视图。但是,您可以获取适配器中的所有项目,对其进行修改,然后使用adapter.notifyDataSetChanged()触发适配器重新绘制recyclerView中的视图。例如:

// getItems() is a method you will have to add to you adapter to get the items from your 
// adapter. Otherwise you can add a method in your adapter to update the items. 
List<DataType> items = adapter.getItems(); 
for (DataType item : items) { 
    // make some change to each item 
} 
// this will force the recycler view to redraw its views 
adapter.notifyDataSetChanged(); 

您还可以通过只是在适配器更新这些项目,并使用notifyItemChanged(position)使在适配器项目更有针对性的变化。

+0

谢谢Nitpick博士!我没有使用'scrollToPosition',因为它没有动画,其他帖子建议我应该使用'smoothScrollToPosition'来制作动画卷轴......你能指点更新还是指向我可以学习如何获得一个位置的位置带有'scrollToPosition'的动画滚动?关于计数,感谢您的建议,我会尝试以某种方式标记视图,以便根据需要编辑它们... –

+0

您是否使用[RecyclerView from support v7](https://developer.android.com/话题/库/支持库/#的features.html V7-recyclerview)?我能够将它与android的LinearLayoutManager一起使用,以实现平滑滚动到列表的底部,而无需重写'smoothScrollToPosition'。我将示例代码添加到我的答案中。 –