Android(2.2/2.3系统)Gallery解决默认和横竖屏切换选中状态问题
作者:lqh 时间:2022-09-30 19:06:18
前言
Gallery的Item使用的是一个ImageView+TextView,并且为其设置了selector,当使用setSelection设置时、横竖屏切换时Item的状态不会改变,这个目前在2.2/2.3系统中存在,高版本如4.0是不存在的。
正文
一、第一步,解决ImageView的状态问题
为ImageView设置 :android:focusableInTouchMode="true"
注意同样属性设置TextView不管用。
二、第二步,手动控制文本根据状态不同设置不同颜色
2.1 在onConfigurationChanged中通知Gallery的Adapter数据更新notifyDataSetChanged
2.2 在Gallery的onItemSelected中通知Gallery的Adapter数据更新notifyDataSetChanged
2.3 在Gallery Adapter的getView中根据当前选中索引修改文本颜色
if (position == mGallery.getSelectedItemPosition()) {
title.setTextColor(COLOR_NAV_SELECTED);
} else
title.setTextColor(COLOR_NAV_NORMAL);
其中COLOR_NAV_SELECTED和COLOR_NAV_NORMAL分别是选中和未选中的颜色。
结束
注意,该问题在手动滑动Gallery时不存在,通过setSelection更改Gallery选中项时存在,且高版本中不存在。
标签:Android,Gallery
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#的sortedlist使用方法
2023-09-22 04:58:13
mybatis <foreach>标签动态增删改查方式
2022-07-27 20:03:16
java实现支付宝退款功能
2021-09-25 14:00:00
![](https://img.aspxhome.com/file/2023/0/62240_0s.jpg)
SpringBoot使用自定义注解+AOP+Redis实现接口限流的实例代码
2022-04-11 09:15:45
![](https://img.aspxhome.com/file/2023/2/101792_0s.png)
c#基数排序Radix sort的实现方法
2021-07-25 02:02:21
详解使用Spring Boot开发Restful程序
2023-01-24 09:20:09
![](https://img.aspxhome.com/file/2023/3/128863_0s.png)
使用Spring Boot AOP处理方法的入参和返回值
2022-03-02 07:25:41
![](https://img.aspxhome.com/file/2023/3/87363_0s.jpg)
Java Shutdown Hook场景使用及源码分析
2023-05-19 06:01:30
![](https://img.aspxhome.com/file/2023/2/99722_0s.png)
Java深入探究关键字abstract的使用
2023-08-03 07:48:20
Android WebView如何判定网页加载的错误
2023-09-22 14:33:20
![](https://img.aspxhome.com/file/2023/1/103911_0s.png)
C#的3DES加密解密算法实例代码
2023-09-23 01:10:19
C# 静态构造函数使用总结
2022-04-28 16:27:35
![](https://img.aspxhome.com/file/2023/5/129635_0s.jpg)
Android开发之时间日期操作实例
2021-10-20 16:16:55
![](https://img.aspxhome.com/file/2023/3/138613_0s.png)
Spring boot外部配置(配置中心化)详解
2022-07-11 23:13:26
Android如何使用Bmob后端云实现失物招领功能
2023-10-15 07:28:05
![](https://img.aspxhome.com/file/2023/6/100636_0s.jpg)
Android中的HOOK技术是什么
2022-06-19 18:18:24
![](https://img.aspxhome.com/file/2023/2/138942_0s.jpg)
深入分析Java内存区域的使用详解
2023-09-25 23:21:29
员工管理系统java版
2022-05-04 23:33:18
![](https://img.aspxhome.com/file/2023/3/81943_0s.jpg)
Java设计模式之java装饰者模式详解
2023-10-27 06:17:04
![](https://img.aspxhome.com/file/2023/6/68726_0s.png)
Android中Matrix用法实例分析
2023-07-07 18:40:22
![](https://img.aspxhome.com/file/2023/7/113317_0s.png)