为什么我的“回收者"视图选择了错误的项目以及被单击的确切选择的项目? [英] Why my Recycler view selecting wrong items along with exactly selected items that are clicked?

查看:57
本文介绍了为什么我的“回收者"视图选择了错误的项目以及被单击的确切选择的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的Recycler视图选择了错误的项目以及被单击的精确选择的项目? 显示回收站视图问题的图像

我想将RecyclerView放在单击事件开始的弹出活动中,并且RecycleView应该从视图中更改所选项目的背景.我使用了以下代码进行活动"使其弹出

I want to place RecyclerView in a pop up activity started on click event and RecycleView should change the background of selected items from view. I have used following code for Activity to make it pops up

弹出活动代码

  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);

我的Recycler View适配器的代码如下

    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
}

推荐答案

回收站视图在OnBindViewHolder中回收视图.因此,单击项目时,它会反映在其他位置.为此,

The recycler view recycles the view in OnBindViewHolder.So when items are clicked it gets reflected in some other positions.To solve this.

创建一个全局变量来存储点击位置.

create a global variable to store the clicked position.

private mItemSelected=-1;

然后在查看器内部添加clickListener和onClick存储被单击项的位置.

Then inside viewholder add the clickListener and onClick store the position of the clicked item.

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();

        }
    });
  }
}

然后 在OnBindViewHolder中,

And in inside OnBindViewHolder,

@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));
    }
}

这篇关于为什么我的“回收者"视图选择了错误的项目以及被单击的确切选择的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