我有两个单选按钮,用于执行单选.但是在单行上,两个单选按钮都被选中了



在提供的解决方案之后,我添加了这些问题的图像,其中以前的值不会取消选择并且滚动时选中的值消失我正在尝试使用回收器视图适配器中的两个单选按钮从 11 名球员中选择一名队长和一名副队长。数据位于 ArrayList 中。单一选择有效,但我使用模态类来存储价值船长和副队长,就像船长和副队长一样,但它不存储价值。在选择任何队长和副队长之前,只有两者的值为假。 如果我选择一个球员姓名 A 作为队长,后来我想将他更改为副队长,那么副队长按钮也会被选中,队长单选按钮也不会取消选择。 我陷入了这个问题而没有得到解决方案.谁能帮忙,请。

public class Custom_Team_adpt extends RecyclerView.Adapter<Custom_Team_adpt.ViewHolder> {
Context context;
public static ArrayList<PlayerTeams> playerTeamsArrayList;
RadioButton selected=null;
RadioButton selected1=null;
public static RadioButton lastChecked = null;
public static int lastCheckedPos = 0;
public static int clickedPos;
public static RadioButton lastChecked1 = null;
public static int lastCheckedPos1 = 0;
int clickedPos1;
public RadioButton cb1;
public RadioButton cb;



public Custom_Team_adpt(Context context, ArrayList<PlayerTeams> playerTeamsArrayList) {
this.context = context;
this.playerTeamsArrayList = playerTeamsArrayList;
}
@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_team_list,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}

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

Picasso.with(context).load(playerTeamsArrayList.get(position).getPlayer_pic()).into(holder.imageView);
holder.player_name_txt.setText(playerTeamsArrayList.get(position).getPlayer_name());
holder.player_points.setText(""+playerTeamsArrayList.get(position).getPlayer_points());
if (playerTeamsArrayList.get(position).getTeam_id().equalsIgnoreCase("1")){
holder.team_name.setBackground(context.getResources().getDrawable(R.drawable.team_one_bg));
holder.team_name.setText(playerTeamsArrayList.get(position).getPlayer_team());
}
else {
holder.team_name.setText(playerTeamsArrayList.get(position).getPlayer_team());
holder.team_name.setBackground(context.getResources().getDrawable(R.drawable.team_two_bg));
}
holder.captain_pts.setText(playerTeamsArrayList.get(position).getIs_captain_per());
holder.vc_pts.setText(playerTeamsArrayList.get(position).getIs_vice_captain_per());
holder.type.setText(playerTeamsArrayList.get(position).getType());
holder.captain.setTag(new Integer(position));
holder.vice_captain.setTag(new Integer(position));
**captain button selection**

holder.captain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cb = (RadioButton) v;
clickedPos = ((Integer)cb.getTag()).intValue();
if(cb.isChecked())
{
if(lastChecked != null)
{
lastChecked.setChecked(false);
playerTeamsArrayList.get(lastCheckedPos).setIs_captain(false);
//  Toast.makeText(context, "Uncheck", Toast.LENGTH_SHORT).show();
}
lastChecked = cb;
lastCheckedPos = clickedPos;

}
else
{
lastChecked=null;
playerTeamsArrayList.get(clickedPos).setIs_captain(true);
}

}
});
**vice captain button selection**
holder.vice_captain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cb1 = (RadioButton) v;
clickedPos1 = ((Integer)cb1.getTag()).intValue();
if(cb1.isChecked())
{
if(lastChecked1 != null)
{
lastChecked1.setChecked(false);
playerTeamsArrayList.get(lastCheckedPos1).setIs_vice_captain(false);

}
lastChecked1 = cb1;
lastCheckedPos1 = clickedPos1;
}
else
{
lastChecked1 = null;
playerTeamsArrayList.get(clickedPos1).setIs_vice_captain(true);
}
}
});
}

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


