在Android的Recycler View中的文本视图背景上生成并设置随机颜色 [英] Generate and set random colors on text view background within Recycler View in Android

查看:65
本文介绍了在Android的Recycler View中的文本视图背景上生成并设置随机颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成随机颜色,并将随机颜色设置为文本视图"的背景,就像在GMail应用程序中一样.文本视图的圆形背景最初是在xml中设置的,这是我使用形状完成的.我进行了一些研究,并使用了Internet上可用的一些代码,但所做的更改未反映在我的应用程序中.

I am Trying to Generate Random Colors and set the Random color as background of Text View Just Like in GMail app. The Text view is Having a circular background initially set in xml which i have done using shape. I have done some research and used some code available on internet but the changes are not reflecting in my app.

以下是我的Recycler View适配器类:

Below is my Recycler View Adapter Class:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Items> {
        ArrayList<GmailDataHolder> data;
        Context context;

        public RecyclerViewAdapter(ArrayList<GmailDataHolder> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public RecyclerViewAdapter.Items onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.gmail_layout_row, parent, false);
            Items i = new Items(v);
            return i;


        }

        @Override
        public void onBindViewHolder(final RecyclerViewAdapter.Items holder, int position) {

//Generating Random Color
            int randomAndroidColor = holder.androidColors[new Random().nextInt(holder.androidColors.length)];
            Drawable background = holder.circleTv.getBackground();
            if (background instanceof ShapeDrawable) {
                ((ShapeDrawable)background).getPaint().setColor(randomAndroidColor);
            } else if (background instanceof GradientDrawable) {
                ((GradientDrawable)background).setColor(randomAndroidColor);
            } else if (background instanceof ColorDrawable) {
                ((ColorDrawable)background).setColor(randomAndroidColor);
            }
            holder.line1.setText(data.get(position).getLine1());
            holder.line2.setText(data.get(position).getLine2() + "...");
            holder.line3.setText(data.get(position).getLine3() + "...");
            holder.time.setText(data.get(position).getTime());


            //get Star Image State from MySql DB

            MyFunctions.getStarState(data, holder, position);
    holder.circleTv.setText(String.valueOf(data.get(position).getLine1().charAt(0)).toUpperCase());

            //Changing Star Image on Click
            MyFunctions.starClickListener(holder);


        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        public class Items extends RecyclerView.ViewHolder {
            TextView circleTv, line1, line2, line3, time;
            int[] androidColors;
            public ImageView star;

            public Items(View itemView) {
                super(itemView);
//Loading Color from resources
                androidColors = itemView.getResources().getIntArray(R.array.androidcolors);
                circleTv = (TextView) itemView.findViewById(R.id.tv_circle);
                line1 = (TextView) itemView.findViewById(R.id.tv_line1);
                line2 = (TextView) itemView.findViewById(R.id.tv_line2);
                line3 = (TextView) itemView.findViewById(R.id.tv_line3);
                time = (TextView) itemView.findViewById(R.id.tv_time);
                star = (ImageView) itemView.findViewById(R.id.img_star);


            }
        }

Colors.xml文件:

Colors.xml File:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#dc4538</color>
    <color name="colorFacebook">#374dae</color>
    <color name="colorCenterFb">#d5617ae6</color>
    <color name="colorStartGoogle">#d8dd4c3a</color>
    <color name="colorEndGoogle">#dd4c3a</color>
    <color name="colorEndLinkedIn">#1887b0</color>
    <color name="colorStartLinkedIn">#e31887b0</color>
    <color name="colorStrokeLinkedIn">#ec106584</color>
    <color name="colorStrokeGoogle">#b73e2e</color>
    <color name="colorStrokeFacebook">#e2263a91</color>
    <color name="status">#ba3223</color>
    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

    <integer-array name="androidcolors">
        <item>@color/blue</item>
        <item>@color/purple</item>
        <item>@color/green</item>
        <item>@color/orange</item>
        <item>@color/red</item>
        <item>@color/darkblue</item>
        <item>@color/darkpurple</item>
        <item>@color/darkgreen</item>
        <item>@color/darkorange</item>
        <item>@color/darkred</item>
    </integer-array>


</resources>

最初,TextView在xml中声明了以下Background:

Initially The TextView is having the following Background declared in xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>

推荐答案

如果您需要使用自己的/所需颜色,只需使用以下代码

If you needs to use your own/required colors, just use below code

List<String> colors;

colors=new ArrayList<String>();

        colors.add("#5E97F6");
        colors.add("#9CCC65");
        colors.add("#FF8A65");
        colors.add("#9E9E9E");
        colors.add("#9FA8DA");
        colors.add("#90A4AE");
        colors.add("#AED581");
        colors.add("#F6BF26");
        colors.add("#FFA726");
        colors.add("#4DD0E1");
        colors.add("#BA68C8");
        colors.add("#A1887F");

// all colors used by gmail application :) may be,

 // genrating random num from 0 to 11 because you can add more or less 

Random r = new Random();
        int i1 = r.nextInt(11- 0) + 0;

//genrating shape with colors 

GradientDrawable draw = new GradientDrawable();
        draw.setShape(GradientDrawable.OVAL);
        draw.setColor(Color.parseColor(colors.get(i1)))

// assigning to textview 
contact_name_circle.setBackground(draw); //textview

这篇关于在Android的Recycler View中的文本视图背景上生成并设置随机颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