LayoutManagerGroup icon indicating copy to clipboard operation
LayoutManagerGroup copied to clipboard

PickerLayoutManager 在recyclerview-v7:27.1.1版本出错

Open z-chu opened this issue 7 years ago • 4 comments

 java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0 android.support.v7.widget.RecyclerView{901d672 VFED..... .F....I. 0,0-0,0 #7f0800c6 app:id/recycler1}, adapter:com.dingmouren.example.layoutmanagergroup.fragment.PickerFragment$MyAdapter@34b3ac3, layout:com.dingmouren.layoutmanagergroup.picker.PickerLayoutManager@8c19f40, context:com.dingmouren.example.layoutmanagergroup.MainActivity@21e0b16
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5779)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
        at com.dingmouren.layoutmanagergroup.picker.PickerLayoutManager.onMeasure(PickerLayoutManager.java:69)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3210)
        at android.view.View.measure(View.java:23279)
...

z-chu avatar Jun 09 '18 10:06 z-chu

解决办法:加个判断state.getItemCount()>0,不然 recycler.getViewForPosition(0)就会报IndexOutOfBoundsException错误

...
@Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        if (getItemCount() != 0 && mItemCount != 0&&state.getItemCount()>0) {
             View view = recycler.getViewForPosition(0);
...

z-chu avatar Jun 09 '18 11:06 z-chu

非常感谢!

zhangxiliang avatar Jun 26 '18 06:06 zhangxiliang

rride public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { if (getItemCount() != 0 && mItemCount != 0&&state.getIt

判断之后,你会发现,最底部的无法选中, 测量出了问题

senjoeson avatar Aug 02 '19 06:08 senjoeson

@Override public boolean isAutoMeasureEnabled() { if (this.mItemCount != 0){ return false; } return super.isAutoMeasureEnabled(); }

在LayoutManager按上述代码样式重写即可解决

CodingCodersCode avatar Dec 26 '20 09:12 CodingCodersCode