在不同的recyclerViews

时间:2017-08-02 04:34:24

标签: android-recyclerview android-databinding android-binding-adapter

活动A和B包含一个RecyclerView并保存类似的数据,因此他们使用名为AViewModel的公共视图模型

活动A.java

 @BindingAdapter("bind:adapter")
public static void setAdapter(RecyclerView recyclerView, ArrayList<AViewModel> vm) {
    AdapterA adapter = ((AdapterA) recyclerView.getAdapter());
    if (adapter != null) {
        adapter.clearList();
        adapter.addAll(vm);
        adapter.notifyDataSetChanged();
    }
}

活动B.java

 @BindingAdapter("bind:adapter")
public static void setAdapter(RecyclerView recyclerView, ArrayList<AViewModel> vm) {
    AdapterB adapter = ((AdapterB) recyclerView.getAdapter());
    if (adapter != null) {
        adapter.clearList();
        adapter.addAll(vm);
        adapter.notifyDataSetChanged();
    }
}

现在启动活动A时,应用程序崩溃并显示

  

AdapterA无法强制转换为AdapterB

怀疑setAdapter的类似方法签名。 有什么建议可以解决问题的原因吗?

错误记录

  

E / AndroidRuntime:致命异常:主要                                                                                   过程:com。 .hackthon.listviewpoc,PID:308                                                                                   java.lang.ClassCastException:com。 .hackthon.listviewpoc.AdapterA   无法投射到com。 .hackthon.listviewpoc.AdapterB                                                                                       在com.hackthon.listviewpoc.ActivityB.setAdapter(ActivityB.java:39)                                                                                       在   com.hackthon.listviewpoc.databinding.ActivityABinding.executeBindings(ActivityABinding.java:145)                                                                                       在   android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:379)                                                                                       在   android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:351)                                                                                       在android.databinding.ViewDataBinding $ 6.run(ViewDataBinding.java:178)                                                                                       在   android.databinding.ViewDataBinding $ 5.onViewAttachedToWindow(ViewDataBinding.java:146)                                                                                       在android.view.View.dispatchAttachedToWindow(View.java:14531)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2836)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)                                                                                       在   android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)                                                                                       在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1364)                                                                                       在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)                                                                                       在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)                                                                                       在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                                                       在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                                                       在android.view.Choreographer.doFrame(Choreographer.java:606)                                                                                       在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                                                       在android.os.Handler.handleCallback(Handler.java:739)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                       在android.os.Looper.loop(Looper.java:148)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

不要定义两个@BindingAdapter("bind:adapter"),只需编写一次并使用instanceof

@BindingAdapter("bind:adapter")
public static void setAdapter(RecyclerView recyclerView, ArrayList<AViewModel> vm) {
    if(recyclerView.getAdapter() instanceof AdapterA){
        AdapterA adapter = ((AdapterA) recyclerView.getAdapter());
        adapter.clearList();
        adapter.addAll(vm);
        adapter.notifyDataSetChanged();
    }
    else if(recyclerView.getAdapter() instanceof AdapterB){
        AdapterB adapter = ((AdapterB) recyclerView.getAdapter());
        adapter.clearList();
        adapter.addAll(vm);
        adapter.notifyDataSetChanged();

    }
}

如果它出现任何错误,请告诉我。