2 回答
TA贡献1155条经验 获得超0个赞
你正在做一些奇怪的事情,如果可能的话让它变得简单。
第 1 步:在您的适配器类中创建一个方法来获取所选项目
public NewGameCourseItem getSelectedItem() {
return mCourseList.get(selectedPosition);
}
第 2 步:然后修改您的点击方法,如下所示
mStartGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NewGameCourseItem item = adapter.getSelecteditem();
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
/** Also intent selected items: course name and hole number **/
intent.putExtra("COURSENAME", item.getCourseName());
intent.putExtra("HOLESNM", item.getHolesNm());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
第 3 步:现在修改您的 onCheckedChanged 如下
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
notifyItemChanged(selectedPosition);
selectedPosition = holder.getAdapterPosition();
notifyItemChanged(selectedPosition);
}
TA贡献1783条经验 获得超4个赞
在适配器中创建一个类变量
private int selectedPosition = -1; //change -1 to any default position, need to be selected.
修改适配器中的以下功能
@Override
public void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {
final NewGameCourseItem currentItem = mCourseList.get(position);
holder.mRadioButton.setChecked(selectedPosition == position);
holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked) {
int tempSelected = selectedPosition;
selectedPosition = position;
notifyDataSetChanged();
}
}
});
}
在适配器中创建一个新方法,如下所示 -
public int getSelectedItemIndex() {
return selectedPosition;
}
我假设是Activity 类中adapter的变量。NewGameCourseAdapter修改Activity中的按钮点击:
mStartGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedRecyclerItemPosition = adapter.getSelectedItemIndex();
//Calling intent and pass selected item info
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
/** Also intent selected items course name and hole number **/
intent.putExtra("COURSENAME", mCourseList.get(selectedRecyclerItemPosition).getCourseName());
intent.putExtra("HOLESNM", mCourseList.get(selectedRecyclerItemPosition).getHolesNm());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
添加回答
举报