猿问

如何更改卡片视图的背景颜色?

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() {


                }

            });

        }


    }

}


喵喔喔
浏览 179回答 1
1回答

慕少森

IsColorChanged您可以在您的内部添加标志,notificationsModel并且每当您更改cardview模型中更新标志的颜色时,在您的onBindViewHolder中,您可以添加如下检查:if(IsColorChanged){&nbsp; &nbsp; cardView.setCardBackgroundColor(Color.WHITE);}else{&nbsp; &nbsp; cardView.setCardBackgroundColor(Color.BLACK);// ANY OTHER COLOR}
随时随地看视频慕课网APP

相关分类

Java
我要回答