最近做一个项目需要用到ListView加载上千条数据,当然结果是很卡! 怎么解决呢?google一下?
找到了一个开源项目http://commonsware.com/cwac
但是怎么使用呢?网上找了一堆demo没说清楚!经过本人反复实验 终于弄懂了!如果有不合理或者更好的建议请写下来大家一起共同解决!
好了废话不多说 开始!
demo和jar地址http://download.csdn.net/detail/zabio/6979685
1.准备好项目所需要的jar包 CWAC-AdapterWrapper.jar 和CWAC-EndlessAdapter.jar
2.导入项目这个步骤就不再说了
3.看代码 简单粗糙的写了下
package com.hy.lazylistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.commonsware.cwac.endless.EndlessAdapter;
public class MainActivity extends Activity {
private ListView lv;
private List list = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//初始化第一次显示的数据
for (int j = 0; j < 100; j++) {
list.add(j + "startData");
}
lv.setAdapter(new MyAdapter());
}
//这个是我们需要的可以无限往下滑进行列表数据加载的控件
class MyAdapter extends EndlessAdapter {
//加载时的动画
private RotateAnimation rotate = null;
private View pendingView = null;
//构造的时候 要super一下
MyAdapter() {
//里面这个很熟悉 常用的baseAdapter
super(new MyBaseAdapter());
rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
//显示加载时的view
@Override
protected View getPendingView(ViewGroup parent) {
View row = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView = row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
startProgressAnimation();
return (row);
}
//设置加载的时间
@Override
protected boolean cacheInBackground() {
SystemClock.sleep(10000);
//设置限制总数据大小
return (getWrappedAdapter().getCount() < 1000);
}
//往list里面添加数据
@Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount() < 1000) {
for (int i = 0; i <100; i++) {
list.add(i+"addData");
}
}
}
void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}
}
//这是常见的控件不解释了
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class Holder {
TextView t, t2;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Holder h;
if (view == null) {
h=new Holder();
view = View.inflate(MainActivity.this, R.layout.item, null);
h.t = (TextView) view.findViewById(R.id.tv);
h.t2 = (TextView) view.findViewById(R.id.tv2);
view.setTag(h);
} else {
h = (Holder) view.getTag();
}
h.t.setText("position" + position);
h.t2.setText(list.get(position).toString());
return view ;
}
}
}
--------------------资源文件--------------------
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true" />
</RelativeLayout>
item------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0f0"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00f"/>
</LinearLayout>
row-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/throbber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_popup_sync_1"
android:visibility="gone" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
/>
</FrameLayout>