我正在尝试在自定义的文本监视器中添加/删除
一个RecyclerView
项。
以下是我的自定义文本监视器的一部分:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the position and size from edit text
int position = (int) editText.getTag(R.string.position);
int size = (int) editText.getTag(R.string.listSize);
//if the character count equals 1
if (count == 1){
//check if the current position is proven to be the last item in the list
if (position + 1 == size){
//add an item to the list here
}
}
}
当它说“在此处将项目添加到适配器列表”时,我想要向我的recyclerview
添加一个项目并更新适配器。
我相信没有简单的方法可以做到这一点。我需要使用Singleton设计模式
来设置这个吗?还是我可以在我的MainActivity
上创建一个自定义侦听器,在我想要添加项目时调用它?
我还使用了一个自定义适配器,如果有人需要,我可以发布它。
**使用我的自定义适配器进行更新
public class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerHolder>{
private List<Players> playerList;
public PlayerAdapter(List<Players> list) {
playerList = list;
}
/* ViewHolder for each item */
public class PlayerHolder extends RecyclerView.ViewHolder {
EditText playerName;
PlayerHolder(View itemView) {
super(itemView);
Log.e("Holder","setview");
playerName = itemView.findViewById(R.id.name);
MyTextWatcher textWatcher = new MyTextWatcher(playerName);
playerName.addTextChangedListener(textWatcher);
}
}
@Override
public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.new_player_item_layout, parent, false);
return new PlayerHolder(itemView);
}
@Override
public void onBindViewHolder(PlayerHolder holder, int position) {
Players playerItem = playerList.get(position);
//Sets Text
//holder.playerName.setText(playerItem.getName());
holder.playerName.setTag(R.string.listSize, playerList.size());
holder.playerName.setTag(R.string.position, position);
}
@Override
public int getItemCount() {
return playerList.size();
}
public void updateList(List<Players> newList){
playerList = newList;
notifyDataSetChanged();
}
}