从Viewholder通知RecyclerView Adapter的最佳方法? [英] Best way to notify RecyclerView Adapter from Viewholder?

查看:104
本文介绍了从Viewholder通知RecyclerView Adapter的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多种项目视图类型的RecyclerView,因此它们分为单独的ViewHolder类.以前,当所有ViewHolders与RecyclerView Adapter属于同一类时,单击或删除某个项目时,我可以直接更新适配器.但是,我将ViewHolders分为不同的类,以更好地组织代码.现在,我无法直接更新RecyclerView适配器.以下哪种解决方案是从ViewHolder与适配器进行通信的最佳方式?如果有比我列出的解决方案更好的解决方案,请提出建议!

I have a RecyclerView with multiple item view types so they are broken into separate ViewHolder classes. Before, when all the ViewHolders were in the same class as the RecyclerView Adapter, I could update the adapter directly when an item was clicked or deleted. However, I separated the ViewHolders into different classes to better organize the code. Now, I cannot directly update my RecyclerView Adapter. Which of the following solutions is the best way to communicate from my ViewHolder to my Adapter? If there is a better solution than the ones I have listed, please suggest!

  1. 使用带有回调侦听器的接口.请记住,可能有5-10种不同的视图类型,因此此方法是否需要5-10个接口?
  1. Using a Interface with a Callback listener. Keep in mind that there might be 5 - 10 different view types so would this method require 5 - 10 interfaces?
  1. 使用诸如Greenrobot的Eventbus之类的Eventbus库.

接口或事件总线解决方案真的是最好的方法吗?当然,在视图和适配器之间必须有更好的通信方式!

Is an interface or eventbus solution really the best way? There surely has to be a better method for communication between views and adapters!

推荐答案

我通常在活动或片段中保存对适配器的引用. ViewHolders可以作为内部类放在Adapter中,也可以作为类放在单独的包中.您可以使用一个界面与视图持有人进行交互.您只需要在您的活动或片段中实现它即可.例如:

I usually save reference to the adapter in activity or fragment. ViewHolders can be placed as inner classes in Adapter or as classes in separate package. You can use one interface to interact with view holders. You just need to implement it in your activity or fragment. For Example:

public class MyAdapter extends RecyclerView.Adapter<VH> {
    private MyInterface mInterface;

    public MyAdapter(MyInterface i) {
         mInterface = i;
    }

    //... some code to create view holders


    //binding
    @Override
    protected void onBindItemViewHolder(ViewHolder viewHolder, final int position, int type) {
        viewHolder.bindView(position, mInterface); //call methods of interface in bindView() methods of your viewHolders
    }

    interface MyInterface {
        void someEvent();
    }
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView mText;

    ViewHolder(View root) {
        super(root);
        mText = (TextView)root.findViewById(R.id.text)
    }

    void bindView(int pos, MyAdapter.MyInterface myInterface) {
        mText.setOnClickListener(v -> myInterface.someEvent());
        //...some other code
    }
}

在您的活动或片段中的某处:

And somewhere in your activity or fragment:

final MyAdapter adapter = new MyAdapter(new MyAdapter.MyInterface() {
    @Override
    public void someEvent() {
        adapter.notifyDataSetChanged();
    }
});

这篇关于从Viewholder通知RecyclerView Adapter的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