2017-07-11 202 views
-2

在我的應用程序中,我使用了RecyclerView,包含文本查看和複選框,但是當一個複選框被選中時,多個複選框也被選中 ,我在網站上發現類似於這種情況的問題,我實際上試圖實現它們來解決我的問題,但我不能,因爲與代碼有一些區別,這裏是:當選中RecyclerView中的複選框時,多個複選框也被選中

class RecyclerView_dAdapter extends RecyclerView.Adapter {

private List<List_Item> list_items; 
private List<List_Item> favorite_itemes = new ArrayList<>(); 
private Context context; 

RecyclerView_dAdapter(List<List_Item> list_Item, Context context) { 
    list_items = list_Item; 
    this.context = context; 

} 

@Override 
public RecyclerView_dAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false); 
    return new ViewHolder(v); 
} 


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

    List_Item current = list_items.get(position); 
    final String name = current.getName(); 

    holder.TextName.setText(name); 
    holder.TextName.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(context, Show_Team_Activity.class); 
      intent.putExtra("TeamName", name); 
      context.startActivity(intent); 
     } 
    }); 


    holder.setItemClickListener(new ItemClickListener() { 
     @Override 
     public void onItemClick(View v, int pos) { 
      CheckBox chk = (CheckBox) v; 

      if (chk.isChecked()) {    
       favorite_itemes.add(list_items.get(pos)); 
       chk.setChecked(true); 
      } else if (!chk.isChecked()) {     
       favorite_itemes.remove(list_items.get(pos)); 
       chk.setChecked(false); 
      } 
     } 
    }); 

} 

@Override 
public int getItemCount() { 
     return list_items.size();   
} 

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    private TextView TextName; 
    private CheckBox favorite_checkBox; 
    ItemClickListener itemClickListener; 

    ViewHolder(View view) { 
     super(view); 
     TextName = (TextView) view.findViewById(R.id.textView_rowitem); 
     favorite_checkBox = (CheckBox) view.findViewById(R.id.favorite_checkBox); 
     favorite_checkBox.setOnClickListener(this); 
    } 

    public void setItemClickListener(ItemClickListener ic) { 
     this.itemClickListener = ic; 
    } 

    @Override 
    public void onClick(View v) { 
     this.itemClickListener.onItemClick(v, getLayoutPosition()); 
    } 
} 

}

回答

0

這是因爲RecyclerView回收視圖。在onBindViewHolder方法中添加下面這段代碼:

holder.favorite_checkBox.setChecked(favorite_itemes.contains(current)); 
+0

感謝您的幫助,其工作 –

0

您需要保持複選框狀態。

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

    List_Item current = list_items.get(position); 
    final String name = current.getName(); 

    holder.TextName.setText(name); 
    holder.TextName.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(context, Show_Team_Activity.class); 
      intent.putExtra("TeamName", name); 
      context.startActivity(intent); 
     } 
    }); 

//set check boxes state 
holder.favorite_checkBox.setChecked(favorite_itemes.contains(current)) 

    holder.setItemClickListener(new ItemClickListener() { 
     @Override 
     public void onItemClick(View v, int pos) { 
      CheckBox chk = (CheckBox) v; 

      if (chk.isChecked()) {    
       favorite_itemes.add(list_items.get(pos)); 
       //chk.setChecked(true); 
      } else if (!chk.isChecked()) {     
       favorite_itemes.remove(list_items.get(pos)); 
       //chk.setChecked(false); 
      } 
      //notify changes 
      notifyItemChanged(position); 
     } 
    }); 

} 
相關問題