我已经制作了一个recyclerview.in recyclerview我有n个复选框,一个选择所有按钮就可以选择所有复选框变为真。我做到了这一点。现在我只想保存arraylist中的值pf复选框,当状态为true时取消选择它从arraylist中变得清晰。在selectall按钮上,所有复选框都存储在arraylist中。我怎么能这样做。
public class SearchFragment extends BaseFragment {
ArrayList<String> arrayList = new ArrayList<>();
public TextView tv2;
public RecyclerView rvList;
Context context;
public static SearchFragment newInstance () {
return new SearchFragment();
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
tv2=(TextView) view.findViewById(R.id.tv2);
rvList=(RecyclerView)view.findViewById(R.id.rvList);
arrayList = new ArrayList<>();
for (int i = 0; i < 10; i++){
arrayList.add("Produk " + i);
}
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
rvList.setLayoutManager(layoutManager);
MyAdapter mAdapter = new MyAdapter(getContext(),arrayList);
// 4. set adapter
rvList.setAdapter(mAdapter);
// 5. set item animator to DefaultAnimator
rvList.setItemAnimator(new DefaultItemAnimator());
return view;
}
@Override
public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAdapter mAdapter = new MyAdapter(getContext(),arrayList);
rvList.setAdapter(mAdapter);
if(AppContants.CheckFlag){
AppContants.CheckFlag=false;
//arrayList.addAll(arrayList);
}
else{
AppContants.CheckFlag=true;
//arrayList.clear();
}
mAdapter.notifyDataSetChanged();
}
});
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private HashMap<Integer, Boolean> isChecked = new HashMap<>();
private Context mContext;
private LayoutInflater inflater;
CheckBox cbProduk;
ArrayList<String> arrayList = new ArrayList<>();
public MyAdapter(Context context, List<String> arrayList){
this.mContext = context;
this.arrayList = (ArrayList<String>) arrayList;
inflater = LayoutInflater.from(context);
}
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = inflater.inflate(R.layout.list_item_produt, parent, false);
MyViewHolder holder = new MyViewHolder(rootView);
cbProduk=(CheckBox)rootView.findViewById(R.id.cbProduk);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.cbProduk.setText(arrayList.get(position));
if (AppContants.CheckFlag) {
holder.cbProduk.setChecked(true);
} else {
holder. cbProduk.setChecked(false);
}
MyAdapter mAdapter = new MyAdapter(getContext(),arrayList);
if (AppContants.CheckFlag) {
holder.cbProduk.setChecked(true);
} else {
holder. cbProduk.setChecked(false);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public void NotifyDatasetchanged() {
}
public class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox cbProduk;
public MyViewHolder(View itemView) {
super(itemView);
cbProduk = (CheckBox) itemView.findViewById(R.id.cbProduk);
cbProduk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
cbProduk.setChecked(true);
Toast.makeText(mContext, "checklist", Toast.LENGTH_SHORT).show();
} else {
cbProduk.setChecked(false);
Toast.makeText(mContext, "unchecklist", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
}
答案 0 :(得分:1)
您可以使用稀疏布尔数组来检查项目的状态,如
private SparseBooleanArray itemStateArray= new SparseBooleanArray();
然后在项目点击处理程序onClick()中使用itemStateArray中项目的状态在切换之前进行检查
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
if (!itemStateArray.get(adapterPosition, false)) {
mCheckedTextView.setChecked(true);
itemStateArray.put(adapterPosition, true);
}
else {
mCheckedTextView.setChecked(false);
itemStateArray.put(adapterPosition, false);
}
}
查看完整用途here