您是否使用过LazyAdapter。
尝试这个...
演示.java:
public class Demo extends Activity {
LazyAdapter adapter;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
lv = (ListView) findViewById(R.id.list);
ArrayList<String> songsList = new ArrayList<String>();
songsList.add("Song 1");
songsList.add("Song 2");
songsList.add("Song 3");
adapter=new LazyAdapter(this, songsList);
lv.setAdapter(adapter);
}
public void onListItemClick(ListView l, View v, int position, long id) {
//Do Something here
}
}
demo_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:textSize="20sp"
android:dividerHeight="1dp"/>
</RelativeLayout>
LazyAdapter.java:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> data;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity a, ArrayList<String> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.lazyadapter_layout, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
String song;
song = data.get(position);
// Setting all values in listview
title.setText(song);
return vi;
}
}
惰性适配器布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="480dp"
android:background="#404040"
android:gravity="center"
android:text="ViewSwitcher"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</LinearLayout>
希望这对你有帮助!!!
有关更多详细信息,请参阅本教程。