object RecyclerViewHelper { /** * RecyclerView 注册加载更多监听 */ @JvmStatic funaddOnScrollListener(recyclerView: RecyclerView, loadMore: () -> Unit) { recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { overridefunonScrollStateChanged(recyclerView: RecyclerView, newState: Int) { super.onScrollStateChanged(recyclerView, newState) if (newState == RecyclerView.SCROLL_STATE_IDLE) { val layoutManager = recyclerView.layoutManager if (layoutManager is LinearLayoutManager) { val lastPosition = layoutManager.findLastCompletelyVisibleItemPosition() val count = layoutManager.itemCount if (lastPosition >= count - 2) { loadMore() return@onScrollStateChanged } } elseif (layoutManager is StaggeredGridLayoutManager) { val spanCount = layoutManager.spanCount val count = layoutManager.itemCount val result = IntArray(spanCount) layoutManager.findLastCompletelyVisibleItemPositions(result) for (it in result) { if (it >= count - spanCount - 1) { loadMore() return@onScrollStateChanged } } } elseif (layoutManager is GridLayoutManager) { val spanCount = layoutManager.spanCount val count = layoutManager.itemCount val lastPosition = layoutManager.findLastCompletelyVisibleItemPosition() if (lastPosition >= count - spanCount - 1) { loadMore() return@onScrollStateChanged }
} } } }) }
/** * 判断是否一屏显示 * * 错误或者空了返回 false */ @JvmStatic funisOneScreen(recyclerView: RecyclerView?): Boolean { recyclerView?.let { val layoutManager = recyclerView.layoutManager if (layoutManager is LinearLayoutManager) { // include GridLayoutManager val count = layoutManager.itemCount return count > 0 && layoutManager.findFirstCompletelyVisibleItemPosition() == 0 && layoutManager.findLastCompletelyVisibleItemPosition() == count - 1 } elseif (layoutManager is StaggeredGridLayoutManager) { val spanCount = layoutManager.spanCount val count = layoutManager.itemCount val last = IntArray(spanCount) val first = IntArray(spanCount) layoutManager.findLastCompletelyVisibleItemPositions(last) layoutManager.findFirstCompletelyVisibleItemPositions(first) return count > 0 && first.min() == 0 && last.max() == count - 1 } }
returnfalse }
privatefun IntArray.min(): Int { if (this.isNotEmpty()) { var result = this[0] this.forEach { if (result > it) result = it } return result } return -1 }
privatefun IntArray.max(): Int { if (this.isNotEmpty()) { var result = this[0] this.forEach { if (result < it) result = it } return result } return -1 } }
java.lang.IllegalArgumentException: Called attach on a child which is not detached: BaseViewHolder{2b241e1 position=12 id=-1, oldPos=-1, pLpos:-1} androidx.recyclerview.widget.RecyclerView{afecb06 VFED..... ......ID 0,0-1080,2055 #7f09236e app:id/recycler_view_xxx}, adapter:com.xxxx.adapter.XxxAdapter@cfc75c7, layout:androidx.recyclerview.widget.LinearLayoutManager@24af7f4, context:com.xxxx.XxxActivity@1ed75e2 at androidx.recyclerview.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:917) at androidx.recyclerview.widget.ChildHelper.attachViewToParent(ChildHelper.java:239) at androidx.recyclerview.widget.RecyclerView.addAnimatingView(RecyclerView.java:1438) at androidx.recyclerview.widget.RecyclerView.animateDisappearance(RecyclerView.java:4377) at androidx.recyclerview.widget.RecyclerView$4.processDisappeared(RecyclerView.java:616) at androidx.recyclerview.widget.ViewInfoStore.process(ViewInfoStore.java:242) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4210) at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3864) at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4410) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1131) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1131) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at com.android.internal.policy.DecorView.onLayout(DecorView.java:905) at android.view.View.layout(View.java:22213) at android.view.ViewGroup.layout(ViewGroup.java:6340) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3286) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2757) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1865) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7933) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1018) at android.view.Choreographer.doCallbacks(Choreographer.java:837) at android.view.Choreographer.doFrame(Choreographer.java:767) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1003) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:230) at android.app.ActivityThread.main(ActivityThread.java:7951) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
问题分析
对同一个 position 位置同时进行notifyItemRemoved(position) 和 notifyItemInserted(position) 操作导致。