public static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout selection;
ImageView  imageView;
TextView     player_name_txt;
final TextView   player_points;
public  RadioButton  captain;
public  RadioButton  vice_captain;
TextView   captain_pts;
TextView   vc_pts;
TextView  team_name;
TextView  type;
public ViewHolder(View itemView) {
super(itemView);
setIsRecyclable(false);
selection=(LinearLayout)itemView.findViewById(R.id.select_lay);
imageView=(ImageView)itemView.findViewById(R.id.profile_pic);
player_name_txt=(TextView)itemView.findViewById(R.id.player_name);
player_points=(TextView)itemView.findViewById(R.id.player_points);
captain=(RadioButton)itemView.findViewById(R.id.captain_click);
vice_captain=(RadioButton)itemView.findViewById(R.id.vice_captain_click);
captain_pts=(TextView)itemView.findViewById(R.id.captain_percent);
vc_pts=(TextView)itemView.findViewById(R.id.vice_cp_percent);
team_name=(TextView)itemView.findViewById(R.id.team_name);
type=(TextView)itemView.findViewById(R.id.type_txt);

}
}

}

custom_team_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
android:layout_margin="3sp"
android:elevation="6dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/select_lay"
android:orientation="horizontal"
android:padding="2sp"
android:weightSum="1">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:gravity="center"
android:orientation="vertical"
android:weightSum="0.1">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="60dp"
android:layout_weight="0.1"
android:layout_height="40dp"
android:id="@+id/profile_pic"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="CSK"
android:textAllCaps="true"
android:textSize="12sp"
android:fontFamily="@font/opensanslight"
android:textColor="#000"
android:background="#44BDF1"
android:id="@+id/team_name"
android:gravity="center"
/>
<TextView
android:layout_width="0dp"
android:background="@drawable/type_bg"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="WK"
android:id="@+id/type_txt"
android:textSize="12sp"
android:fontFamily="@font/opensanslight"
android:textColor="#000"
android:gravity="center"
/>
</LinearLayout>

</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="vertical"
android:weightSum="0.5">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="M Dhoni"
android:id="@+id/player_name"
android:textSize="14sp"
android:fontFamily="@font/opensansregular"
android:textColor="#000"
android:gravity="left|center"
android:paddingLeft="3sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="250 "
android:id="@+id/player_points"
android:textSize="14sp"
android:fontFamily="@font/opensansregular"
android:textColor="#000"
android:gravity="left|center"
android:paddingLeft="3sp"
/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:gravity="center"
android:orientation="vertical"
android:weightSum="0.1">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="20sp"
android:layout_weight="0.05"
android:layout_gravity="center"
android:src="@drawable/captain"
android:id="@+id/captain_click"
android:textSize="15sp"
android:fontFamily="@font/opensansregular"
android:textStyle="bold"
android:textColor="#2196F3"
android:tag="captain"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:text="50%"
android:id="@+id/captain_percent"
android:textSize="13sp"
android:fontFamily="@font/opensansregular"
android:textStyle="bold"
android:textColor="#2F2F30"
android:gravity="center"
/>


</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:orientation="vertical"
android:weightSum="0.1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="20sp"
android:layout_weight="0.05"
android:layout_gravity="center"
android:id="@+id/vice_captain_click"
android:fontFamily="@font/opensansregular"
android:tag="vice_captain"
android:gravity="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:text="40%"
android:id="@+id/vice_cp_percent"
android:fontFamily="@font/opensansregular"
android:textStyle="bold"
android:textColor="#2F2F30"
android:gravity="center"
android:textSize="13sp"
/>

</LinearLayout>


</LinearLayout>

</androidx.cardview.widget.CardView>
</LinearLayout>

在这种情况下,您可以使用 RadioGroup: setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener(:注册一个回调,当选中的单选按钮在此组中发生更改时调用。

public class MainAdapter extensions RecyclerView.Adapter {

ArrayList<String> players;
public MainAdapter(ArrayList<String> stringList) {
players = stringList;
}
@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
return new MainViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
if ("Captain" == players.get(position)) {
holder.radioGroup.check(R.id.rb_c);
} else {
holder.radioGroup.check(R.id.rb_vc);
}
}
@Override
public int getItemCount() {
return players.size();
}
class MainViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
RadioGroup radioGroup;
View mView;
public MainViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
radioGroup = itemView.findViewById(R.id.rg);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rb_vc) {
players.set(getAdapterPosition(), "ViceCaptain");
} else if (i == R.id.rb_c) {
players.set(getAdapterPosition(), "Captain");
}
}
}

}

