i have created one recycler view in which i have putted checkboxes on select all button all check should be true.The problem is that i have created select all in fragment and my checkboxes are in adapter. In my main fragment xml i have putted the select all button and recycler view and in adapter i have putted the checkboxes. my code is
public class SearchFragment extends BaseFragment {
Object arr;
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);
}
@OnClick({R.id.txtFilter})
public void onClick (View view) {
switch (view.getId()) {
case R.id.txtFilter:
startIntent(FilterActivity.class);
getActivity().overridePendingTransition(R.anim.left_in, R.anim.plain);
break;
}
}
public class MyAdapter extends
RecyclerView.Adapter<MyAdapter.MyViewHolder> { private HashMap<Integer,
Boolean> isChecked = new HashMap<>();
private Context mContext;
private LayoutInflater inflater;
ArrayList<String> arrayList = new ArrayList<>();
public MyAdapter(Context context, List<String> arrayList){
this.mContext = context;
this.arrayList = (ArrayList<String>) arrayList;
inflater = LayoutInflater.from(context);
}
public MyAdapter(ArrayList<String> arrayList) {
}
@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);
return holder;
}
@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int
position) {
holder.cbProduk.setText(arrayList.get(position));
if (isChecked.containsKey(position)){
holder.cbProduk.setChecked(isChecked.get(position));
} else {
holder.cbProduk.setChecked(false);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
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){
// KONDISI PADA SAAT CEKLIS
Toast.makeText(mContext, "checklist",
Toast.LENGTH_SHORT).show();
} else {
// KONDISI PADA SAAT CEKLIS DIHILANGKAN
Toast.makeText(mContext, "unchecklist",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire