简体   繁体   English

如何在适配器和视图持有者之间创建接口

[英]How to create an interface between an adapter and a viewholder

I want to create an interface between an adapter and a view holder (this view holder is an inner class of another adapter) so that I can update the text view (number). 我想在适配器和视图持有者之间创建一个接口(此视图持有者是另一个适配器的内部类),以便可以更新文本视图(数字)。 How can I do this? 我怎样才能做到这一点?

In detail: 详细:

I have two recycle views (Main List Recycler View and Sub List Recycler View horizontally placed as shown in the fig) one having a number (as one of its item) and other having checkbox (as its item). 我有两个回收视图(如图所示水平放置的“主列表回收器视图”和“子列表回收器视图”),一个具有编号(作为其一项),另一个具有复选框(作为其项)。

在此处输入图片说明

I have two adapters FilterMainListAdapter and FilterSubListAdapter with view holders FilterMainListViewHolder and FilterSubListViewHolder populating the fields. 我有两个适配器FilterMainListAdapterFilterSubListAdapter其中的视图持有者FilterMainListViewHolderFilterSubListViewHolder填充字段。

When checkboxes are selected in the Sub List Recycler View, I want the corresponding number in the Main List Recycler View to update. 当在“子列表回收者”视图中选中复选框时,我希望更新“主列表回收者”视图中的相应编号。

For this, I'm using and Interface. 为此,我使用和接口。

public interface ChangeFilterMainNumber {
void OnChangeFilterMainNumberListener(int totalCheckedNumber);
}

I've checkbox's onClick method inside the FilterSubListViewHolder and I'm trying to send the total check boxes checked number as follows. 我已经在FilterSubListViewHolder选中了复选框的onClick方法,并且尝试按以下方式发送复选框总数。

changeFilterMainNumber.OnChangeFilterMainNumberListener(totalCheckedNumber);

After that, I'm implementing ChangeFilterMainNumber interface inside the FilterMainListViewHolder 之后,我要在FilterMainListViewHolder内部实现ChangeFilterMainNumber接口

public class FilterMainListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
ChangeFilterMainNumber {...}

But How can I define this interface inside the FilterSubListAdapter ? 但是,如何在FilterSubListAdapter内部定义此接口?

changeFilterMainNumber = ???;

[If it is an activity one can define the interface like this changeFilterMainNumber = (ChangeFilterMainNumber) context inside the default constructor of FilterSubListAdapter . [如果是活性的一种可限定这样的界面changeFilterMainNumber = (ChangeFilterMainNumber) context的默认构造内部FilterSubListAdapter But what about a view holder that is an inner class of another adapter?] 但是,作为另一个适配器的内部类的视图持有者呢?]

or is there a better approach in finding a solution to my problem other than this? 还是有其他更好的方法来解决我的问题?

Update: You can take a look at the code here https://github.com/gSrikar/FilterScreen 更新:您可以在这里查看代码https://github.com/gSrikar/FilterScreen

If I implement the function as you want, I will implement like this: (This is like an Observer pattern) 如果我根据需要实现该功能,则将执行以下操作:(这类似于观察者模式)

class Fragment/Activity implement OnChangeFilterMainNumberListener{
    FilterMainListAdapter mainAdapter;
    FilterSubListAdapter subAdapter;

    void oncreate() {
         mainAdapter = new FilterMainListAdapter(this);

    }

    @Override
    void OnChangeFilterMainNumberListener(int totalCheckedNumber) {
          .....
         // Update data to sub list
    }
}

class FilterMainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    public interface ChangeFilterMainNumber {
          void OnChangeFilterMainNumberListener(int totalCheckedNumber);
     }
    ChangeFilterMainNumber listener;
    FilterMainListAdapter(ChangeFilterMainNumber listener) {
         this.listener = listener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null) {
                            listener.OnChangeFilterMainNumberListener(position)
                    }
                }
            });
    }

}

class FilterSubListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 recyclerview 适配器和 viewholder 之间的接口 - Interface between recyclerview adapter & viewholder 如何在片段和适配器之间创建接口 - How to create an interface between a fragment and an adapter 如何在 Fragment 和适配器之间创建接口? - How to create interface between Fragment and adapter? ViewHolder如何优化适配器? - How can ViewHolder optimize the Adapter ? 将类传递给 Kotlin 函数以在适配器中创建 ViewHolder - Pass a class to Kotlin function to create ViewHolder in adapter ViewHolder用自定义适配器创建联系人列表 - ViewHolder to create contact list with custom adapter "如何为 RecyclerView 适配器创建一个包装器,该包装器将接受任何实现 RecyclerView.Adapter 的适配器<RecyclerView.ViewHolder>" - How can I create a wrapper for the RecyclerView Adapter that will accept any Adapter that implements RecyclerView.Adapter<RecyclerView.ViewHolder> 如何为多个视图持有人创建一个与所选视图持有人匹配的视图持有人视图适配器,不在同一回收者视图列表中 - how to create one recycler view adapter for multiple viewholders that matches a viewholder of choice not in the same recycler view list 如何在RecyclerView Adapter中为毕加索设置目标Viewholder - How to set target viewholder for picasso in RecyclerView Adapter 如何在 RecyclerView 适配器中为 Kotlin 中的 Android 初始化 viewHolder - How to initialize viewHolder in RecyclerView adapter for Android in Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM