- 浏览: 553483 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
0759cxz:
good job!
Android 同时使用多个library时的问题 -
zhouxiaoli521:
lgj452555712 写道cursor 没有关闭,最好加上 ...
Android sqlite中判断某个表是否存在方法 -
lgj452555712:
cursor 没有关闭,最好加上finally 后关上fina ...
Android sqlite中判断某个表是否存在方法 -
zhouxiaoli521:
zhouxiaoli521 写道weinifk 写道publi ...
StringBuffer的setLength -
zhouxiaoli521:
weinifk 写道public static void ma ...
StringBuffer的setLength
ListView 和 Adapter 的基础 工作原理: 如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这不可能! 实际上Android为你缓存了视图。 Android中有个叫做Recycler的构件,下图是他的工作原理: 请看下面的示例代码,这里在getView中使用了System.out进行输出 执行程序,然后在Logcat中查看日志 getView 被调用 9 次 ,convertView 对于所有的可见项目是空值(如下) 然后稍微向下滚动List,直到item10出现: convertView仍然是空值,因为recycler中没有视图(item1的边缘仍然可见,在顶端) 再滚动List convertView不是空值了!item1离开屏幕到Recycler中去了,然后item11被创建 再滚动: convertView 如我们所期待的非空了,在item11离开屏幕之后,它的视图(@437430f8)作为convertView容纳item21了 我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线 你需要做这些: 以下是代码: 运行程序,你会看到每4个item一个分割线 看看日志,无异常,所有的convertView都是空的 滚动list: convertView对于分割线是空的,直到第一个分割线可见,当其离开屏幕,视图去到Recycler并且convertView开始起作用。 本文翻译自http://android.amberfog.com/?p=296 代码下载:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 0; i < 50; i++) {
mAdapter.addItem("item " + i);
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item1, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
02-05 13:47:32.559: INFO/System.out(947): getView 0 null
02-05 13:47:32.570: INFO/System.out(947): getView 1 null
02-05 13:47:32.589: INFO/System.out(947): getView 2 null
02-05 13:47:32.599: INFO/System.out(947): getView 3 null
02-05 13:47:32.619: INFO/System.out(947): getView 4 null
02-05 13:47:32.629: INFO/System.out(947): getView 5 null
02-05 13:47:32.708: INFO/System.out(947): getView 6 null
02-05 13:47:32.719: INFO/System.out(947): getView 7 null
02-05 13:47:32.729: INFO/System.out(947): getView 8 null
02
-
05
13
:
48
:
25.169
: INFO/System.out(
947
): getView
9
null
02
-
05
13
:
48
:
42.879
: INFO/System.out(
947
): getView
10
android.widget.LinearLayout
@437430f8
02
-
05
14
:
01
:
31.069
: INFO/System.out(
947
): getView
11
android.widget.LinearLayout
@437447d0
02
-
05
14
:
01
:
31.142
: INFO/System.out(
947
): getView
12
android.widget.LinearLayout
@43744ff8
02
-
05
14
:
01
:
31.279
: INFO/System.out(
947
): getView
13
android.widget.LinearLayout
@43743fa8
02
-
05
14
:
01
:
31.350
: INFO/System.out(
947
): getView
14
android.widget.LinearLayout
@43745820
02
-
05
14
:
01
:
31.429
: INFO/System.out(
947
): getView
15
android.widget.LinearLayout
@43746048
02
-
05
14
:
01
:
31.550
: INFO/System.out(
947
): getView
16
android.widget.LinearLayout
@43746870
02
-
05
14
:
01
:
31.669
: INFO/System.out(
947
): getView
17
android.widget.LinearLayout
@43747098
02
-
05
14
:
01
:
31.839
: INFO/System.out(
947
): getView
18
android.widget.LinearLayout
@437478c0
02
-
05
14
:
03
:
30.900
: INFO/System.out(
947
): getView
19
android.widget.LinearLayout
@43748df0
02
-
05
14
:
03
:
32.069
: INFO/System.out(
947
): getView
20
android.widget.LinearLayout
@437430f8
不同的项目布局(item layout)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("item " + i);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
02
-
05
15
:
19
:
03.080
: INFO/System.out(
1035
): getView
0
null
type =
0
02
-
05
15
:
19
:
03.112
: INFO/System.out(
1035
): getView
1
null
type =
0
02
-
05
15
:
19
:
03.130
: INFO/System.out(
1035
): getView
2
null
type =
0
02
-
05
15
:
19
:
03.141
: INFO/System.out(
1035
): getView
3
null
type =
0
02
-
05
15
:
19
:
03.160
: INFO/System.out(
1035
): getView
4
null
type =
1
02
-
05
15
:
19
:
03.170
: INFO/System.out(
1035
): getView
5
null
type =
0
02
-
05
15
:
19
:
03.180
: INFO/System.out(
1035
): getView
6
null
type =
0
02
-
05
15
:
19
:
03.190
: INFO/System.out(
1035
): getView
7
null
type =
0
02
-
05
15
:
19
:
03.210
: INFO/System.out(
1035
): getView
8
null
type =
0
02
-
05
15
:
19
:
03.210
: INFO/System.out(
1035
): getView
9
null
type =
1
02
-
05
15
:
19
:
54.160
: INFO/System.out(
1035
): getView
10
null
type =
0
02
-
05
15
:
19
:
57.440
: INFO/System.out(
1035
): getView
11
android.widget.LinearLayout
@43744528
type =
0
02
-
05
15
:
20
:
01.310
: INFO/System.out(
1035
): getView
12
android.widget.LinearLayout
@43744eb0
type =
0
02
-
05
15
:
20
:
01.880
: INFO/System.out(
1035
): getView
13
android.widget.LinearLayout
@437456d8
type =
0
02
-
05
15
:
20
:
02.869
: INFO/System.out(
1035
): getView
14
null
type =
1
02
-
05
15
:
20
:
06.489
: INFO/System.out(
1035
): getView
15
android.widget.LinearLayout
@43745f00
type =
0
02
-
05
15
:
20
:
07.749
: INFO/System.out(
1035
): getView
16
android.widget.LinearLayout
@43747170
type =
0
02
-
05
15
:
20
:
10.250
: INFO/System.out(
1035
): getView
17
android.widget.LinearLayout
@43747998
type =
0
02
-
05
15
:
20
:
11.661
: INFO/System.out(
1035
): getView
18
android.widget.LinearLayout
@437481c0
type =
0
02
-
05
15
:
20
:
13.180
: INFO/System.out(
1035
): getView
19
android.widget.LinearLayout
@437468a0
type =
1
02
-
05
15
:
20
:
16.900
: INFO/System.out(
1035
): getView
20
android.widget.LinearLayout
@437489e8
type =
0
02
-
05
15
:
20
:
25.690
: INFO/System.out(
1035
): getView
21
android.widget.LinearLayout
@4374a8d8
type =
0
发表评论
-
Android NDK开发环境搭建_r8
2014-04-23 16:32 1038本文主内容: 1、 Android NDK 安装 2、 ... -
Android java.lang.VerifyError 异常解决办法
2014-04-01 13:58 4542通常这个异常的问题出现在jar包上 我的情况是 sdk ... -
Android —— 关于ADT 17的BuildConfig.DEBUG
2014-03-21 11:02 1113在日常开发中,我们使用android.util.Lo ... -
Android 让多个Fragment 切换时不重新实例化
2013-12-05 18:48 1419在项目中需要进行Fragment的切换,一直都是用repla ... -
android学习—— context 和 getApplicationContext()
2013-09-22 11:08 1894在android中常常会遇到与context有关的内容 浅 ... -
Android requestFeature() must be called before adding content
2013-07-08 17:18 2308E/AndroidRuntime( 408): androi ... -
Android onActivityResult和横屏
2013-06-20 16:48 1791做一个项目 页面强制横屏 两个activity需要交互 现 ... -
Android 无法加载fragment的问题
2013-06-17 14:42 2396在项目中继承了fragment 重写了构造函数 make ... -
android源码解读一(Context)
2013-04-27 19:16 1718Context类 Context是一个抽象类 publi ... -
Android 4.1 动态加载APK中的资源
2013-04-24 17:29 4148*** is not owned by the curr ... -
Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8
2013-04-15 15:45 1746过程中,增大对应AVD的 ... -
Android下面的MD5加密
2013-04-08 12:17 957网上关于android下MD5加密的资料很多,但是测试了下 ... -
Android 调用系统应用程序信息(Application Info)界面
2013-03-15 18:30 1233“Android系统设置->应用程序->管理应用 ... -
Android Dialog背景透明和黑暗度
2013-03-14 18:20 4033设置透明度,主要设置的是dialog自身的透明度 ... -
全局监听SCREEN_ON和SCREEN_OFF的替代方法--监听屏幕解锁事件
2013-03-08 12:17 3005在做一个程序的时候,需要时刻保持某一服务是启动的, ... -
Android 退出应用程序
2013-01-21 10:53 896android.os.Process.killProcess ... -
Android控件WebView(浏览器)常用功能(图片、缩放)例子介绍
2012-11-06 11:03 2500Android系统默认提供WebView控件(view)来在应 ... -
Android ProgressBar使用.9图在部分android手机上显示异常!
2012-11-05 10:18 2099写道 我使用的是progressbar.xml配置文 ... -
Android 同时使用多个library时的问题
2012-10-29 12:40 1550剧情是这样,我的app要使用两个library,如:Li ... -
android圆形进度条ProgressBar颜色设置
2012-07-29 15:14 1306总结的挺全面 http://aichixihongshi.i ...
相关推荐
ListView中getView重用好多次,有头像的布局
android listview getview多调用俩周期
不用adapter的notifyDataSetChanged()方式修改listview的item某个控件的设置,通过点击item中的textview使得该textview的值发生改变
遇到以下问题的都可以看看: 1、ListView item中加入checkbox后onListItemClick 事件无法触发。 原因:checkbox的优先级高于ListItem于是...解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置
android 的listview 内部item的布局包含checkbox控件时,会遇到常见的问题:1.checkbox无法选择;2.选择的checkbox后滑动listview内容时会checkbox选择的值会刷新成原来状态值(即没选中);3.选择listview所有checkbox...
主要介绍了Android ListView与getView调用卡顿问题解决办法的相关资料,这里提供实例及解决办法帮助大家解决这种问题,需要的朋友可以参考下
以前倒是没有注意listview的getView会重复执行多次,在测试的时候去断点跟踪,发现同一条数据不断的重复执行,下面与大家分享下正确的解决方法,希望对你有所帮助
Android-扩展BaseAdapter实现progressBar下载 ... * 9.getView()中判断convertView是否为空,防止过多填充布局导致OOM * 10.将ViewHolder对象存储在Item的tag * 11.创建点击事件和线程MyThread对象
在数据源适配器知识点上面,我们可以自己定义一个数据源适配器,要继承BaseAdapter,实现getView的方法 画廊控件:gallery imageswitch HorizontalScrollView,在一个屏幕上显示出超过屏幕尺寸的内容
* 思路简介:一般listview中涉及到图片加载的,为了提高用户体验,一般会使用一个软引用和存文件的方式来实现。不过还见过这样来实现的:即用一个HashMap来存放很小一部分的数据, * 当HashMap中的大小达到一定的...
Android ListView 下拉刷新 解决不停调用getview导致界面卡的问题
在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现ListView上面每个Item上的Button的事件有两种方法: 1.使用final变量扩展局部变量的生命周期范围主要代码(本文最后附全部代码): //注意...
我们在使用ListView时 用户时长会 连续滚动数屏,且滚动过程中会触发所有的getView的异步任务,getView线程加载图片则会 开很多条线程或者是线程池不够用 堵塞在 前面的getView任务中。直到 逐个执行完成 到达 当前...
在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布局和自定义adapter了,一般是继承于BaseAdapter,示例代码见下方。写ListView的点击...
android_ListView优化Adapter的作用就是ListView界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。想过没有? 在我们的列表有1000000项时会是什么样的?是不是会...
1.在adapter 的 getview方法里面启动加载图片的thread,如果listview在滑动则wait 2.监听listview滑动停止事件,获得listview显示的item的最上面和最下面的序号,并唤醒所有加载图片的thread,判断加载图片的序号...
|--android中anim文件特效 |--app信息menifest获取(如版本号) |--AsyncQueryHandler之异步查询Cursor处理 |--AutoCompleteTextView自动提示的用法 |--BitMap、Drawable、inputStream及byte[] 互转 |--...
对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView。在这种模式下,我们会在getView中,根据不同的数据源,让控件...