我是android的新手,现在正在使用recyclerview,我想将recycleview第3项颜色更改为蓝色,如下所示:
。 。 。 所以,我写下面的代码,但不幸的是,当我滚动列表,我得到这个结果!!!!
@Override
public void onBindViewHolder(vh holder, int position) {
Contacts example = contacts.get(position);
holder.first_name.setText(example.name);
holder.last_name.setText(example.last_name);
if (position == 3){
holder.first_name.setTextColor(Color.BLUE);
}
}
我真的很困惑。谢谢你的帮助。
答案 0 :(得分:0)
在RecyclerView
中,视图会被重复使用。滚动后,一个项目的视图将用于另一个项目。因此,在这种情况下,多个视图的颜色会发生变化。
解决方法是在else条件下将颜色更改为默认值。
if (position == 3) {
holder.first_name.setTextColor(Color.BLUE);
} else {
holder.first_name.setTextColor(Color.BLACK);
}
答案 1 :(得分:0)
// adapter.java ===>
public class Adapter extends RecyclerView.Adapter<Adapter.vh> {
private List<Contacts> contacts;
public Adapter(List<Contacts> contacts) {
this.contacts = contacts;
}
public class vh extends RecyclerView.ViewHolder {
protected TextView first_name;
protected TextView last_name;
protected ImageView Image;
public vh(View v) {
super(v);
first_name = (TextView) v.findViewById(R.id.first);
last_name = (TextView) v.findViewById(R.id.last);
Image = (ImageView) v.findViewById(R.id.imageView);
}
}
@Override
public Adapter.vh onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("creat",String.valueOf(viewType));
Log.d("creat",String.valueOf(parent));
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
Log.d("View",String.valueOf(v));
return new vh(v);
}
@Override
public void onBindViewHolder(vh holder, int position) {
Contacts example = contacts.get(position);
holder.first_name.setText(example.name);
holder.last_name.setText(example.last_name);
if (position == 3){
holder.first_name.setTextColor(Color.BLUE);
}
}
@Override
public int getItemCount() {
Log.d("Item","ItemCount");
return contacts.size();
}}
数据源的联系人类:Contacts.java ==&gt;
public class Contacts {
public String name;
public String last_name;
public Contacts(String name, String last_name) {
this.name = name;
this.last_name = last_name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}}
和主要活动:ListActivity.java ==&gt;
public class ListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
List<Contacts> l = new ArrayList<>();
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
l.add(new Contacts("Alireza","Akbari"));
l.add(new Contacts("Akbari","Alireza"));
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
Adapter cAdapter = new Adapter(l);
rv.setAdapter(cAdapter);
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(lm);
}}
答案 2 :(得分:0)
Recyclerview重用视图..
您只需添加&#39;其他&#39;
即可if (position == 3){
holder.first_name.setTextColor(Color.BLUE);
}
else{
holder.first_name.setTextColor(Color.RED);
}