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
提交
取消