Android 为什么“我的回收者”视图会选择错误的项目以及单击的选定项目?
为什么“我的回收者”视图会选择错误的项目以及单击的选定项目? 我想将RecycleView放置在单击事件时启动的弹出式活动中,RecycleView应更改视图中选定项目的背景。我使用下面的活动代码使其弹出 弹出式活动代码 My Recycler视图适配器的代码如下Android 为什么“我的回收者”视图会选择错误的项目以及单击的选定项目?,android,android-recyclerview,Android,Android Recyclerview,为什么“我的回收者”视图会选择错误的项目以及单击的选定项目? 我想将RecycleView放置在单击事件时启动的弹出式活动中,RecycleView应更改视图中选定项目的背景。我使用下面的活动代码使其弹出 弹出式活动代码 My Recycler视图适配器的代码如下 public class PopUp extends Activity { public int counter=0; private int mItemSelected=-1; public Li
public class PopUp extends Activity {
public int counter=0;
private int mItemSelected=-1;
public List<student> students=new ArrayList<student>();
public RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//to set content view for that activity
setContentView(R.layout.popup);
//end of the setting the layout for the activity
//this is mechanism to calculate the width and height of the screen
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float width=metrics.widthPixels*8/10;
float height=metrics.heightPixels*6/10;
getWindow().setLayout((int)width, (int) height);
//end of the mechanism
//the method to populate the list
populateList();
//end of the method
//now creating the recycler view
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
// use a linear layout manager
LinearLayoutManager mLayoutManager = new
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(mLayoutManager);
MyAdapter adapter=new MyAdapter(students);
recyclerView.setAdapter(adapter);
//end of the recycler view
}
//this is event for the ok button
public void Ok(View view){
}
//end of the ok button for the pop activity
//this is event for the button named as cancel
public void Cancel(View view){
}
//end of the cancel button event
//this is method for printing line
public void PrintLine(String line){
Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT).show();
}
//end of the method
//this method to populate the ArrayList
public void populateList(){
String name="Mashhood Qadeer Bhatti";
String address="Sammundri Faisalabad";
boolean status=false;
for(int i=0; i<10; i++){
students.add(new student(name+"\t"+i,address,status));
}
}
//end of the method
//this is section for recycler adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<student> values;
//this is constructor
public MyAdapter(List<student> myDataset) {
values = myDataset;
}
//end of the constructor
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v = inflater.inflate(R.layout.row_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.name.setText(values.get(position).getName().toString());
holder.address.setText(values.get(position).getAddress().toString());
holder.status.setSelected(values.get(position).getSelction());
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mItemSelected==position){
v.setBackground(getResources().getDrawable(R.drawable.im));
PrintLine("The position matched"+position);
values.get(position).setSelction(!values.get(position).getSelction());
holder.status.setChecked(values.get(position).getSelction());
}
}
});
}
@Override
public int getItemCount() {
return values.size();
}
//there will be view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name;
public TextView address;
public View layout;
public RadioButton status;
public ViewHolder(View v) {
super(v);
layout = v;
name = (TextView) v.findViewById(R.id.name);
address = (TextView) v.findViewById(R.id.address);
status=(RadioButton) v.findViewById(R.id.status);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyDataSetChanged();
mItemSelected=getAdapterPosition();
}
});
/* v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemSelected=getAdapterPosition();
PrintLine("This is position for the"+getAdapterPosition());
notifyDataSetChanged();
values.get(mItemSelected).setSelction(!values.get(mItemSelected).getSelction());
}
});*/
}
}
//end of the view holder
}
//end of that section
}
公共类弹出窗口扩展活动{
公共整数计数器=0;
private int mItemSelected=-1;
public List students=new ArrayList();
公共回收视图回收视图;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//为该活动设置内容视图的步骤
setContentView(R.layout.popup);
//活动布局设置结束
//这是计算屏幕宽度和高度的机制
DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(度量);
浮动宽度=度量。宽度像素*8/10;
浮动高度=度量。高度像素*6/10;
getWindow().setLayout((int)宽度,(int)高度);
//机制结束
//填充列表的方法
大众主义者();
//方法结束
//现在创建回收器视图
recyclerView=(recyclerView)findViewById(R.id.my\u recycler\u视图);
recyclerView.setHasFixedSize(true);
//使用线性布局管理器
LinearLayoutManager mLayoutManager=新建
LinearLayoutManager(这是LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(mLayoutManager);
MyAdapter=新的MyAdapter(学生);
recyclerView.setAdapter(适配器);
//回收器视图的结尾
}
//这是ok按钮的事件
公共无效确定(视图){
}
//pop活动的“确定”按钮结束
//这是名为“取消”的按钮的事件
公共作废取消(查看){
}
//取消按钮事件结束
//这是印刷线的方法
公共无效打印行(字符串行){
Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT.show();
}
//方法结束
//此方法用于填充ArrayList
公共无效公共列表(){
字符串名称=“Mashhood Qadeer Bhatti”;
字符串地址=“Sammundri Faisalabad”;
布尔状态=假;
对于(int i=0;i而言,recycler视图在OnBindViewHolder中回收视图。因此,当单击项目时,它会反映在一些其他位置。要解决此问题,请执行以下操作
创建一个全局变量来存储单击的位置
private int mItemSelected = -1;
然后在viewholder内部添加clickListener,然后单击存储单击项目的位置
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name;
public TextView address;
public View layout;
public RadioButton status;
public ViewHolder(View v) {
super(v);
layout = v;
name = (TextView) v.findViewById(R.id.name);
address = (TextView) v.findViewById(R.id.address);
status = (RadioButton) v.findViewById(R.id.status);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemSelected = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
而且
在BindViewHolder内
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.name.setText(values.get(position).getName().toString());
holder.address.setText(values.get(position).getAddress().toString());
holder.status.setSelected(values.get(position).getSelction());
if(mItemSelected==position){
holder.status.setChecked(true);
v.setBackground(getResources().getDrawable(R.drawable.im));
}else{
holder.status.setChecked(false);
v.setBackground(getResources().getDrawable(unselected Item));
}
}
公共类弹出窗口扩展活动{
公共整数计数器=0;
private int mItemSelected=-1;
public List students=new ArrayList();
公共回收视图回收视图;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//为该活动设置内容视图的步骤
setContentView(R.layout.popup);
//活动布局设置结束
//这是计算屏幕宽度和高度的机制
DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(度量);
浮动宽度=度量。宽度像素*8/10;
浮动高度=度量。高度像素*6/10;
getWindow().setLayout((int)宽度,(int)高度);
//机制结束
//填充列表的方法
大众主义者();
//方法结束
//现在创建回收器视图
recyclerView=(recyclerView)findViewById(R.id.my\u recycler\u视图);
recyclerView.setHasFixedSize(true);
//使用线性布局管理器
LinearLayoutManager mllayoutmanager=新的LinearLayoutManager(此,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(mLayoutManager);
MyAdapter=新的MyAdapter(学生);
recyclerView.setAdapter(适配器);
//回收器视图的结尾
}
//这是ok按钮的事件
公共无效确定(视图){
}
//pop活动的“确定”按钮结束
//这是名为“取消”的按钮的事件
公共作废取消(查看){
}
//取消按钮事件结束
//这是印刷线的方法
公共无效打印行(字符串行){
Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT.show();
}
//方法结束
//此方法用于填充ArrayList
公共无效公共列表(){
字符串名称=“Mashhood Qadeer Bhatti”;
字符串地址=“Sammundri Faisalabad”;
布尔状态=假;
对于(int i=0;iUser holder.getAdapterPosition()而不是提供职位。先生,如果我在学习的早期阶段写错了什么,请原谅。先生,我使用holder来获取我的ui小部件,例如,我在ViewAdapter中有一个名为status的单选按钮,因此我希望在单击时将收音机设置为选中状态,这就是我使用holder.status.setSelected()的原因;然后为其提供反向布尔值,仅用于切换功能,以显示是否选中。先生!如果使用holder.getAdapterPosition()它将给我适配器的id,然后如何更改它的背景。holder给我们提供了两个位置,一个是onBindViewHolderMethod中的int位置,这是在可见屏幕上的位置。另一个是holder.getAdapterPosition()这是物品的实际位置。因此我建议您使用第二个。先生,根据您和匿名先生的建议,我已经放置了获得的位置
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.name.setText(values.get(position).getName().toString());
holder.address.setText(values.get(position).getAddress().toString());
holder.status.setSelected(values.get(position).getSelction());
if(mItemSelected==position){
holder.status.setChecked(true);
v.setBackground(getResources().getDrawable(R.drawable.im));
}else{
holder.status.setChecked(false);
v.setBackground(getResources().getDrawable(unselected Item));
}
}
public class PopUp extends Activity {
public int counter=0;
private int mItemSelected=-1;
public List<student> students=new ArrayList<student>();
public RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//to set content view for that activity
setContentView(R.layout.popup);
//end of the setting the layout for the activity
//this is mechanism to calculate the width and height of the screen
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float width=metrics.widthPixels*8/10;
float height=metrics.heightPixels*6/10;
getWindow().setLayout((int)width, (int) height);
//end of the mechanism
//the method to populate the list
populateList();
//end of the method
//now creating the recycler view
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
// use a linear layout manager
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(mLayoutManager);
MyAdapter adapter=new MyAdapter(students);
recyclerView.setAdapter(adapter);
//end of the recycler view
}
//this is event for the ok button
public void Ok(View view){
}
//end of the ok button for the pop activity
//this is event for the button named as cancel
public void Cancel(View view){
}
//end of the cancel button event
//this is method for printing line
public void PrintLine(String line){
Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT).show();
}
//end of the method
//this method to populate the ArrayList
public void populateList(){
String name="Mashhood Qadeer Bhatti";
String address="Sammundri Faisalabad";
boolean status=false;
for(int i=0; i<10; i++){
students.add(new student(name+"\t"+i,address,status));
}
}
//end of the method
//this is section for recycler adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<student> values;
//this is constructor
public MyAdapter(List<student> myDataset) {
values = myDataset;
}
//end of the constructor
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v = inflater.inflate(R.layout.row_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.name.setText(values.get(position).getName().toString());
holder.address.setText(values.get(position).getAddress().toString());
holder.status.setSelected(values.get(position).getSelction());
if(values.get(position).getSelction()) {
holder.layout.setBackground(getResources().getDrawable(R.drawable.im));
}
else{
holder.layout.setBackground(getResources().getDrawable(R.drawable.imagee));
}
}
@Override
public int getItemCount() {
return values.size();
}
//there will be view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name;
public TextView address;
public View layout;
public RadioButton status;
public ViewHolder(View v) {
super(v);
layout = v;
name = (TextView) v.findViewById(R.id.name);
address = (TextView) v.findViewById(R.id.address);
status=(RadioButton) v.findViewById(R.id.status);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mItemSelected = getAdapterPosition();
try{
PrintLine("The adapter position "+getAdapterPosition());
Boolean current_value=values.get(getAdapterPosition()).getSelction();
values.get(getAdapterPosition()).setSelction(!current_value);
notifyDataSetChanged();
}
catch(Exception ex){
PrintLine("Exception of type"+ex.getMessage());
}
}
});
}
}
//end of the view holder
}
//end of that section
}