Android ListView那些事
来源:互联网 发布:python 人工智能 开发 编辑:程序博客网 时间:2024/04/29 11:04
ListView是我们在开发Android程序时用得比较多的一种widget,通常用来展示多条数据,这里,我对ListView的一些功能点作一个简单介绍。
1. Cache color hint
但是,由于系统默认的cache color hint的颜色是#191919,当你滑动ListView时,你就会看到一个黑色的背景。
出现这种情况肯定是不正确的,那么怎么解决呢?
最简单的方法是将cache color hint设置为透明。
android:cacheColorHint="#00000000" 或 setCacheColorHint(Color.TRANSPARENT)
以下是官方文档的原文:
As mentioned before, ListView has a transparent/translucent background by default, and so all default widgets in the Android UI toolkit. This implies that when ListView redraws its children, it has to blend the children with the window's background. Once again, this requires costly readbacks from memory that are particularly painful during a scroll or a fling when drawing happens dozen of times per second.
To improve drawing performance during scrolling operations, the Android framework reuses the cache color hint. When this hint is set, the framework copies each child of the list in a Bitmap filled with the hint value (assuming that another optimization, called scrolling cache, is not turned off). ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. Also, since the default cache color hint is #191919, you get a dark background behind each item during a scroll.
To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file.
2. Divider
3. ListView绘制顺序
由于通过情况下,listview的item的背景是透明的,所以,当用户按下一个item时,黄色的selector就会透出来。假如你的item是一个TextView,你将其背景设置为白色,那么当按钮一个item是时,selector就不会显示出来了,因为它是绘制在所有item的下面。
所以,对于ListView来说,其绘制顺序是:Background -> Dividers -> List selector -> Itemviews
4. OnItemClickListener不触发
- ...
- case MotionEvent.ACTION_UP: {
- switch (mTouchMode) {
- case TOUCH_MODE_DOWN:
- case TOUCH_MODE_TAP:
- case TOUCH_MODE_DONE_WAITING:
- final int motionPosition = mMotionPosition;
- final View child = getChildAt(motionPosition - mFirstPosition);
- final float x = ev.getX();
- final boolean inList = x > mListPadding.left
- && x < getWidth() - mListPadding.right;
- if (child != null && !child.hasFocusable() && inList) {
- if (mTouchMode != TOUCH_MODE_DOWN) {
- child.setPressed(false);
- }
- if (mPerformClick == null) {
- mPerformClick = new PerformClick();
- }
- ...
public static final int descendantFocusability
注意:
还有一种情况也会导致OnItemClickListener或OnItemLongClickListener回调不会执行,那就是ListView的child设置了onClickListener或onLongClickListener。我们可以通过源代码看出,在你调用setOnClickListener()方法后,它会调用setClickable(true),在onTouchEvent里面的实现如下:
- if (((viewFlags & CLICKABLE) == CLICKABLE ||
- (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
- // ....
- return true;
- }
5. ListView不要做的事
- Android ListView那些事
- Android ListView那些事
- android listview的填充对象有那些
- android listview+RatingBar的那些坑
- 【Android开发经验】关于ListView中item与控件抢夺焦点的那些事
- Android关于ListView中item与控件抢夺焦点的那些事
- Android关于ListView中item与控件抢夺焦点的那些事
- 【Android开发经验】关于ListView中item与控件抢夺焦点的那些事
- Android关于ListView中item与控件抢夺焦点的那些事
- 《Android那些事》——ListView中Button点击事件解决方案
- 《Android那些事》——ScrollView嵌套ListView只显示一行的问题的解决方法
- 项目中那些事| ListView 一些问题
- ListView加载数据的那些事
- 关于ListView的那些事(笔记)
- 有关Scrollview嵌套ListView的那些事
- 项目中那些事|ListView中嵌套ListView问题
- android listview中那些后知后觉的属性或方法
- android JNI那些事
- 『java』java中的一些标签示例
- 线段树单点更新
- 怎么样才算作弊搜索引擎
- Android L liblog
- 二分法+快速幂POJ 3233
- Android ListView那些事
- 最短路径之 floyd (适用于 多点距离)
- K-means聚类算法
- 控件CCtrlList的虚拟列表使用方法--详解
- VS2010/MFC入门编程二十(radio botton 的使用)
- 百度指数邀请码有没有?
- 快速幂POJ 1061
- 如果音乐网站因为版权问题而被关闭,那FLASH站点该怎么办?
- Ubuntu 鼠标一直闪动的问题解决