更新适配器/回收器在自定义文本观察器中的视图



我正在尝试从自定义TextWatcher add/delete 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 design pattern进行设置,或者有没有办法在我的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();
    }
}

编辑:添加而不是更新你想要的是一个接口来连接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
        }
    }

然后,您将侦听器引用传递到文本观察器中

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

文本观察器将包含对要调用的接口的引用 类 CustomWatcher 实现 TextWatcher { 受保护的玩家玩家; 私有添加播放器接口侦听器;

   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 
   }
}

最新更新