滚动很多时,RecyclerView.Adapter ClassCastException

问题描述 投票:0回答:1

我正在构建一个聊天应用程序,其中我使用带有适配器的RecyclerView,并且使用了不同的Viewholder,因为服务器发送的Rich Content需要使用不同的视图图等。

[我通常使用该应用程序并与服务器聊天时,要求提供不同的内容,这意味着不同的观看者可以正常工作。每次发送其他请求时,顶层适配器会将主要的Viewholder转换为我需要的那个。

经过长时间的聊天,并且生成了不同的Viewholder后,如果我快速滚动,上下滚动RecyclerView,则主适配器将重新渲染这些项目,如果出现较早的聊天状态,它将崩溃并带有classcastException,它没有崩溃。

这里是一个例子:

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

    //depending the type of rich content i get from the server i inflate the respective xml
    switch (MessageType.values()[viewType]) {
        case 1:
            View plainTextView = inflater....
            return new ViewHolder1(plainTextView);
        case 2:
            View secondView = inflater...
            return new ViewHolder2(secondView);
        case 3:
            View thirdView = inflater...
            return new ViewHolder3(verticalView);     
    }
   return null;
}

然后在onBindViewHolder中,根据onCreateViewHolder的情况,我将ViewHolder进行了强制转换

    @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
                [...]
    case 2:

                    final ViewHolder2 holder = (ViewHolder2) holder;
                    //do stuff
                    break;

错误

2020-01-20 13:22:57.322 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.----.app, PID: 28353
java.lang.ClassCastException: com.-----.ui.adapters.MainCarouselAdapter$CarouselViewHolder cannot be cast to com.----.ui.adapters.QuickRepliesAdapter$OptionsViewHolder
    at com.-----.ui.adapters.QuickRepliesAdapter.onBindViewHolder(QuickRepliesAdapter.java:22)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
    at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:288)
    at androidx.recyclerview.widget.GapWorker.prefetchInnerRecyclerViewWithDeadline(GapWorker.java:335)
    at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:351)
    at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:361)
    at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:368)
    at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:399)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
android android-recyclerview adapter classcastexception
1个回答
0
投票
对于面临类似问题的任何人,我的主RecyclerViews和子RecyclerViews都有一个共享的viewPool。
© www.soinside.com 2019 - 2024. All rights reserved.