[英]Updating Values in Parent RecyclerView from a Child RecyclerView
I have two RecyclerViews.我有两个 RecyclerViews。 RecyclerView A and RecyclerView B. Every item of RecyclerView A contains RecyclerView B and a TextView.
RecyclerView A 和 RecyclerView B。 RecyclerView A 的每一项都包含 RecyclerView B 和一个 TextView。 That is RecyclerView B is child of RecyclerView A.
即 RecyclerView B 是 RecyclerView A 的孩子。
How can i update the value of TextView in RecyclerView A on Button press which is in every item of RecyclerView B. TextView Belongs to Item of RecyclerView A.我如何在 RecyclerView B 的每个项目中按下按钮时更新 RecyclerView A 中 TextView 的值。TextView 属于 RecyclerView A 的项目。
You must be inflating the RecuclerView B inside onBindViewHolder of RecyclerView A, there only you can pass the position of that particular RecyclerViewA's child in RecyclerViewB's adapter.您必须在 RecyclerView A 的 onBindViewHolder 内膨胀 RecucleView B,只有您可以在 RecyclerViewB 的适配器中传递该特定 RecyclerViewA 的孩子的位置。 I'm assuming that you can have access to LayoutManager and RecyclerView instance of RecyclerView A.
我假设您可以访问 RecyclerView A 的 LayoutManager 和 RecyclerView 实例。
RecyclerViewA's onBindViewHolder: RecyclerViewA 的 onBindViewHolder:
@Override
public void onBindViewHolder(final ViewHolder holder, int positionInRecyclerViewA) {
RecyclerViewAdapterB adapterB = new RecyclerViewAdapterB(positionInRecyclerViewA);
holder.recyclerViewB.setAdapter(adapterB);
}
RecyclerViewB's onBindViewHolder: RecyclerViewB 的 onBindViewHolder:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.button.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
onButtonClick();
}
}
}
private void onButtonClick() {
if (positionInRecyclerViewA >= linearLayoutManagerA.findFirstVisibleItemPosition() &&
positionInRecyclerViewA <= linearLayoutManagerA.findLastVisibleItemPosition()) {
Adapter.ViewHolder holder = (Adapter.ViewHolder) recyclerViewA
.findViewHolderForAdapterPosition(positionInRecyclerViewA);
holder.textView.setText("text");
}
}
Update you list as well so next time when you scroll it'll have latest data.更新您的列表,以便下次滚动时它会有最新的数据。
Use this.. in recyclerView2 adapter.. and declare.. public static RecyclerViewAdapterOne reyclerViewAdapOne = ...
;在 recyclerView2 适配器中使用这个.. 并声明..
public static RecyclerViewAdapterOne reyclerViewAdapOne = ...
; in the activity and onClick()
of button in recylerView 2 adapter use Activity.reyclerViewAdapOne.updateValue(newText);
在recylerView 2 适配器的按钮的活动和
onClick()
中使用Activity.reyclerViewAdapOne.updateValue(newText);
and in RecyclerViewAdapterOne add this function ..并在RecyclerViewAdapterOne添加此功能..
updateValue(String newText) //updating the recycler adapter of recyclerview one
{
this.list.set( your_index, newText); // i assume this is a generic string list
this.notifyDataSetChanged();
}
Receive the button click within your activity/fragment or what ever on click listener you like.在您的活动/片段或您喜欢的任何点击监听器中接收按钮点击。 Change the resources which are holding the text (normally within your data set which you set to the adapter each item should know which text to show).
更改保存文本的资源(通常在您设置为适配器的数据集中,每个项目应该知道要显示哪个文本)。 Call notifyDataSetChanged() on RecyclerView A.
在 RecyclerView A 上调用 notifyDataSetChanged()。
使用 EventBus,发布事件并在主活动上订阅它并通知适配器
put both adapters in one class.send parent position and RecyclerViewB lists from RecyclerViewA Adapter to RecyclerViewB Adapter.then notify RecyclerViewA from RecyclerViewB Adapter.将两个适配器放在一个类中。将父位置和 RecyclerViewB 列表从 RecyclerViewA Adapter 发送到 RecyclerViewB Adapter。然后从 RecyclerViewB Adapter 通知 RecyclerViewA。
parent model:父模型:
public class ExampleModel {
public int text = 0;
public List<Button> child_lst;
}
in your Activity:在您的活动中:
public class TestActivity extends AppCompatActivity {
RecyclerView recyclerView_A;
List<ExampleModel> parent_lst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
recyclerView_A.setLayoutManager(new LinearLayoutManager(this));
recyclerView_A.setAdapter(new AAdapter());
}
public class AAdapter extends RecyclerView.Adapter <AAdapter.Aviewholder> {
@Override
public void onBindViewHolder(Aviewholder holder, int position) {
ExampleModel em = parent_lst.get(position);
holder.textView.setText(em.text+"");
holder.recyclerView_b.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
holder.recyclerView_b.setAdapter(new BAdapter(em.child_lst,position));
}
@Override
public int getItemCount()
{
return parent_lst.size();
}
}
public class BAdapter extends RecyclerView.Adapter <BAdapter.Bviewholder> {
List<Button> child_lst;
int parent_position;
public BAdapter(List<Button> child_lst,int parent_position){
this.child_lst = child_lst;
this.parent_position = parent_position;
}
@Override
public void onBindViewHolder(Bviewholder holder, int position) {
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parent_lst.get(parent_position).text += 1;
recyclerView_A.getAdapter().notifyItemChanged(parent_position);
}
});
}
@Override
public int getItemCount()
{
return child_lst.size();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.