.XML

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:weightSum="2">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vice Captain"
android:id="@+id/rb_vc"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Captain"
android:id="@+id/rb_c"
android:layout_weight="1"/>
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

我使用您的代码进行了更改。我正在使用模态类,其中船长和副船长的实际值为假,因此在绑定视图持有人中,我用布尔值更改了条件。但是在这里它仅适用于行而不是所有以前的选择不会被取消选择 这些是现在所做的更改。

public class Custom_Team_adpt extends RecyclerView.Adapter<Custom_Team_adpt.MainViewHolder> {
Context context;
public static ArrayList<PlayerTeams> playerTeamsArrayList;
public Custom_Team_adpt(Context context, ArrayList<PlayerTeams> playerTeamsArrayList) {
this.context = context;
this.playerTeamsArrayList = playerTeamsArrayList;
}
@Override
public MainViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_team_list,parent,false);
MainViewHolder viewHolder=new MainViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(final MainViewHolder holder, final int position) {
if (true == playerTeamsArrayList.get(position).isIs_captain()) {
holder.radioGroup.check(R.id.rb_c);
}
else if (true == playerTeamsArrayList.get(position).isIs_vice_captain()){
holder.radioGroup.check(R.id.rb_vc);
}
}

@Override
public int getItemCount() {
return playerTeamsArrayList.size();
}
class MainViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
RadioGroup radioGroup;
View mView;
public MainViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
radioGroup = itemView.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rb_vc) {
playerTeamsArrayList.get(getAdapterPosition()).setIs_vice_captain(true);
} else if (i == R.id.rb_c) {
playerTeamsArrayList.get(getAdapterPosition()).setIs_vice_captain(true);
}
}
}
}

请检查此更新的代码,并让我知道它是否有效:

public class MainAdapter extends 
RecyclerView.Adapter<MainAdapter.MainViewHolder> {
private ArrayList<String> players;
MainViewModel mViewModel;
String captain = "Captain";
String viceCaptain = "ViceCaptain";

public MainAdapter(ArrayList<String> stringList, MainViewModel viewModel) {
players = stringList;
mViewModel = viewModel;
}
@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
return new MainViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
holder.radioGroup.setOnCheckedChangeListener(null);
if (captain.equals(players.get(position))) {
holder.radioGroup.check(R.id.rb_c);
} else if (viceCaptain.equals(players.get(position))) {
holder.radioGroup.check(R.id.rb_vc);
} else {
holder.radioGroup.clearCheck();
}
holder.radioGroup.setOnCheckedChangeListener(holder);
}
@Override
public int getItemCount() {
return players.size();
}
class MainViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
RadioGroup radioGroup;
RadioButton rb1, rb2;
public MainViewHolder(@NonNull View itemView) {
super(itemView);
radioGroup = itemView.findViewById(R.id.rg);
rb1 = itemView.findViewById(R.id.rb_c);
rb2 = itemView.findViewById(R.id.rb_vc);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rb_vc) {
int position = players.indexOf(viceCaptain);
Collections.replaceAll(players, viceCaptain, "");
players.set(getAdapterPosition(), viceCaptain);
mViewModel.updateLiveData(position);
} else if (i == R.id.rb_c) {
int position = players.indexOf(captain);
Collections.replaceAll(players, captain, "");
players.set(getAdapterPosition(), captain);
mViewModel.updateLiveData(position);
} else {
players.set(getAdapterPosition(), "");
}
}
}

}

