小编Aam*_*mir的帖子

在Android的Recycler View中的文本视图背景上生成并设置随机颜色

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

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

    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 …
Run Code Online (Sandbox Code Playgroud)

random android colors textview android-recyclerview

6
推荐指数
2
解决办法
6038
查看次数

标签 统计

android ×1

android-recyclerview ×1

colors ×1

random ×1

textview ×1