gpt4 book ai didi

android - RecyclerView textChangeListener 或数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 19:04:31 24 4
gpt4 key购买 nike

我有一个 RecylerView,其中每一行都有一个 EditText。此 EditText 供用户输入玩家名称。当用户输入名称时,我需要 Adapter 正在查看的列表以某种方式更新。

首先我想我会在 EditText 上放置一个 textChangedListener ,这部分起作用了。 问题是当用户添加新行或删除行时,似乎每个 EditText 的监听器在调用 notifyDataSetChanged() 时被调用 Adapter . notifyDataSetChanged() 也从 textChangedListener 调用,给出错误

Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView.

另一种选择是不使用textChangedListener,而是仅在用户添加一行、删除一行或开始游戏时才更新适配器列表。但是为此,我不知何故需要从 RecyclerView 中存在的所有 editTexts 中获取信息,但我还没有找到这样做的方法。如果这是可能的,我可以简单地清除适配器列表,并在每次调用 notifyDataSetChanged() 时替换为用户放入 recyclerView 的任何内容。

第三种选择 是使用数据绑定(bind)。我一直在尝试遵循指南,但没有成功。绑定(bind)必须使 editTexts 直接更新适配器列表,并且最好在列表更改时更新 EditText,但这并不重要。

关于如何最好地实现它有什么想法吗?

public class ReviewAdapter extends RecyclerView.Adapter<ReviewAdapter.MyViewHolder> {

private ArrayList<String> data;

public ReviewAdapter(ArrayList<String> data) {

this.data = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context c = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(c);
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.txtPlayerName.setText(data.get(position));
holder.txtRowNumber.setText(position+1 + "");
holder.btnRemoveRow.setOnClickListener(new MyClickListener(position, holder.btnRemoveRow));
holder.txtPlayerName.addTextChangedListener(new MyTextWatcher(position));

}

@Override
public int getItemCount() {
return data.size();
}

class MyClickListener implements View.OnClickListener {

private int pos;
Button button;


public MyClickListener(int _pos, Button _button) {
super();
pos = _pos;
button = _button;


}

@Override
public void onClick(View view) {
data.remove(pos);
System.out.println("MyClickListener.onClick.pos: " + pos);
MainActivity.txtPlayers.setText(data.size() + " players");
ReviewAdapter.this.notifyDataSetChanged();
}
}

class MyTextWatcher implements TextWatcher {

int pos;

public MyTextWatcher(int pos) {
this.pos = pos;
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
data.set(pos, s.toString());
notifyDataSetChanged();

}

@Override
public void afterTextChanged(Editable editable) {
}
}


class MyViewHolder extends RecyclerView.ViewHolder {

EditText txtPlayerName;
TextView txtRowNumber;
Button btnRemoveRow;

public MyViewHolder(View view) {
super(view);

txtPlayerName = itemView.findViewById(R.id.listText);
txtRowNumber = itemView.findViewById(R.id.listIcon);
btnRemoveRow = itemView.findViewById(R.id.btnRemove);
}
}
}

最佳答案

修改并缩小了您的 Adapter 类。永远不要在 onBindViewHolder 中分配 onClickListener 而不是那个类你可以使用这个可能会解决你的问题。

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class ReviewAdapter extends RecyclerView.Adapter<ReviewAdapter.MyViewHolder> {

private ArrayList<String> data;
private Context mContext;

public ReviewAdapter(Context mContext, ArrayList<String> data) {
this.mContext=mContext;
this.data = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.txtPlayerName.setText(data.get(position));
holder.txtRowNumber.setText(String.valueOf(position + 1));

}

@Override
public int getItemCount() {
return data.size();
}


class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, TextWatcher {

EditText txtPlayerName;
TextView txtRowNumber;
Button btnRemoveRow;

public MyViewHolder(View view) {
super(view);

txtPlayerName = itemView.findViewById(R.id.listText);
txtRowNumber = itemView.findViewById(R.id.listIcon);
btnRemoveRow = itemView.findViewById(R.id.btnRemove);
btnRemoveRow.setOnClickListener(this);
txtPlayerName.addTextChangedListener(this);
}

@Override
public void onClick(View view) {
if (view == btnRemoveRow) {
data.remove(getAdapterPosition());
System.out.println("MyClickListener.onClick.pos: " + getAdapterPosition());
MainActivity.txtPlayers.setText(data.size() + " players");
notifyDataSetChanged();
}
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
data.set(getAdapterPosition(), s.toString());
//notifyItemChanged(getAdapterPosition());

}

@Override
public void afterTextChanged(Editable editable) {

}
}
}

关于android - RecyclerView textChangeListener 或数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47771149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com