为ListView同时设置点击时的背景和点击松手之后的背景
来源:互联网 发布:ubuntu uefi 安装 编辑:程序博客网 时间:2024/06/12 21:35
本例要达到的效果是,
(1)点击ListView的item时会有指定的背景,
(2)松手之后,刚才点击的item也会有指定的背景
实现(1)很简单:在xml中为ListView设置listSelector即可。
<ListView android:id="@+id/pop_listview_left" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" android:divider="@color/popup_left_bg" android:dividerHeight="1dp" android:listSelector="@color/popup_right_bg" android:scrollingCache="false" />
实现(2)也很简单,在adapter中动态改变背景:
if (position == selectedPosition){ convertView.setBackgroundResource(R.color.left_selected); }else{ convertView.setBackgroundResource(R.color.left_normal); }并且在该ListView的点击事件中及时更新selectedPosition:
leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //更新背景色 FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter()); adapter.setSelectedPosition(position); adapter.notifyDataSetChanged(); } });
可是,问题出来了:设置了(2)之后,(1)的效果没了!!!
这是因为,在设置
convertView.setBackgroundResource(R.color.left_selected);
时,(1)中listSelector中指定的颜色会被覆盖。
解决方法有两种:
(一)
将convertView的一个纯色的背景改为一个selector,并设置其在点击时的颜色为透明(这样下面listSelector的颜色就露出来了)。以下是selector_left_normal.xml和selector_left_selected.xml.
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@android:color/transparent"/> <item android:state_pressed="false" android:drawable="@color/popup_left_bg"/></selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@android:color/transparent"/> <item android:state_pressed="false" android:drawable="@color/popup_right_bg"/></selector>
然后将(2)中的代码改为:
if (position == selectedPosition){ convertView.setBackgroundResource(R.drawable.selector_left_selected); }else{ convertView.setBackgroundResource(R.drawable.selector_left_normal); }
(二)
参照(一),将ListView的listSelector属性去掉,并将其颜色复制到上面两个selector中替换transparent那个颜色。
也就是说,每次点击ListView的条目之后,设置背景色时,
(a)如果该条目现在是选中状态,则直接设为某一个颜色
(b)否则,将其颜色设置为一个selector,并在selector中分别指定点击和没有点击时的颜色。
问题圆满解决。
0 0
- 为ListView同时设置点击时的背景和点击松手之后的背景
- UITableViewCell点击不松手时Cell的背景设置
- listview设置点击的当前项的背景
- Android中的ListView点击时的背景颜色设置
- Android中的ListView点击时的背景颜色设置
- listview 添加点击时的背景颜色
- Android的List点击某一项之后的背景设置
- Android的List点击某一项之后的背景设置
- ListView透明背景点击设置
- 设置GridView点击时的背景颜色
- 点击listview,改变item的背景颜色设置
- 去掉ListView滑动的黑色背景和点击ListView某个item的橙黄色背景
- Android之去掉ListView的点击背景
- 点击ListView时背景阴影
- 去掉ListView发生点击时的黄色背景zai
- //点击listview的某一项改变其背景颜色,(例如,点击该项时该项的背景为黄色,其他没被点击的项,背景为率色(默认))
- android中为listview同时设置长按项onItemLongClick和点击事件onItemClick出现的问题
- Android:ListView设置item点击背景效果
- Java中Collection子接口(1)——List接口(传智播客毕老师视频讲解)
- springmvc 框架,后台跳转到Jsp页面后,显示的是源码
- Linux下关于snmp的snmpwalk命令
- SAP开发者指引-RFC的使用
- Const,Const函数,Const变量,函数后面的Const
- 为ListView同时设置点击时的背景和点击松手之后的背景
- JS动态修改界面元素后,其增加元素的class无效
- SSI框架总结
- 精致树叶鼠标指针(两款)
- 用递归对数组进行全排列
- Linux workqueue 使用
- Oracle触发器的概念和类型
- Android 中自定义控件之判断还剩多少可输入字符的EditText
- 上研后的一些体会