RecyclerView 中 setBackground 后 view 大小改变解决方式

200 阅读1分钟

将xml中默认的background去掉,布局设置为android:layout_width="wrap_content" android:layout_height="wrap_content"

onBindViewHolder方法中 设置setBackgroundResource之前设置,使用下面俩个中的任意一个,即可保证notifyDataSetChanged();时候item的大小不会改变。 holder.xx.setBackgroundResource(0); holder.xx.setBackground(null);

if (item.isChecked()) {
  holder.xx.setTextColor(context.getResources().getColor(R.color.color_4285f4));
  holder.xx1.setBackgroundResource(0);
  //holder.root.setBackground(null);
  holder.xx1.setBackgroundResource(R.drawable.shape_stroke1_color_4285f4);
} else {
  holder.xx.setBackgroundResource(0);
  //holder.xx.setBackground(null);
  holder.xx.setTextColor(context.getResources().getColor(R.color.color_5e5f66));
  holder.xx1.setBackgroundResource(R.drawable.shape_stroke1_color_4285f4_2);
}