Android ListView 实现随机背景颜色

问题

在做一个Android小项目的时候,想实现如下图的一个效果:
在这里插入图片描述
主要是ListView中每个ItemView的背景颜色随机问题,当然这个颜色最好是自己预先设定好的几种,不然真要随机了,指不定变出什么奇奇怪怪的出来。
一开始觉得,很简单嘛,用setbackgroundColor()方法就行了
结果:
在这里插入图片描述
额。。。俺的圆角呢???
好吧。setbackgroundColor()这个方法好像把我的圆角给覆盖为默认的了
行,你不是会改背景样式吗,我也给你一并改了!

一、修改ListView的JavaBean

在JavaBean中增加一个Drawable类型的成员变量bgcolor,并添加相应的set()和get()方法:

private Drawable bgcolor;
    public Drawable getBgcolor() {
    
    
        return bgcolor;
    }
    public void setBgcolor(Drawable bgcolor) {
    
    
        this.bgcolor = bgcolor;
    }

二、修改ListView的Adapter适配器

1、在ViewHolder类中,添加需要修改背景颜色的那个组件

我这里是用的TextView,所以设置如下:

 class ViewHolder {
    
    
        TextView TimeViewWidth;
        //下面是其他组件,我就不列出来了
        ...
    }
2、修改getView方法

绑定组件:

 viewHolder.TimeViewWidth = view.findViewById(R.id.entire_TimeViewWidth);
entire_TimeViewWidth是TimeViewWidth的id,在相关的布局文件中设置。

最后,就是给这个组件使用setbackground()方法了:

EntireUsingBean entireusingrecords = getItem(position);
viewHolder.TimeViewWidth.setBackground(entireusingrecords.getBgcolor());
EntireUsingBean就是我自己的JavaBean类

三、创建相应的背景样式文件

我只想展示5种背景颜色,分别是红橙黄绿蓝,所以就创建了5个xml样式文件:
比如:红色的就是:background_red_timebar_.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid
                android:color="#ec6161"/>
            <corners
                android:radius="20dp"/>
        </shape>

    </item>
</layer-list>

其他几个的创建就简单了,只要复制粘贴,修改文件名称和其中的color属性就OK了。

注意:其中的radius属性就是圆角,需要跟外面的灰色布局的圆角一致

四、给ListView添加数据

终于到了最后一步了!
大部分人应该都是在Activity中设置数据的,我这里是在Fragment里,也不碍事
操作如下:

1、声明ListView组件并绑定
private ListView listview;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
View root = inflater.inflate(R.layout.fragment_mainuse, container, false) ;
listview = root.findViewById(R.id.entire_using_AllList);
/*
*** 其他代码
*/
}
如果是在Activity中,就更方便,直接用findViewById方法
2、声明数据列表
private List<EntireUsingBean> entireAppList = new ArrayList<>();
3、给数据列表赋值

产生一个大小为0~4的随机数colornum,然后用switch语句,根据colornum的值,设置对应的样式文件:
比如:随机数为0,就设置Eu的bgcolor属性为background_red_timebar_.xml

因为是Fragment,所以需要getContext()来获取当前的context,Activity的话就不需要了
Random r = new Random();
//根据自己的数据类型,来写for循环里面的遍历变量,我这里的数据是map类型,所以这样写
for (Map.Entry<String, AppUsageInfo> entry : EntireApps.entrySet()) {
    
    
//根据自己的构造方法,来声明对象
EntireUsingBean Eu = new EntireUsingBean();
int colornum = r.nextInt(100);
colornum = colornum%5;
switch (colornum){
    
    
    case 0:
        Eu.setBgcolor(getContext().getResources().getDrawable(R.drawable.background_red_timebar_));
        break;
    case 1:
        Eu.setBgcolor(getContext().getResources().getDrawable(R.drawable.background_orange_timebar_));
        break;
    case 2:
        Eu.setBgcolor(getContext().getResources().getDrawable(R.drawable.background_yellow_timebar_));
        break;
    case 3:
        Eu.setBgcolor(getContext().getResources().getDrawable(R.drawable.background_green_timebar_));
        break;
    case 4:
        Eu.setBgcolor(getContext().getResources().getDrawable(R.drawable.background_blue_timebar_));
        break;
/*
*** 其他代码
*/
            }

结果

现在来瞧一瞧成果吧
在这里插入图片描述
完美!
第一次写博客,没想到还挺不容易T_T
俺只是抛砖引玉,如果有更好的方法,欢迎大家在评论区留言呀


猜你喜欢

转载自blog.csdn.net/qq_40466537/article/details/110201736
今日推荐