[英]Two Item get selected in Recyclerview when i click on Linear Layout to change Background
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 例如,如果我单击位置0,它也会更改为位置9,19表示0,9,19
here is my code RecyclerAdapter.java 这是我的代码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 看到这里是位置0
and same thing happen at position 9 同样的事情发生在位置9
Here is code of MainActivity.java 这是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 我的xml代码,用于在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>
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. 因此,在您的示例中,项目0的视图持有者被重用,并成为项目9的视图持有者。您的代码中没有任何内容将背景更改为默认值,因此它保持其单击值。
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: 因此, 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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.