Android RecyclerView用代码动态设置item的selector

简介: Android RecyclerView用代码动态设置item的selector

ColorStateList

ColorStateList功能在XML中的使用

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

在XML中我们可以通过不同状态设置true然后设置color="hex_color"来达到不同状态显示不同颜色的目的,下面是总结的状态表

通过以上的设置我们就可以使用Color state list resource

在代码里动态设置状态色

public ColorStateList (int[][] states, int[] colors)

以上是ColorStateList的构造方法。第一个参数是状态,也就是上述表格中的状态;第二个参数是颜色,是第一个参数二维数组中的第一维数组对应状态集合对应的颜色。听上去很绕口,看代码就可以轻易了解了

private ColorStateList createColorStateList(int selected,int normal) {
        int[] colors = new int[] { selected,  normal};
        int[][] states = new int[2][];
        states[0] = new int[] { android.R.attr.state_selected , android.R.attr.state_enabled};
        states[1] = new int[] { android.R.attr.state_enabled };
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

上述代码states[0]一维数组里有两个状态,当着两个状态都成立的时候selected这个颜色才生效,所以这里回答了一个问题,为什么第一个参数是二维的,因为可以对应多个状态。然后我们把颜色设置即可。

textView.setTextColor(createColorStateList(Color.parseColor("#333ccc"),R.color.color_333333));

这样就可以代码动态设置状态颜色了。

注意:这个是为view设置颜色,不是背景,如果为背景设置颜色,需要使用StateListDrawable。

StateListDrawable

StateListDrawable所支持的11种状态

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>
StateListDrawable的使用
xml静态使用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />
Java动态代码构造并使用
//初始化一个空对象
StateListDrawable stalistDrawable = new StateListDrawable();
//获取对应的属性值 Android框架自带的属性 attr
int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
stalistDrawable.addState(newint []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1));
stalistDrawable.addState(newint []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2);
stalistDrawable.addState(newint []{selected }, getResources().getDrawable(R.drawable.pic3);
stalistDrawable.addState(newint []{focused }, getResources().getDrawable(R.drawable.pic4);
//没有任何状态时显示的图片,我们给它设置我空集合
stalistDrawable.addState(newint []{}, getResources().getDrawable(R.drawable.pic5);

设置item的selector

xml方式

直接在drawable文件夹中添加一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/icon_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/icon_select" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_default"/>
</selector>

或者

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="#FFFFFF" android:state_pressed="true" />
    <item android:drawable="#F3F3F3" />
</selector>

在drawable下创建的xml都是作为背景使用的,背景可以使用纯颜色也可以使用图片。

动态添加
StateListDrawable selector = new StateListDrawable();
 
Drawable pressDrawable = getResources().getDrawable(R.drawable.pressDrawableId);
Drawable selectDrawable = getResources().getDrawable(R.drawable.selectDrawableId);
Drawable defaultDrawable = getResources().getDrawable(R.drawable.defaultDrawableId);
 
 
selector.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
selector.addState(new int[]{android.R.attr.state_selected}, selectDrawable);
// 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效
selector.addState(new int[]{}, defaultDrawable);

然后将该select动态设置为控件背景。

public static StateListDrawable createStateListDrawable(String selectedColor, String normalColor) {
 
        StateListDrawable selector = new StateListDrawable();
 
        Drawable pressDrawable = new ColorDrawable(Color.parseColor(selectedColor));
        Drawable selectDrawable = new ColorDrawable(Color.parseColor(selectedColor));
        Drawable defaultDrawable = new ColorDrawable(Color.parseColor(normalColor));
 
        selector.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
        selector.addState(new int[]{android.R.attr.state_selected}, selectDrawable);
        // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效
        selector.addState(new int[]{}, defaultDrawable);
 
        return selector;
    }

使用:

Drawable background = ViewUtil.createStateListDrawable("#99cc00", "#dddddd");
 
frameLayout.setBackground(background);

将color转drawable

 
        // 使用 ColorDrawable 设置背景颜色
        int color = Color.RED;
        ColorDrawable colorDrawable = new ColorDrawable(color);
        findViewById(R.id.my_view).setBackground(colorDrawable);
 
        // 使用 GradientDrawable 设置背景颜色
        int color2 = Color.BLUE;
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(color2);
        findViewById(R.id.my_view2).setBackground(gradientDrawable);
相关文章
|
1月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
50 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
7天前
|
Android开发 开发者 索引
Android实战经验之如何使用DiffUtil提升RecyclerView的刷新性能
本文介绍如何使用 `DiffUtil` 实现 `RecyclerView` 数据集的高效更新,避免不必要的全局刷新,尤其适用于处理大量数据场景。通过定义 `DiffUtil.Callback`、计算差异并应用到适配器,可以显著提升性能。同时,文章还列举了常见错误及原因,帮助开发者避免陷阱。
28 9
|
8天前
|
存储 缓存 Android开发
Android RecyclerView 缓存机制深度解析与面试题
本文首发于公众号“AntDream”,详细解析了 `RecyclerView` 的缓存机制,包括多级缓存的原理与流程,并提供了常见面试题及答案。通过本文,你将深入了解 `RecyclerView` 的高性能秘诀,提升列表和网格的开发技能。
29 8
|
7天前
|
存储 Java Android开发
🔥Android开发大神揭秘:从菜鸟到高手,你的代码为何总是慢人一步?💻
在Android开发中,每位开发者都渴望应用响应迅速、体验流畅。然而,代码执行缓慢却是常见问题。本文将跟随一位大神的脚步,剖析三大典型案例:主线程阻塞导致卡顿、内存泄漏引发性能下降及不合理布局引起的渲染问题,并提供优化方案。通过学习这些技巧,你将能够显著提升应用性能,从新手蜕变为高手。
15 2
|
24天前
|
Android开发
Android经典实战之Textview文字设置不同颜色、下划线、加粗、超链接等效果
本文介绍了 `SpannableString` 在 Android 开发中的强大功能,包括如何在单个字符串中应用多种样式,如颜色、字体大小、风格等,并提供了详细代码示例,展示如何设置文本颜色、添加点击事件等,助你实现丰富文本效果。
66 3
|
1月前
|
JSON JavaScript 前端开发
Android调用Vue中的JavaScript代码
Android调用Vue中的JavaScript代码
17 3
|
27天前
|
图形学 iOS开发 Android开发
从Unity开发到移动平台制胜攻略:全面解析iOS与Android应用发布流程,助你轻松掌握跨平台发布技巧,打造爆款手游不是梦——性能优化、广告集成与内购设置全包含
【8月更文挑战第31天】本书详细介绍了如何在Unity中设置项目以适应移动设备,涵盖性能优化、集成广告及内购功能等关键步骤。通过具体示例和代码片段,指导读者完成iOS和Android应用的打包与发布,确保应用顺利上线并获得成功。无论是性能调整还是平台特定的操作,本书均提供了全面的解决方案。
104 0
|
1月前
|
开发工具 Android开发
Android项目架构设计问题之外部客户方便地设置回调如何解决
Android项目架构设计问题之外部客户方便地设置回调如何解决
18 0
|
Android开发 数据库