问题
I have a recyclerview which lists a number of items and clicking on1 item opens a dialog with subcategories of that item.Each subcategory has a checkbpx with it.The problem is that when I click on 1 checkbox and click OK and go back to select another category the item of the same index in another subcategory is also checked.this is my code from the Dialog box which lists the subcategories
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_ok:
// Updatecatagory();
for (int i = 0; i < mysubcatagoryAdapter.catagory_list.size(); i++) {
if (!mysubcatagoryAdapter.isChecked) {
// mysubcatagoryAdapter.removehash.get(i).;
//mysubcatagoryAdapter.removehash.clear();
mysubcatagoryAdapter.removehash.remove(i);
}
}
finish();
}
}
this is my adapter
public class CustomRecycleViewAdapter_Language extends
RecyclerView.Adapter<CustomRecycleViewAdapter_Language.MyLanguageViewHolder>
{
private Context context;
public ArrayList<String> catagory_list;
private View resourceLayout;
private OnSelected onSelected;
private String checkForAction;
public ArrayList<String> removehash;
public boolean isChecked=false;
int count = 1;
SharedPreferences selection =
getApplicationContext().getSharedPreferences("MyLang", MODE_PRIVATE);
SharedPreferences.Editor editor = selection.edit();
public CustomRecycleViewAdapter_Language(Context context, ArrayList<String>
catagory_list, String checkForAction) {
this.context = context;
this.catagory_list = catagory_list;
this.checkForAction = checkForAction;
}
public class MyLanguageViewHolder extends RecyclerView.ViewHolder {
private TextView tv_option;
private CheckBox cb_option;
private RelativeLayout rl_option;
public MyLanguageViewHolder(View itemView) {
super(itemView);
tv_option = (TextView) itemView.findViewById(R.id.tv_option);
cb_option = (CheckBox) itemView.findViewById(R.id.cb_option);
rl_option = (RelativeLayout) itemView.findViewById(R.id.rl_option);
}
}
@Override
public MyLanguageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
resourceLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.resource_selectfilter, null);
MyLanguageViewHolder mcv = new MyLanguageViewHolder(resourceLayout);
return mcv;
}
@Override
public void onBindViewHolder(final MyLanguageViewHolder holder, final int position) {
holder.tv_option.setText(catagory_list.get(position).split("#")
[1].substring(0, 1).toUpperCase() +
catagory_list.get(position).split("#")[1].substring(1));
removehash = new ArrayList<>();
removehash.clear();
Common.languageselection.add(position);
for (int i = 0; i < catagory_list.size(); i++) {
removehash.add(catagory_list.get(i).split("#")[0]);
}
if (!Common.isFlag) {
try {
if(Common.languageselection.size()!=0) {
if
(Common.setid.get(position).equals(String.valueOf(position))) {
holder.cb_option.setChecked(true);
}
}
count = 2;
} catch (Exception e) {
}
}
if (checkForAction.equals("UPLOAD")) {
holder.cb_option.setVisibility(View.GONE);
}
holder.rl_option.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onSelected != null) {
if (count == 2) {
if (!holder.cb_option.isChecked()) {
holder.cb_option.setChecked(true);
removehash.get(position);
isChecked=true;
Common.setid.add(String.valueOf(position));
} else if (holder.cb_option.isChecked()) {
holder.cb_option.setChecked(false);
Common.setid.remove(String.valueOf(position));
removehash.remove(position);
}
onSelected.onClicked(view, removehash.get(position));
isChecked=true;
} else {
if (!holder.cb_option.isChecked()) {
holder.cb_option.setChecked(true);
Common.setid.add(String.valueOf(position));
removehash.get(position);
isChecked=true;
} else if (holder.cb_option.isChecked()) {
holder.cb_option.setChecked(false);
removehash.remove(position);
Common.setid.remove(String.valueOf(position));
}
if (checkForAction.equals("UPLOAD")) {
holder.cb_option.setVisibility(View.GONE);
onSelected.onClicked(view, catagory_list.get(position));
} else
onSelected.onClicked(view, removehash.get(position));
Common.holdvalues.add(removehash.get(position));
}
Log.v("SHOW", removehash.toString());
}
}
});
// try
// {
//
// if(Common.languageselection.size()>0)
// {
// for(int i=0;i<Common.languageselection.size();i++)
// {
//
//
//
if(Common.languageselection.get(i).equals(Common.setid.get(i)))
// {
//
// holder.cb_option.setChecked(true);
// }
// else
// holder.cb_option.setChecked(false);
//
// }
// }
//
// }catch (Exception e)
// {
//
// }
/* holder.tv_option.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onlanguageSelected != null) {
onlanguageSelected.onLanguageClicked(view,catagory_list.get(position));
}
}
});*/
}
@Override
public int getItemCount() {
return catagory_list.size();
}
public void setOnSelected(OnSelected onSelected) {
this.onSelected = onSelected;
}
public interface OnSelected {
public void onClicked(View view, String selectedVal);
}
}
this is the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_subcatagories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:padding="20dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/ll_respondcontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/rv_subcatagories"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/bt_ok"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginRight="2dp"
android:background="@drawable/btn_background"
android:text="OK"
android:textColor="@color/white" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
RecyclerView Items
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="3dp"
android:paddingTop="2dp">
<RelativeLayout
android:id="@+id/rl_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:padding="5dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<CheckBox
android:id="@+id/cb_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:button="@drawable/checkbox_selector"
android:gravity="left" />
<TextView
android:id="@+id/tv_option"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:text="Arindom"
android:background="@drawable/edittext_bg"
android:gravity="center"
android:padding="2dp"
android:textColor="@color/filtertextcolor"
android:textSize="15dp" />
<View
android:id="@+id/UnderPassword"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tv_option"
android:layout_centerHorizontal="true"
android:background="@color/linecolor" />
</RelativeLayout>
</RelativeLayout>
回答1:
From your question i made a example pls check if it helps.
Main Class RecyclerView Activity
public class RecyclerViewActivity extends AppCompatActivity implements CategoryClickListener {
private RecyclerView recyclerViewCategory;
private RecyclerView.LayoutManager mLayoutManager;
private Context context = RecyclerViewActivity.this;
private Activity activity = RecyclerViewActivity.this;
private CategoryAdapter categoryAdapter;
private CategoryClickListener categoryClickListener;
ArrayList<ArrayList<SubcategoryModel>> listData = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
categoryClickListener = this;
getDummyData();
recyclerViewCategory = (RecyclerView) findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(context);
recyclerViewCategory.setHasFixedSize(true);
recyclerViewCategory.setLayoutManager(mLayoutManager);
categoryAdapter = new CategoryAdapter(context, listData, categoryClickListener);
recyclerViewCategory.setAdapter(categoryAdapter);
}
public void getDummyData() {
for (int i = 0; i < 10; i++) {
ArrayList<SubcategoryModel> subCategoryData = new ArrayList<>();
subCategoryData.add(new SubcategoryModel("Sub category 1", false));
subCategoryData.add(new SubcategoryModel("Sub category 2", false));
subCategoryData.add(new SubcategoryModel("Sub category 3", false));
subCategoryData.add(new SubcategoryModel("Sub category 4", false));
subCategoryData.add(new SubcategoryModel("Sub category 5", false));
listData.add(subCategoryData);
}
}
private void showDialog(ArrayList<SubcategoryModel> listSubcategory) {
try {
final Dialog dialog = new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().windowAnimations
= R.style.Animation_AppCompat_DropDownUp;
dialog.setContentView(R.layout.dialog_subcategory);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
RecyclerView recyclerViewSubcategory = (RecyclerView) dialog.findViewById(R.id.recycler_view_subcategory);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
recyclerViewSubcategory.setHasFixedSize(true);
recyclerViewSubcategory.setLayoutManager(mLayoutManager);
SubCategoryAdapter adapter = new SubCategoryAdapter(context, listSubcategory);
recyclerViewSubcategory.setAdapter(adapter);
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void categoryClick(int position) {
showDialog(listData.get(position));
}
}
Category Adapter class
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CustomViewHolder> {
private ArrayList<ArrayList<SubcategoryModel>> listData;
private Context context;
private CategoryClickListener categoryClickListener;
public CategoryAdapter(Context context, ArrayList<ArrayList<SubcategoryModel>> listData, CategoryClickListener categoryClickListener) {
this.listData = listData;
this.context = context;
this.categoryClickListener = categoryClickListener;
}
@Override
public CategoryAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category_view, parent, false);
CategoryAdapter.CustomViewHolder viewHolder = new CategoryAdapter.CustomViewHolder(view);
return viewHolder;
}
//This method binds all widgets with a value
@Override
public void onBindViewHolder(final CategoryAdapter.CustomViewHolder holder, int position) {
try {
holder.textViewCategory.setText("Category "+(position+1));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return (null != listData ? listData.size() : 0);
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewCategory;
public CustomViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
textViewCategory = (TextView) itemView.findViewById(R.id.textViewCategory);
}
@Override
public void onClick(View v) {
categoryClickListener.categoryClick(getAdapterPosition());
}
}
}
Subcategory Adapter class
class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.CustomViewHolder> {
private ArrayList<SubcategoryModel> listData;
private Context context;
public SubCategoryAdapter(Context context, ArrayList<SubcategoryModel> listData) {
this.listData = listData;
this.context = context;
}
@Override
public SubCategoryAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_subcategory_view, parent, false);
SubCategoryAdapter.CustomViewHolder viewHolder = new SubCategoryAdapter.CustomViewHolder(view);
return viewHolder;
}
//This method binds all widgets with a value
@Override
public void onBindViewHolder(final SubCategoryAdapter.CustomViewHolder holder, int position) {
try {
SubcategoryModel subCategory = listData.get(position);
holder.textViewSubCategory.setText(subCategory.name);
if(subCategory.isChecked)
{
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return (null != listData ? listData.size() : 0);
}
class CustomViewHolder extends RecyclerView.ViewHolder {
private TextView textViewSubCategory;
private CheckBox checkBox;
public CustomViewHolder(View itemView) {
super(itemView);
textViewSubCategory = (TextView) itemView.findViewById(R.id.textViewSubCategory);
checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listData.get(getAdapterPosition()).isChecked=isChecked;
}
});
}
}
}
Subcategory Model class
public class SubcategoryModel {
String name;
boolean isChecked;
public SubcategoryModel(String name, boolean isChecked)
{
this.name = name;
this.isChecked = isChecked;
}
}
Category click listener
public interface CategoryClickListener {
public void categoryClick(int position);
}
activity_recycler_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scrollbars="none">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
dialog_subcategory
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_subcategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scrollbars="none">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
row_category_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="10dp">
<TextView
android:id="@+id/textViewCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"/>
</LinearLayout>
row_subcategory_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewSubCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"/>
</LinearLayout>
It may be help you.
来源:https://stackoverflow.com/questions/43411583/recyclerview-with-checkbox-checks-all-items-in-subcategories-of-same-index-value