在自定义 TextWatcher 中更新 Adapter/RecyclerView

Update Adapter/RecyclerView in custom TextWatcher

我正在尝试 add/delete 我的 RecyclerView 中的一个项目来自自定义 TextWatcher

这是我的自定义文本观察器的一部分

@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

        }
    } 
}

它说 "add an item to the adapter list here" 我想向我的 recyclerview 添加一个项目并更新适配器。

我很确定没有办法轻松做到这一点。我是否需要使用 Singleton design pattern 进行设置,或者有没有一种方法可以在我的 MainActivity 上创建一个自定义侦听器,当我想添加一个项目时调用它?

我也在使用自定义适配器,如果有人需要,我可以 post。

**使用我的自定义适配器更新

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();
    }
}

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

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

您可以在您的适配器中实现它 例如

    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 将包含对要调用的接口的引用 class CustomWatcher 实现 TextWatcher { 受保护的 Player 播放器; 私有 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 

   }
}