未单击时,将检查Recyclerview复选框

时间:2018-04-25 15:53:51

标签: java android

我有一个带有复选框的Recyclerview。当用户选中一个框时,当它们向下滚动时,另一个框将被检查。我理解为什么,因为那个盒子正在被回收,这是有道理的。

但是我遇到了设置它的问题所以它没有这样做:

我目前的onBindViewHolder:

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

    holder.playerNameNumber.setText(playerData.get(position).name + "\n" +playerData.get(position).number );

    holder.pickedPlayer.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View thisView){
            PlayerDetails contact = new PlayerDetails();
            int contactPos = position;
            if(holder.pickedPlayer.isChecked()) {

                contact.number = playerData.get(position).number;
                contact.name = playerData.get(position).name;
                playerListGame.addPlayer(contact);
                String name = contact.name;
                Toast.makeText(thisView.getContext(), name + " Added", Toast.LENGTH_LONG).show();
                holder.pickedPlayer.setChecked(true);
            }
           else if(!holder.pickedPlayer.isChecked()){
                contact.number = playerData.get(position).number;
                contact.name = playerData.get(position).name;
                for(PlayerDetails i : playerListGame.myPlayers){
                    if(i.name == contact.name && i.number == contact.number){
                        contactPos = playerListGame.myPlayers.indexOf(i);
                    }
                }
                playerListGame.removePlayer(contactPos);
                String name = contact.name;
                Toast.makeText(thisView.getContext(), name + " Removed", Toast.LENGTH_LONG).show();
            }

        }

    });


}

现在我知道SetIsRecyclable是一个很大的不,但它确实解决了我的问题:

  public ViewHolder(View v){
            super(v);
            playerNameNumber = v.findViewById(R.id.playerDetails);
            pickedPlayer =v.findViewById(R.id.
              this.setIsRecyclable(false); 

如何避免使用这种方法来解决此问题?

3 个答案:

答案 0 :(得分:0)

你也必须在你的数据集中使用一个布尔值。所以当用户选中或取消选中一个时间框来更新受尊重位置数据集的布尔值并调用通知项目时更改

arraylist.get(pos).setDataCheked(true);
notifyItemChanged(pos);

答案 1 :(得分:0)

text <- "very well they all three get on well together"

library(magrittr)

text %>% 
  quanteda::kwic("well", window = 2) %>% 
  data.frame() %>% 
  tidyr::separate(pre, into = c("pre1", "pre2"), fill = "left") %>% 
  tidyr::separate(post, into = c("post1", "post2"), fill = "right")

  docname from to pre1 pre2 keyword    post1 post2
1   text1    2  2 <NA> very    well     they   all
2   text1    8  8  get   on    well together  <NA>

答案 2 :(得分:0)

您需要设置复选框的当前值,就像您设置播放器名称和号码一样。我已经假设它在你的playerData中被称为类似的东西。

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

    holder.playerNameNumber.setText(playerData.get(position).name + "\n" +playerData.get(position).number );
    holder.pickedPlayer.setChecked(playerData.get(position).picked);

    // same onclick listener, removed for readability
}