Cardview一旦它被点击,我试图改变它的背景颜色。这里列出通知历史记录RecyclerView。Cardview颜色只在那个时候改变。如果我返回并再次打开应用程序意味着它不会被更改。我必须做什么?这是我的适配器类public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.ViewHolder> { public static final String TAG = "DataViewHolder"; private Context context; private ArrayList<NotificationsModel> notificationsModelList; public NotificationsAdapter(Context context, ArrayList<NotificationsModel> notificationsModelList) { this.context = context; this.notificationsModelList = notificationsModelList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.notification_layout, parent, false); Log.d(TAG, "onCreateViewHolder: "); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { final NotificationsModel notificationsModel = notificationsModelList.get(position); holder.title_news.setText(notificationsModel.getTitle()); holder.notif_message.setText(notificationsModel.getMessage()); } @Override public int getItemCount() { return notificationsModelList == null ? 0 : notificationsModelList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { TextView notif_message, title_news; //Initializing Views public ViewHolder(View itemView) { super(itemView); title_news = (TextView) itemView.findViewById(R.id.title_news); notif_message = (TextView) itemView.findViewById(R.id.notif_message); CardView cardView = (CardView) itemView.findViewById(R.id.cardViewLayoutNotif); itemView.findViewById(R.id.notif_message); itemView.setOnClickListener(new View.OnClickListener() { } }); } }}
1 回答

慕少森
TA贡献2019条经验 获得超9个赞
IsColorChanged您可以在您的内部添加标志,notificationsModel并且每当您更改cardview模型中更新标志的颜色时,
在您的onBindViewHolder中,您可以添加如下检查:
if(IsColorChanged){
cardView.setCardBackgroundColor(Color.WHITE);
}else{
cardView.setCardBackgroundColor(Color.BLACK);// ANY OTHER COLOR
}
添加回答
举报
0/150
提交
取消