RecyclerView 设置最后一个ArrayList项数据为第一项
Posted
技术标签:
【中文标题】RecyclerView 设置最后一个ArrayList项数据为第一项【英文标题】:RecyclerView set the last ArrayList item data to the first item 【发布时间】:2020-07-04 19:07:15 【问题描述】:我有一个 RecyclerView 和一个具有 Person 模型的 ArrayList 的适配器。 当应用程序启动时,我在 EditText 中输入名称并按下按钮:
1)
2)
3)
4)
但是当我在activity中记录personList数据时,结果是:
personList: Item 0 -----> Alireza
personList: Item 1 -----> Tohid
personList: Item 2 -----> Alireza
第一个项目被删除,最后一个项目在第一个项目中重复。 (我的适配器比较复杂,但我已经减少了代码和视图以专注于主要问题)
适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_
android:layout_ />
<EditText
android:id="@+id/et_name"
android:layout_
android:layout_
android:hint="Name" />
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:text="Add" />
</LinearLayout>
适配器类:
public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
private ArrayList<Person> mList;
private View.OnClickListener listener;
public TestAdapter(ArrayList<Person> mList, View.OnClickListener listener)
this.mList = mList;
this.listener = listener;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_adapter_item, parent, false);
if (viewType == Person.State.FIRST.getValue())
return new FirstViewHolder(itemView);
else // if (viewType == Person.State.SECOND.getValue())
return new SecondViewHolder(itemView);
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
final int pos = holder.getAdapterPosition();
final Person person = mList.get(pos);
if (holder instanceof FirstViewHolder)
final FirstViewHolder h = (FirstViewHolder) holder;
h.tvName.setVisibility(View.GONE);
h.etName.setVisibility(View.VISIBLE);
h.etName.setText("");
h.etName.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
person.setFirstName(s.toString());
@Override
public void afterTextChanged(Editable s)
);
h.btnAccept.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
person.setState(Person.State.SECOND);
notifyItemChanged(pos);
listener.onClick(v);
);
else // if (holder instanceof SecondViewHolder)
final SecondViewHolder h = (SecondViewHolder) holder;
h.tvName.setVisibility(View.VISIBLE);
h.tvName.setText(person.getFirstName());
h.etName.setVisibility(View.GONE);
h.btnAccept.setVisibility(View.GONE);
private static class FirstViewHolder extends RecyclerView.ViewHolder
private TextView tvName;
private EditText etName;
private Button btnAccept;
private FirstViewHolder(@NonNull View itemView)
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
etName = itemView.findViewById(R.id.et_name);
btnAccept = itemView.findViewById(R.id.button);
private static class SecondViewHolder extends RecyclerView.ViewHolder
private TextView tvName;
private EditText etName;
private Button btnAccept;
SecondViewHolder(@NonNull View itemView)
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
etName = itemView.findViewById(R.id.et_name);
btnAccept = itemView.findViewById(R.id.button);
@Override
public int getItemCount()
return mList.size();
@Override
public int getItemViewType(int position)
return mList.get(position).getState().getValue();
活动的相关部分:
ArrayList<Person> personList = new ArrayList<>();
personList.add(new Person("", Person.State.FIRST));
adapter = new TestAdapter(personList, new View.OnClickListener()
@Override
public void onClick(View v)
if (personList.size() < 3)
personList.add(new Person("", Person.State.FIRST));
adapter.notifyItemInserted(personList.size());
else
for (int i = 0; i < personList.size(); i++)
Log.i("personList", "Item " + i + " -----> " + personList.get(i).getName());
);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
有什么问题?以及如何获得正确和真实的 ArrayList 数据?
【问题讨论】:
【参考方案1】:您的问题在于 addTextChangedListener,当在回收站视图中添加新项目时,旧的 TextChangedListeners 没有被删除。他们不断更改您的列表项值。当状态更改为 SECOND 状态时,您应该删除这些侦听器(这在How to remove all listeners added with addTextChangedListener 中看起来非常棘手)。或者您应该在触发 onTextChanged 时添加此验证。 if (person.getState() == Person.State.FIRST)
person.setFirstName(s.toString());
【讨论】:
以上是关于RecyclerView 设置最后一个ArrayList项数据为第一项的主要内容,如果未能解决你的问题,请参考以下文章
获取 ArrayList 值并将其设置为按钮单击时的 TextView 不工作 - RecyclerView