我的活动有两个不同的回收视图,使用两个不同的适配器。 Recyclerviews是否有办法相互沟通?
例如:
userChoiceRecyclerView(第二个recyclerview)有一堆显示文本的cardview对象
displayRecyclerView(first recyclerview)在userChoiceRecyclerView中单击cardview对象之前没有任何信息,当您单击displayRecyclerView中的对象时,它将删除此循环器中的cardview对象。
public class AnswerRecyclerViewAdapter extends RecyclerView.Adapter<AnswerViewHolder> {
private Context mContext;
private char[] mCharacterNameToChar;
public AnswerRecyclerViewAdapter(Context context, String name) {
mContext = context;
mCharacterNameToChar = name.toUpperCase().toCharArray();
}
public void answerUpdateName(String name) {
mCharacterNameToChar = name.toUpperCase().toCharArray();
notifyDataSetChanged();
}
@Override
public AnswerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
...
return viewHolder;
}
@Override
public void onBindViewHolder(AnswerViewHolder holder, int position) {
holder.mTextView.setText(...);
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// removes the cardviews that was updated from the
// userChoiceRecyclerView
}
});
}
@Override
public int getItemCount() {
return mCharacterNameToChar.length;
}
}
userChoiceRecyclerView与此recyclerview基本相同,只是它已经在cardviews中有信息。我想知道如何让回收者彼此沟通
答案 0 :(得分:0)
首先,您需要为ReRecyclerView中的每个对象添加 Clicklistener 。
您需要在适配器中添加此代码:
private static MyClickListener myClickListener;
.
.
public void setOnItemClickListener(MyClickListener myClickListenerHolder) {
myClickListener = myClickListenerHolder;
}
.
.
public interface MyClickListener {
void onItemClick(int position, View v);
}
并在 cardView ClickListener中添加:
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// removes the cardviews that was updated from the
myClickListener.onItemClick(position,view);
}
});
现在在 MainActivity.java ,您可以通过在主广告的 onResume 中添加对象来控制对象点击次数。
示例:
RecyclerView.Adapter SecondAdapter, displayRecyclerAdapter;
RecyclerView displayRecyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
.
.
@Override
protected void onResume() {
super.onResume();
((AnswerRecyclerViewAdapter) SecondAdapter).setOnItemClickListener(new AnswerRecyclerViewAdapter
.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
//from here you can send data to your other RecyclerView like this
displayRecyclerAdapter = new displayRecyclerAdapterClass(Context,SomeString);
displayRecyclerView.setAdapter(displayRecyclerAdapter);
}
});
}
我希望这可以帮到你^ _ ^