DingMouRen / LayoutManagerGroup

:point_right: Customize the LayoutManager of RecyclerView(自定义LayoutManager)
4.98k stars 679 forks source link

PickerLayoutManager error #51

Open syusikoku opened 5 years ago

syusikoku commented 5 years ago

java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0 android.support.v7.widget.RecyclerView{196c0bf2 VFED.... ......I. 0,0-0,0 #7f090085 app:id/recycler1}, adapter:com.example.slmg.fragments.PickerFragment$MyAdapter@2dd10243, layout:com.example.lmg.lib.PickerLayoutManager@338090c0, context:com.example.slmg.MainActivity@20df3294 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5885) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854) at com.example.lmg.lib.PickerLayoutManager.onMeasure(PickerLayoutManager.java:68) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3321) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083) at android.widget.LinearLayout.onMeasure(LinearLayout.java:615) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.support.v7.widget.CardView.onMeasure(CardView.java:260) at android.view.View.measure(View.java:17430) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560) at android.view.View.measure(View.java:17430) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)

com.example.lmg.lib.PickerLayoutManager#onMeasure中出错: View view = recycler.getViewForPosition(0); 越界,修改如下之后: if (getItemCount() != 0 && mItemCount != 0 && state.getItemCount() > 0) { View view = recycler.getViewForPosition(0); measureChildWithMargins(view, widthSpec, heightSpec); } 测量的结果又有问题

KunMinX commented 5 years ago

+1

KunMinX commented 5 years ago

很简单,我的做法是,将 getItemCount 改成 state.getItemCount, 并且在数据的头尾都加上空字符串,即可。

sankemao commented 4 years ago

在pickerLayoutManager中添加

    @Override
    public boolean isAutoMeasureEnabled() {
        return false;
    }