gpt4 book ai didi

android - 在自定义 TextWatcher 中更新 Adapter/RecyclerView

转载 作者:行者123 更新时间:2023-12-02 01:04:40 25 4
gpt4 key购买 nike

我正在尝试通过自定义 TextWatcher 在我的 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();
}
}

最佳答案

编辑:添加而不是更新你想要的是一个接口(interface)来连接 TextWatcher 和 RecyclerView 的逻辑例如,您可以拥有此界面,但您可以根据需要修改它。

interface AddPlayerInterface {
private void addPlayer(Player player);
}

您可以在您的 Adapter 中实现它例如

    class Adapter extends RecyclerView.Adapter implements AddPlayerInterface {
// ... your other adapter implementation codes

@override
private void addPlayer(Player player) {
// here you can add the new player to the list
list.add(player);
notifyDataSetChanged(); // or any of the helper methods to notify adapter of change like for specific rows
}

}

然后您通过

将您的监听器引用传递给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, listener);
}

您的 textwatcher 将包含对要调用的接口(interface)的引用 类 CustomWatcher 实现 TextWatcher { protected Player 播放器; 私有(private) AddPlayerInterface 监听器;

   private CustomWatcher(Player player, AddPlayerInterface interface)
{
this.player = player;
this.listener = interface;
}

@Override
public void afterTextChanged(Editable editable)
{
// if you want it on after textchanged you'd call it here and you would probably create the new player instance here
Player player = new Player();
listener.addPlayer(player); // this would call the listener implementation on your adapter

}
}

关于android - 在自定义 TextWatcher 中更新 Adapter/RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532237/

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