当我点击线性布局更改背景时,在Recyclerview中选择了两个项目

时间:2018-05-20 14:44:25

标签: android android-recyclerview

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

这是我的代码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

这是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中显示

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 个答案:

答案 0 :(得分:0)

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

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

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