猿问

RecyclerView 单选按钮不会只注册最新检查的项目(也记得以前的)

我有 RecyclerView,它有多个项目,这些项目包含单选按钮、课程名称 (TextView) 和孔号 (TextView)。应该发生的是,当我单击单选按钮时,它应该只选择那个 1,如果有之前的检查,它应该取消选中它(单选)。好吧,目前它一次只检查 1 个,这是可以的(因此前端可以正常工作),但是“在引擎盖下”发生了什么......例如:


RecyclerView 目前有 3 个项目。我单击要检查的第三个项目,然后单击第一个,然后再次单击第三个。现在我点击“开始游戏”按钮,应该发生的是它应该采用最后选中的项目(在本例中是最后选择的第三个)并将其课程名称和洞号用于下一个活动,但现在发生的是它意图第一个项目课程名称和洞号......另外,如果我和以前一样,但我没有点击第一个项目,而是点击第二个项目,然后即使最后我点击了第三个项目,而不是意图它的课程名称和孔号,它意味着第二个项目......所以它总是意图被点击的项目(在某个时候)并且从那些被点击的项目中它检查第一个(从列表的顶部到底部)并接受它的意图。


这是我的适配器,我在其中检查选择了哪一个:


public class NewGameCourseAdapter extends RecyclerView.Adapter<NewGameCourseAdapter.NewGameCourseViewHolder> {

    private ArrayList<NewGameCourseItem> mCourseList;

    private NewGamePlayerAdapter.OnItemsCheckStateListener checkStateListener;



private NewGameCourseAdapter.OnItemClickListener itemClickListener;


public void setOnItemClickListener(NewGameCourseAdapter.OnItemClickListener listener) {

    itemClickListener = listener;

}


public interface OnItemClickListener {

    void onItemClick(int position);

}


public void setOnItemsCheckStateListener(NewGamePlayerAdapter.OnItemsCheckStateListener checkStateListener) {

    this.checkStateListener = checkStateListener;

}


public static class NewGameCourseViewHolder extends RecyclerView.ViewHolder {

        public RadioButton mRadioButton;


        public NewGameCourseViewHolder(@NonNull View itemView, final NewGameCourseAdapter.OnItemClickListener listener) {

            super(itemView);

            mRadioButton = itemView.findViewById(R.id.radioButton);

        }

    }

onBindViewHolder:


@Override

    public void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {

        final NewGameCourseItem currentItem = mCourseList.get(position);


        /** This can prevent some unwanted actions in some cases **/

        holder.mRadioButton.setOnCheckedChangeListener(null);


        holder.mRadioButton.setChecked(selectedPosition == position);



摘要:在前端它看起来是正确的,它只选择最后单击的那个单选按钮并取消选中前一个(它应该),但在内部,它不会像它应该做的那样“忘记”以前的选择......


胡子哥哥
浏览 98回答 2
2回答

白衣非少年

你正在做一些奇怪的事情,如果可能的话让它变得简单。第 1 步:在您的适配器类中创建一个方法来获取所选项目public NewGameCourseItem getSelectedItem() {&nbsp; &nbsp;return mCourseList.get(selectedPosition);}第 2 步:然后修改您的点击方法,如下所示mStartGame.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewGameCourseItem&nbsp; item = adapter.getSelecteditem();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/** Also intent selected items: course name and hole number **/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;intent.putExtra("COURSENAME", item.getCourseName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;intent.putExtra("HOLESNM", item.getHolesNm());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);&nbsp; &nbsp; &nbsp; }&nbsp;});第 3 步:现在修改您的 onCheckedChanged 如下&nbsp;public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {&nbsp; &nbsp; &nbsp;notifyItemChanged(selectedPosition);&nbsp; &nbsp; &nbsp;selectedPosition = holder.getAdapterPosition();&nbsp; &nbsp; &nbsp;notifyItemChanged(selectedPosition);&nbsp;}

慕娘9325324

在适配器中创建一个类变量private int selectedPosition = -1; //change -1 to any default position, need to be selected.修改适配器中的以下功能@Overridepublic void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {&nbsp; &nbsp; final NewGameCourseItem currentItem = mCourseList.get(position);&nbsp; &nbsp; holder.mRadioButton.setChecked(selectedPosition == position);&nbsp; &nbsp; holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tempSelected = selectedPosition;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedPosition = position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}在适配器中创建一个新方法,如下所示 -public int getSelectedItemIndex() {&nbsp; &nbsp; return selectedPosition;}我假设是Activity 类中adapter的变量。NewGameCourseAdapter修改Activity中的按钮点击:mStartGame.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; int selectedRecyclerItemPosition = adapter.getSelectedItemIndex();&nbsp; &nbsp; &nbsp; &nbsp; //Calling intent and pass selected item info&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);&nbsp; &nbsp; &nbsp; &nbsp; /** Also intent selected items course name and hole number **/&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("COURSENAME", mCourseList.get(selectedRecyclerItemPosition).getCourseName());&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("HOLESNM", mCourseList.get(selectedRecyclerItemPosition).getHolesNm());&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);&nbsp; &nbsp; }});
随时随地看视频慕课网APP

相关分类

Java
我要回答