public class MainActivity extends AppCompatActivity {
MainViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewModel = new ViewModelProvider(this).get(MainViewModel.class);
ArrayList<String> strings = new ArrayList<>();
strings.add("Captain");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("");
strings.add("ViceCaptain");
final RecyclerView recyclerView = findViewById(R.id.rv);
recyclerView.getRecycledViewPool().clear();
final MainAdapter mainAdapter = new MainAdapter(strings, viewModel);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mainAdapter);
viewModel.update.observe(this, new Observer<Integer>() {
@Override
public void onChanged(final Integer integer) {
if (integer != -1) {
recyclerView.post(new Runnable() {
@Override
public void run() {
mainAdapter.notifyItemChanged(integer);
}
});
}
}
});
}

}

公共类 MainViewModel 扩展视图模型 {

private MutableLiveData<Integer> _update = new MutableLiveData<>(-2);
public LiveData<Integer> update = _update;
public void updateLiveData(Integer b){
try {
_update.setValue(b);
}
finally {
_update.setValue(-2);
}
}

}

您必须在此中使用 ViewModel,因为您无法在回收器视图中更改其他视图

尝试这个,在这里我创建了一个模型,其中列表和行中单个选定的单选按钮具有选定和未选择的布尔值,它适用于垂直和水平视图,一次只选择一个弧度按钮。

<RadioButton
android:id="@+id/radioCaptains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:buttonTint="@color/colorAccent"
android:checked="false"
android:padding="8dp"
android:text="Captain"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<RadioButton
android:id="@+id/radioViceCaptains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:buttonTint="@color/colorAccent"
android:checked="false"
android:padding="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/radioCaptains"
android:text="Vice Captain" />

我的适配器类是

class SampleAdapter extends  RecyclerView.Adapter<SampleAdapter.Items> {
private List<DataModel> list;
private Context context;
public SampleAdapter(List<DataModel> list, Context context) {
this.list=list;
this.context = context;
}
@Override
public SampleAdapter.Items onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.sample_text, parent, false);
return new Items(view);
}
@Override
public void onBindViewHolder(final SampleAdapter.Items holder, int position) {
holder.mEventName.setText(list.get(position).getLevel());
if (list.get(position).getChecked() &&  list.get(position).getType()==1) {
holder.mRadioViceCaptain.setChecked(false);
holder.mRadioCaptain.setChecked(true);
} else if(list.get(position).getChecked() &&  list.get(position).getType()==2) {
holder.mRadioViceCaptain.setChecked(true);
holder.mRadioCaptain.setChecked(false);
}
else{
holder.mRadioCaptain.setChecked(false);
holder.mRadioViceCaptain.setChecked(false);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class Items extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private TextView mEventName;
private RadioButton mRadioCaptain;
private RadioButton mRadioViceCaptain;
public Items(final View itemView) {
super(itemView);
mEventName = (TextView) itemView.findViewById(R.id.text_view);
mRadioCaptain = (RadioButton) itemView.findViewById(R.id.radioCaptains);
mRadioViceCaptain = (RadioButton) itemView.findViewById(R.id.radioViceCaptains);
mRadioCaptain.setOnCheckedChangeListener(this);
mRadioViceCaptain.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isPressed()) {
Log.e("position",getAdapterPosition()+"");
Log.e("b",b +"");
for(int i=0; i<list.size(); i++){
list.get(i).setChecked(false);
list.get(i).setType(0);
}
list.get(getAdapterPosition()).setChecked(true);
if (compoundButton == mRadioCaptain) {
list.get(getAdapterPosition()).setType(1);
}
else if (compoundButton == mRadioViceCaptain) {
list.get(getAdapterPosition()).setType(2);
}
notifyDataSetChanged();
}
}
}
}

我的模型类是

class DataModel {
Boolean checked;
int type;
String level;
public DataModel(Boolean checked, String level,int type) {
this.checked = checked;
this.level = level;
this.type = type;

}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}

检查变量的初始值为 false,类型为零,如下所示

DataModel data4=new DataModel(false,"Bombay",0);
DataModel data5=new DataModel(false,"Delhi",0);
mCityList.add(data);
mCityList.add(data1);

这对我这边很好。

最新更新