ListView优化
Adapter的作用就是ListView界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。想过没有? 在我们的列表有1000000项时会是什么样的?是不是会占用极大的系统资源?
L
ist.xml:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
LinearLayout
android:id
=
"@+id/linearLayout1"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
ImageView
android:layout_height
=
"wrap_content"
android:layout_width
=
"wrap_content"
android:src
=
"@drawable/icon"
android:id
=
"@+id/imageView1"
android:layout_margin
=
"5px"
>
</
ImageView
>
<
TextView
android:text
=
"TextView"
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center"
android:textSize
=
"20px"
>
</
TextView
>
</
LinearLayout
>
</
LinearLayout
>
@
Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
//
TODO
Auto-generated method stub
//找到R.layout.list布局文件
layout_Adapter = (LinearLayout)
this
.layoutInflater.inflate(R.layout.list,
null
);
imageView = (ImageView)layout_Adapter.findViewById(R.id.imageView1);
textView = (TextView)layout_Adapter.findViewById(R.id.textView1);
imageView.setImageResource(R.drawable.icon);
textView.setText(title[position]);
return
layout_Adapter;
}
2.利用convertview回收视图,效率提高200%。
@
Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
//
TODO
Auto-generated method stub
if
(convertView ==
null
){
convertView = (LinearLayout)
this
.layoutInflater.inflate(R.layout.list,
null
);
}
imageView = (ImageView)convertView.findViewById(R.id.imageView1);
textView
= (TextView)convertView.findViewById(R.id.textView1);
imageView.setImageResource(R.drawable.icon);
textView
.setText(title[position]);
return
convertView;
}
上面的代码是不是好了很多?系统将会减少创建很多View。性能得到了很大的提升
。
3.利用viewholder模式,效率在提高50% |
public
View getView(
int
position, View convertView, ViewGroup parent) {
//
TODO
Auto-generated method stub
ViewHolder holder;
if
(convertView ==
null
){
convertView = (LinearLayout)
this
.
layoutInflater
.inflate(R.layout.
list
,
null
);
holder =
new
ViewHolder();
holder.
tv
= (TextView)convertView.findViewById(R.id.
textView1
);
holder.
im
= (ImageView)convertView.findViewById(R.id.
imageView1
);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
Log.
d
(
"list"
,
"convertView...."
);
}
holder.
im
.setImageResource(R.drawable.
icon
);
holder.
tv
.setText(
title
[position]);
return
convertView;
}
static
class
ViewHolder{
TextView
tv
;
ImageView
im
;
}
这种方法为View设置了Tag属性,通过setTag()方法将View的ImageView、TextView组件的对象添加到Tag 中,当回收利用时直接用getTag()方法取出,不用重新获取组件。
ListView优化三原则 :
1.复用convertView
在getItemView中,判断convertView是否为空,如果不为空,可复用。
2.异步加载图片
item中如果包含有webimage,那么最好异步加载
3.快速滑动时不显示图片
当快速滑动列表时(SCROLL_STATE_FLING),item中的图片或获取需要消耗资源的view,可以不显示出来;而处于其他两种状态(SCROLL_STATE_IDLE 和SCROLL_STATE_TOUCH_SCROLL),则将那些view显示出来
public void
setVisibility
(int visibility)
visibility
|
One of
VISIBLE
,
INVISIBLE
, or
GONE
|
但是在调用此方法的时候
image.setVisibility(visibility)
其中visibility是int型的参数。
对应上面:
VISIBLE
=0x00000000;
INVISIBLE
=0x00000004;
GONE
=0x00000008。
即:
image.setVisibility(0x00000000) / image.setVisibility(View.
VISIBLE
); 表示显示;
image.setVisibility(0x00000004) / image.setVisibility(View.
INVISIBLE
);表示隐藏;
image.setVisibility(0x00000008) / image.setVisibility(View.
GONE
);表示view不存在