Two Item get selected in Recyclerview when i click on Linear Layout to change Background

旧街凉风 提交于 2020-06-28 04:14:10

问题


I searched Lot about this problem but did not Find Exact Solution.My Problem is that if i select layout at some position to change the background it works fine but at the same time it also change background of other layout. example if i click on position 0 it also change to postion 9,19 means 0,9,19

here is my code RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.myViewHolder> {
  Context mContext;
  private List<randomDatas> mData;
    static int count=0;

    public RecyclerAdapter(Context mContext, List<randomDatas> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater mInflater=LayoutInflater.from(mContext);
        view=mInflater.inflate(R.layout.recycler_list,parent,false);
        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final myViewHolder holder, int position) {
    holder.textView.setText(mData.get(position).getText());


        //Start of layout onclick Method
        holder.layoutScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int[] images = new int[]{R.drawable.bg_lime, R.drawable.bg_orange,
                        R.drawable.bg_purple, R.drawable.bg_teal, R.drawable.bg_brown};


                if(count<(images.length-1)) {
                    holder.layoutScreen.setBackgroundResource(images[count]);
                    count++;

                }
                else{
                    holder.layoutScreen.setBackgroundResource(images[count]);
                    count=0;

                }

            }


        });


    }

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

    public static class  myViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layoutScreen;
        TextView textView;

        public myViewHolder(View itemView) {
            super(itemView);

            layoutScreen=(LinearLayout)itemView.findViewById(R.id.list_item);
            textView=(TextView)itemView.findViewById(R.id.Text_id);


        }
    }


}

See here is Position 0

and same thing happen at position 9

Here is code of MainActivity.java

public class MainActivity extends AppCompatActivity {

    Activity mContext;
    ArrayList<randomDatas> rdatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.rdatas = (ArrayList<randomDatas>) Data.getData();

        RecyclerView plist = (RecyclerView) findViewById(R.id.recycle_id);
        plist.setLayoutManager(new LinearLayoutManager(this));
        RecyclerAdapter localPosdataAdapter4 = new RecyclerAdapter(getApplicationContext(),rdatas);
        plist.setAdapter(localPosdataAdapter4);
        setTitle("MainActivity");
    }
}

My xml code which is used in to show in Recyclerview

recycler_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:padding="10dp"
    android:background="#DCDCDC"
    android:id="@+id/list_item">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="some Text"
        android:textColor="#000000"
        android:textSize="30dp"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:id="@+id/Text_id"/>



</LinearLayout>

回答1:


View holders are reused which means that once you change the background of a view holder, that background will stay until it is changed back. So, in your example, the view holder for item 0 was reused and became the view holder for item 9. Nothing in your code changed the background back to the default, so it stay at its clicked value.

You need to always set the background of a view holder to either "selected" or "unselected". So, the additional code in onBindViewHolder() will look something like this:

if (position == [selected position]) {
    holder.layoutScreen.setBackgroundResource([the drawable that is the "selected" background]); 
} else {
    holder.layoutScreen.setBackgroundResource([the drawable that is the "unselected" background]);
}


来源:https://stackoverflow.com/questions/50435985/two-item-get-selected-in-recyclerview-when-i-click-on-linear-layout-to-change-ba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!