我正在尝试在滑动时更改recyclerView
项的颜色,使其高亮,因为在滑动时我会打开一个AlertDialog
,这样人们就可以识别他要更改哪个项目。
但我的主要问题是,当我尝试使用以下代码更改背景颜色时:
viewHolder.itemView.setBackgroundColor(Color.parseColor("#cc0000"));
什么也没有发生,这是我从recyclerView
构建器中提取的完整代码。
public void buildTopRecyclerView(){
mRecyclerViewTOP = findViewById(R.id.listView);
mRecyclerViewTOP.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
exampleAdapter = new ExampleAdapter(itemCassas);
mRecyclerViewTOP.setLayoutManager(mLayoutManager);
mRecyclerViewTOP.setAdapter(exampleAdapter);
// onSwipe();
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
if (direction == ItemTouchHelper.RIGHT) {
customAllertDelete(position);
viewHolder.itemView.setBackgroundColor(Color.parseColor("#cc0000"));
exampleAdapter.notifyDataSetChanged();
}
if (direction == ItemTouchHelper.LEFT) {
customAllertQuantity(position);
}
}
}).attachToRecyclerView(mRecyclerViewTOP);
}
如果有用的话,这里也是适配器代码
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ItemCassa> mExampleList;
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerlist_item,parent,false);
return new ExampleViewHolder(v);
}
ExampleAdapter(ArrayList<ItemCassa> exampleList){
mExampleList = exampleList;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ItemCassa item = mExampleList.get(position);
holder.desc.setText(item.getBtnName());
holder.qta.setText(String.valueOf(item.getQuant()));
holder.imp.setText(String.valueOf((new DecimalFormat("#0.00").format(item.getPrice()))));
if(position % 2 == 0 ){
holder.itemView.setBackgroundColor(Color.parseColor("#C0C0C0"));
holder.desc.setBackgroundColor(Color.parseColor("#C0C0C0"));
holder.qta.setBackgroundColor(Color.parseColor("#C0C0C0"));
holder.imp.setBackgroundColor(Color.parseColor("#C0C0C0"));
}else if(position % 2 == 1){
holder.itemView.setBackgroundColor(Color.parseColor("#D3D3D3"));
holder.desc.setBackgroundColor(Color.parseColor("#D3D3D3"));
holder.qta.setBackgroundColor(Color.parseColor("#D3D3D3"));
holder.imp.setBackgroundColor(Color.parseColor("#D3D3D3"));
}
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView desc;
public TextView qta;
public TextView imp;
ExampleViewHolder(View itemView) {
super(itemView);
desc = itemView.findViewById(R.id.Desc);
qta = itemView.findViewById(R.id.Qta);
imp = itemView.findViewById(R.id.Imp);
}
}
public void removeItem(int position) {
mExampleList.remove(position);
notifyItemRemoved(position);
}
}
这是一个GIF,稍微“解释”了一下我的意图。如您所见,在滑动时会打开AlertDialog并返回所选项目,但现在我想将所选项目的背景颜色设为彩色,直到用户在AlertDialog中作出选择。