"无尽的适配器"。如何从RecyclerView中删除批量商品?

时间:2015-07-07 07:56:16

标签: android android-recyclerview

我正在尝试实现无尽的适配器样式RecyclerView。我提供的OnScrollListener在用户到达数据集的上限和下限时请求更多内容。

因为数据集预计包含多达10k-30k的项目,所以我试图在任何时候只保留几百个项目。我遇到的问题是RecyclerView在修剪数据时会产生异常。这些是我的适配器中的自定义方法。

public void addToStart(final MyObject[] newItems) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            items.addAll(0,Arrays.asList(newItems));
            trimEnd(newItems.length);
            notifyDataSetChanged();
        }
    });
}

public void addToEnd(final MyObject[] newItems) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            items.addAll(Arrays.asList(newItems));
            trimStart(newItems.length);
            notifyDataSetChanged();
        }
    });


}

private void trimStart(int trimAmount) {
    items = items.subList(items.size() - trimAmount, items.size());
}

private void trimEnd(int trimAmount) {
    items = items.subList(0, items.size() - trimAmount);
}

在项目更新后滚动。抛出以下错误。

  

java.lang.IndexOutOfBoundsException:检测到不一致。无效   项目位置90(偏移量:90).state:100          在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4320)          在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4278)          在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1947)          在android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:434)          在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1322)          在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1150)          在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1007)          在android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1355)          在android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2209)          在android.view.View.dispatchTouchEvent(View.java:8393)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2426)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)          在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)          在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)          在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)          在android.app.Activity.dispatchTouchEvent(Activity.java:2749)          在android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)          在android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)          在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2275)          在android.view.View.dispatchPointerEvent(View.java:8583)          在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4091)          在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957)          在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)          在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3562)          在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3528)          在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3638)          在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3536)          在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3695)          在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)          在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3562)          在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3528)          在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3536)          在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)          在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5806)          在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5745)          在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5716)          在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5896)          在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)          在android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(InputEventReceiver.java)          在android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:183)          在android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5867)          在android.view.ViewRootImpl $ ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5919)          在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)          在android.view.Choreographer.doCallbacks(Choreographer.java:580)          在android.view.Choreographer.doFrame(Choreographer.java:548)          在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)          在android.os.Handler.handleCallback(Handler.java:739)          在android.os.Handler.dispatchMessage(Handler.java:95)          在android.os.Looper.loop(Looper.java:135)          在android.app.ActivityThread.main(ActivityThread.java:5312)          在java.lang.reflect.Method.invoke(Method.java)          在java.lang.reflect.Method.invoke(Method.java:372)          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

如果我不缩小items数组,而只是添加它。 RecyclerView按预期显示不断增长的数据集。我在网上看到的所有例子都只显示添加项目。除了在用户滚动到上/下边界时重新创建适配器的分页以外,是否无法保持数据大小?

0 个答案:

没有答案