简体   繁体   English

更改回收者视图的itemview内可绘制渐变的颜色

[英]Changing color of a gradient drawable inside an itemview of a recycler-view

I'm building an app which is supposed to be visually intuitive to use. 我正在构建一个应该在视觉上直观使用的应用程序。 So, I have a gradient drawable in each item of a recycler view which is supposed to change its color on each click to a darker shade of color which it previously was. 因此,我在回收站视图的每个项目中都有一个可绘制的渐变,该渐变应该在每次单击时将其颜色更改为以前的深色。 Eg from white to pink to red to maroon on clicking 4 times on a particular gradient drawable. 例如,在特定的渐变绘制上单击4次,从白色到粉红色到红色到栗色。 I'm a beginner at this, can it be done? 我是一个初学者,可以做到吗?

Tried going through documentation nothing helped. 尝试浏览文档无济于事。

Here's my code of interface which holds the array of array of colors. 这是我的接口代码,其中包含颜色数组。

public interface ColorUtils {

 int[][] colorsArray=new int[][]{
       new int[]{R.color.red1, R.color.red2, R.color.red3, R.color.red4 },
       new int[]{R.color.blue1, R.color.blue2, R.color.blue3, R.color.blue4},
       new int[]{R.color.green1, R.color.green2, R.color.green3, R.color.green4},
       new int[]{R.color.yellow1, R.color.yellow2, R.color.yellow3, R.color.yellow4},
       new int[]{R.color.orange1, R.color.orange2, R.color.orange3, R.color.orange4},
       new int[]{R.color.brown1, R.color.brown2, R.color.brown3, R.color.brown4},
       new int[]{R.color.grey1, R.color.grey2, R.color.grey3, R.color.grey4}
 };

}

Here's what I am trying to do on onBindViewHolder method. 这是我要在onBindViewHolder方法上执行的操作。

 int countOfClicks=0;
 @Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {



    final GradientDrawable gd = (GradientDrawable) myViewHolder.image.getDrawable().getCurrent();
    final ImageView exclamation = myViewHolder.exclamation;
    final ImageView smily = myViewHolder.smily;

    gd.setColor(Color.WHITE);
    myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            countOfClicks++;
            switch (countOfClicks) {
                case 1:

                    gd.setColor(ColorUtils.colorsArray[myViewHolder.getAdapterPosition()][countOfClicks]);

                    break;
                case 2:

                    gd.setColor(ColorUtils.colorsArray[myViewHolder.getAdapterPosition()][countOfClicks]);

                    break;
                case 3:
                    gd.setColor(ColorUtils.colorsArray[myViewHolder.getAdapterPosition()][countOfClicks]);


                    break;

                default:

                    gd.setColor(ColorUtils.colorsArray[myViewHolder.getAdapterPosition()][countOfClicks]);
                    exclamation.setVisibility(View.GONE);
                    smily.setVisibility(View.VISIBLE);



            }
        }
    });

}

You keep in your Adapter a Hashmap, or a list with the values of the colors. 您可以在适配器中保存哈希表或包含颜色值的列表。 Or you set on your myViewHolder.image as a tag the value like: 或者,您在myViewHolder.image上将标签设置为类似以下值:

myViewHolder.image.setTag(ColorUtils.colorsArray[myViewHolder.getAdapterPosition()])

Then onClick you increment the clickcount, and do notifyDataSetChanged. 然后onClick可以增加点击计数,并执行notifyDataSetChanged。 in onBindViewHolder, you getTag() and based on the value you get, you set the correct color. 在onBindViewHolder中,您可以获取getTag()并根据获取的值来设置正确的颜色。

Why it doesn't work for you? 为什么对您不起作用? 1. You do not call onClick notifyDataSetChanged. 1.您不调用onClick notifyDataSetChanged。 2. If you would call notifyDataSetChanged, it goes through the onBindViewHolder and that causes it to turn white gd.setColor(Color.WHITE); 2.如果您要调用notifyDataSetChanged,它将通过onBindViewHolder并使它变成白色gd.setColor(Color.WHITE);

If you do instead of setColor(Colow.WHITE) the color based on TAG, it will work. 如果您代替setColor(Colow.WHITE)使用基于TAG的颜色,它将起作用。

Another option would be to create a hashmap, of position and viewHolder: 另一种选择是创建一个具有position和viewHolder的哈希图:

var lstHolders: HashMap<Int, MyViewHolder> = HashMap()

onBindViewHolder you do: onBindViewHolder,您可以执行以下操作:

  synchronized(lstHolders) {
      lstHolders.put(position, holder)
  }

And in your MyViewHolder you create a method like void changeColor() then onClickListener you get the viewholder based on your position and call changeColor(). 然后在MyViewHolder中创建一个像void changeColor()之类的方法,然后在onClickListener中根据您的位置获取该Viewholder并调用changeColor()。 This will change the color, without needing to call notidyDataSetChanged (aka will only modify that and not all views) 这将更改颜色,而无需调用notidyDataSetChanged(aka只会修改该颜色,而不会修改所有视图)

okay i solved it. 好吧,我解决了。 i have 7 items within my recycler-view, made a separate int constant for each, initialised them to 0. Then, made a method that increments click-count by one and initialised it within on click method within on Bind viewholder method is triggered, it stores the click count now according to adapter position. 我在回收器视图中有7个项目,分别为每个int常数,将它们初始化为0。然后,创建了一种将点击计数增加1并在bind viewholder方法中的click方法中初始化的方法,它现在根据适配器位置存储点击计数。

public int thisWasClicked(CustomCowAdapter.CowViewHolder cowViewHolder) {

    int position = cowViewHolder.getAdapterPosition();

    switch (position) {
        case 0:
            return setColorToDrawable(position, clicks1++);

        case 1:
            return setColorToDrawable(position, clicks2++);

        case 2:
            return setColorToDrawable(position, clicks3++);

        case 3:
            return setColorToDrawable(position, clicks4++);

        case 4:
            return setColorToDrawable(position, clicks5++);

        case 5:
            return setColorToDrawable(position, clicks6++);

        default:
            return setColorToDrawable(position, clicks7++);

    }


}


public int setColorToDrawable(int i, int clicks) {
    if (clicks < 4) return colorsArray[i][clicks];
    else return -1;
}

then in onBindViewHolder 然后在onBindViewHolder中

cowViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int color = colorUtils.thisWasClicked(cowViewHolder);
            if (color > 0) {
                gd.setColor(ContextCompat.getColor(context, color));
            }
            if (color < 0) {
                gd.setColor(Color.WHITE);
                exclamation.setVisibility(View.GONE);
                smily.setVisibility(View.VISIBLE);

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM