[英]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. 我有两个适配器FilterMainListAdapter
和FilterSubListAdapter
其中的视图持有者FilterMainListViewHolder
和FilterSubListViewHolder
填充字段。
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.