如何在适配器上单击当前项目按钮来更改RecyclerView
的下一个项目的颜色。
我的代码就像:
holder.pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.pick.startAnimation(animFadeIn);
editStatus(studentrouteList.get(position).getId(),"2");
holder.waittime.setText("done!");
holder.itemstudent.setBackgroundColor(Color.parseColor("#bee180"));
int currentpos =holder.getAdapterPosition();
int oldpos = holder.getOldPosition();
Log.e("", String.valueOf("new pos : "+ currentpos + "old pos : "+ oldpos));
Constant.pos = currentpos + 1;
int newpos = position+1;
if(studentrouteList.get(position + 1).getStatus().equals("0")) {
holder.itemstudent.setBackgroundResource(R.drawable.next_pickup); // here change current item view ... Need next item to change
}
}
});
答案 0 :(得分:-1)
您可以通过两种不同的方式轻松实现这一目标。
// position of next updated item which we need to update background
updatedPositionToChangeBgColor = -1
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
Log.d("onBindViewHolder called ","Adapter");
if(updatedPositionToChangeBgColor == position) {
holder.itembackground.setColor("#000000");
} else {
// do nothing
}
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//update position of next item and call notifyItemChanged to tell adapter it need to recreate next item again
updatedPositionToChangeBgColor = position + 1;
notifyItemChanged(updatedPositionToChangeBgColor);
}
});
}
第二种方法还是相同的,但是您从Activity类或模型调用中进行操作,或者可以使用界面更新位置并调用notifyItemChanged(updatedPositionToChangeBgColor);
来告诉Adapter重新创建该项目。和所有设置
好去。
谢谢