【问题标题】:SetBackGroundColor doesn't work in CardViewSetBackGroundColor 在 CardView 中不起作用
【发布时间】:2022-01-20 21:57:22
【问题描述】:

大家好,请帮帮我!!!

我想在 RecyclerView 中更改 CardView 颜色。

Here is the result, but I want to change different color for each CardView

这是我在 onBindViewHolder 中的代码

    public void onBindViewHolder(@NonNull CardViewHolder cardViewHolder, int i){
        Course course = getListCourses().get(i);
        cardViewHolder.tvCourseTitle.setText(course.getCourseTitle());
        cardViewHolder.tvCourseShortDescription.setText(course.getCourseShortDescription());
        cardViewHolder.imgCourse.setImageResource(course.getCourseImage());
        cardViewHolder.cardView.setCardBackgroundColor(Color.parseColor(course.getCourseColor()));
    }

    @Override
    public int getItemCount(){
        return getListCourses().size();
    }
    public class CardViewHolder extends RecyclerView.ViewHolder{
        private ImageView imgCourse;
        private TextView tvCourseTitle, tvCourseShortDescription;
        private CardView cardView, cardViewContainer;


        public CardViewHolder (@NonNull View itemView){
            super(itemView);
            imgCourse = itemView.findViewById(R.id.img_item_course);
            tvCourseTitle = itemView.findViewById(R.id.tv_item_title);
            tvCourseShortDescription = itemView.findViewById(R.id.tv_item_short_description);
            cardView = itemView.findViewById(R.id.cardView_background);
            cardViewContainer = itemView.findViewById(R.id.container);
        }
    }
}

这是Java.lang中的代码

    String courseTitle, courseShortDescription, courseColor;
    int courseImage;

    public Course(String title, String description, int image, String color){
        this.courseTitle = title;
        this.courseShortDescription = description;
        this.courseImage = image;
        this.courseColor = color;
    }

    public String getCourseTitle() {
        return courseTitle;
    }

    public void setCourseTitle(String courseTitle) {
        this.courseTitle = courseTitle;
    }

    public String getCourseShortDescription() {
        return courseShortDescription;
    }

    public void setCourseShortDescription(String courseShortDescription) {
        this.courseShortDescription = courseShortDescription;
    }

    public String getCourseColor() {
        return courseColor;
    }

    public void setCourseColor(String courseColor) {
        this.courseColor = courseColor;
    }

    public int getCourseImage() {
        return courseImage;
    }

    public void setCourseImage(int courseImage) {
        this.courseImage = courseImage;
    }
}

注意当我使用方法 cardViewHolder.cardView.setCardBackgroundColor(Color.parseColor(course.getCourseColor())); , 应用被毁了

【问题讨论】:

    标签: android android-studio cardview


    【解决方案1】:

    每个卡片视图的颜色不同,您可以在您的onBindViewHolder() 中使用这样的条件设置

       if (course.getCourseTitle().equalsIgnoreCase("Hello Android")){
            cardViewHolder.cardView.setBackgroundColor(R.color.teal_200);
        } else if (course.getCourseTitle().equalsIgnoreCase("Hello Python")){
            cardViewHolder.cardView.setBackgroundColor(R.color.teal_700);
        }else if (course.getCourseTitle().equalsIgnoreCase("Android Basic")){
            cardViewHolder.cardView.setBackgroundColor(R.color.design_default_color_error);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多