我从服务器获得了一个列表,并将其设置为适配器的RecyclerView。
因此,当我选择项目0 -〉时,所有项目都被选中/取消选中,在本例中,这是有效的。
但是当我选择了所有的项目,并且当我取消选择复选框1时,我期望取消选择复选框0。
适配器类
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class VagozariSelectedAmlakAdapter extends RecyclerView.Adapter<VagozariSelectedAmlakAdapter.ViewHolder> {
private Context context;
private ArrayList<ClientsDataSet> listAmlak;
boolean isCheckAll = false;
ItemClickListener itemClickListener;
int selectedPosition = -1;
public static ArrayList<String> clientsList = new ArrayList<>();
private RadioButton lastCheckedRB = null;
public VagozariSelectedAmlakAdapter(Context context, ArrayList<ClientsDataSet> listAmlak, ItemClickListener itemClickListener) {
this.context = (Activity) context;
this.listAmlak = listAmlak;
this.itemClickListener = itemClickListener;
}
@NonNull
@Override
public ViewHolder
onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
// Initialize view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_archive_selcted_amlak, parent,
false);
// Pass holder view
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull @NotNull VagozariSelectedAmlakAdapter.ViewHolder holder, int position) {
if (listAmlak.get(position).selected == true) {
holder.radioButton.setChecked(true);
} else {
holder.radioButton.setChecked(false);
}
if (listAmlak.get(position).name != null) {
holder.name_amlak.setText(listAmlak.get(position).name);
} else {
holder.name_amlak.setVisibility(View.GONE);
holder.radioButton.setVisibility(View.GONE);
}
holder.radioButton.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton compoundButton,
boolean statet) {
// check condition
if (statet) {
if (position == 0) {
for (int i = 0; i < listAmlak.size(); i++) {
ClientsDataSet dataSet = listAmlak.get(i);
dataSet.selected = true;
listAmlak.set(i, dataSet);
}
notifyDataSetChanged();
} else if (position != 0 && position > 0) {
// Toast.makeText(context,position + "" , Toast.LENGTH_LONG).show();
ClientsDataSet dataSet = listAmlak.get(position);
dataSet.selected = true;
listAmlak.set(position, dataSet);
ClientsDataSet alltfalse = listAmlak.get(0);
alltfalse.selected = false;
listAmlak.set(0, alltfalse);
////
notifyDataSetChanged();
//// clientsList.add(listAmlak.get(position).id);
}
} else {
if (position == 0) {
for (int i = 0; i < listAmlak.size(); i++) {
ClientsDataSet dataSet = listAmlak.get(i);
dataSet.selected = false;
listAmlak.set(i, dataSet);
isCheckAll = false;
}
notifyDataSetChanged();
} else if (position != 0 && position > 0) {
ClientsDataSet dataSet = listAmlak.get(position);
dataSet.selected = false;
listAmlak.set(position, dataSet);
ClientsDataSet alltfalse = listAmlak.get(0);
alltfalse.selected = false;
listAmlak.set(0, alltfalse);
//
notifyDataSetChanged();
}
}
}
});
}
@Override
public int getItemCount() {
return listAmlak.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
// MY VIEW HOLDER CLASS ------------------------
public class ViewHolder
extends RecyclerView.ViewHolder {
// Initialize variable
CheckBox radioButton;
TextView name_amlak;
ConstraintLayout root;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// Assign variable
radioButton = itemView.findViewById(R.id.item_vagozary_radioButton_selceting_amlak);
name_amlak = itemView.findViewById(R.id.item_vagozary_name_amlak);
root = itemView.findViewById(R.id.root);
}
}
public interface ItemClickListener {
// Create method
void onClick(String s);
}
}
调用Main中的适配器
LinearLayoutManager layoutManager = new LinearLayoutManager(SelectedAmlakActivityV2, LinearLayoutManager.VERTICAL, false);
lists.setLayoutManager(layoutManager);
ClientsDataSet item = new ClientsDataSet();
item.name = "all";
apiClients.clients.add(0, item);
adapter = new VagozariSelectedAmlakAdapter(SelectedAmlakActivityV2, apiClients.clients, itemClickListener);
lists.setAdapter(adapter);
型
错误日志
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView{6631a12 VFED..... ......ID 0,0-1048,1400 #7f090731 app:id/vagozariSelectedAmlak}, adapter:com.adapters.VagozariSelectedAmlakAdapter@60fc5e3, layout:androidx.recyclerview.widget.LinearLayoutManager@3949be0, context:com.view.activity.vagozariMelk.makeVagozary.SelectedAmlakByUserActivity@3b4a0d1
at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:3051)
at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5536)
at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:12253)
at androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7354)
at com.VagozariSelectedAmlakAdapter$1.onCheckedChanged(VagozariSelectedAmlakAdapter.java:114)
at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
at com.app.adapters.VagozariSelectedAmlakAdapter.onBindViewHolder(VagozariSelectedAmlakAdapter.java:69)
at com.app.adapters.VagozariSelectedAmlakAdapter.onBindViewHolder(VagozariSelectedAmlakAdapter.java:26)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
型
我简短的工作
#1:选择框0(因此选择列表中的所有项目)
#2:当我取消选择框1、2或3时,我想取消选择框0,这导致崩溃(在#1中)。
反过来说
1条答案
按热度按时间thtygnil1#
尝试将
holder.radioButton.setOnCheckedChangeListener(null);
添加为onBindViewHolder()
方法的第一行。