本文介绍了当我单击“线性布局"以更改背景时,在Recyclerview中选择了两个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lot中搜索了这个问题,但没有找到确切的解决方案.我的问题是,如果我在某个位置选择布局来更改背景,它可以正常工作,但同时它也会更改其他布局的背景.例如,如果我单击位置0,它也会更改为位置9,19意味着0,9,19

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,19means 0,9,19

这是我的代码RecyclerAdapter.java

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


        }
    }


}

在这里看到位置0

在位置9发生相同的事情

这是MainActivity.java的代码

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

我的xml代码,用于在Recyclerview中显示

My xml code which is used in to show in Recyclerview

recycler_list.xml

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>

推荐答案

视图持有者将被重用,这意味着一旦您更改了视图持有者的背景,该背景将一直存在,直到被更改回.因此,在您的示例中,项目0的视图持有者被重用,并成为项目9的视图持有者.您的代码中没有任何内容将背景更改为默认值,因此它保持其单击值.

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.

您需要始终将视图持有者的背景设置为选定"或未选定".因此, onBindViewHolder()中的其他代码将如下所示:

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

这篇关于当我单击“线性布局"以更改背景时,在Recyclerview中选择了两个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:38