我正在使用Recyclerview.I有一个复选框,并且可以在我的项目背景中成功更改背景颜色。这是我的适配器代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<VinLoadModel> moviesList;
private int activeColor=Color.parseColor("#c6ffc6");
private int passivecolor=Color.parseColor("#ffc6c6");
private onClickListener onClickListener;
private int selectedPosition;
public RecyclerViewAdapter.onClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(RecyclerViewAdapter.onClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView vin, polygon;
public LinearLayout linearLayout;
public CheckBox checkBox;
public Button info;
public MyViewHolder(View view) {
super(view);
vin = (TextView) view.findViewById(R.id.vin_code);
polygon = (TextView) view.findViewById(R.id.polygon_name);
linearLayout = (LinearLayout) view.findViewById(R.id.mainLayout);
info=(Button)view.findViewById(R.id.info_click);
checkBox = (CheckBox) view.findViewById(R.id.checkBox);
}
}
public RecyclerViewAdapter(List<VinLoadModel> moviesList,int selectedPosition) {
this.moviesList = moviesList;
this.selectedPosition=selectedPosition;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vin_load_item_adapter, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
VinLoadModel movie = moviesList.get(position);
holder.vin.setText(movie.getVin());
holder.info.setText("i".toLowerCase());
holder.polygon.setText(movie.getPolygon());
if(selectedPosition==position)
holder.linearLayout.setBackgroundColor(activeColor);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
moviesList.get(position).setColorChanged(true);
holder.linearLayout.setBackgroundColor(activeColor);
}
else
{
moviesList.get(position).setColorChanged(false);
holder.linearLayout.setBackgroundColor(passivecolor);
}
}
});
holder.info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onClickListener!=null)
onClickListener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return moviesList.size();
}
public interface onClickListener
{
void onClick(int position);
}
现在我想通过Position更改项目背景颜色。我写了addOnItemTouchListener方法,但我有两个问题。 当我使用这种方法时,复选框监听器无法正常工作。是否可以使用这两种方法? 第二个问题。我可以改变例如第一个元素背景颜色,但是当我点击第二个元素时,第一个元素背景颜色是默认的(白色)
customListview.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), customListview, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
vinLoadAdapter = new RecyclerViewAdapter(vinLoadModels,position);
customListview.setAdapter(vinLoadAdapter);
}
@Override
public void onLongClick(View view, int position) {
}
}));
我想,如果我更改了第一个背景颜色,然后点击第二个项目,第一个背景颜色也改变了(不是默认的白色) 我怎么能解决我的问题? 谢谢大家
答案 0 :(得分:0)
你需要使用调色板
在listview适配器中使用它async
Palette.from(bitmap).maximumColorCount(numberOfColors).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Get the "vibrant" color swatch based on the bitmap
Palette.Swatch vibrant = palette.getVibrantSwatch();
if (vibrant != null) {
// Set the background color of a layout based on the vibrant color
containerView.setBackgroundColor(vibrant.getRgb());
// Update the title TextView with the proper text color
titleView.setTextColor(vibrant.getTitleTextColor());
}
}
});
这段代码将生成调色板颜色异步,你可以在适配器上使用它 Dyanamic colors using Color Palette