在Android中实现Listview对新人来说比较难以理解,本人看了若干文章后觉得可以使用以下思路来让新人更好理解(同时也做好记录,免得自己以后忘记)。
可参考博客:http://cinderella7.blog.51cto.com/7607653/1281696 (这里用MVC的思想去理解Listview,个人认为还是不错的)
http://blog.csdn.net/jueblog/article/details/11857281 (一个完整的实现)
--------------------------------------------------------------------------分割线-----------------------------------------------------------------
一、理顺整个思路。
自定义Listview中包含三个重要概念,这里用MVC的形式去理解:
class="p0"> ListView相当于MVC框架中的V(视图)
Adapter相当于MVC框架中的C(控制器)
数据源相当于MVC框架中的M(模型)
二、一步一步实现
1、先自定义listview中每个Item子项的布局
listitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="100dp" android:id="@+id/imageView"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/textView"/> </LinearLayout>
2、定义主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView"/> </LinearLayout>
3、然后,定义数据源、视图、适配器
private List<HashMap<String,Object>> mData; //M private ListView listView; //V private MyAdapter myAdapter; //C
4、获取数据源
private List<HashMap<String,Object>> getData(){ ArrayList<HashMap<String,Object>> arrayList = new ArrayList<HashMap<String, Object>>(); HashMap<String,Object> hashMap = null; for(int i=0;i<10;i++){ hashMap = new HashMap<String, Object>(); hashMap.put("index",Integer.toString(i+1)); hashMap.put("img",R.drawable.my); arrayList.add(hashMap); //添加到数据源中 } return arrayList; }
5、自定义适配器,添加数据源到适配器中
//创建自定义adapter private class MyAdapter extends BaseAdapter{ private LayoutInflater mlayoutInflater;
public MyAdapter(Context context) { mlayoutInflater = LayoutInflater.from(context); //动态布局映射 } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = mlayoutInflater.inflate(R.layout.listitem, null); // 根据布局文件实例化view TextView tv = (TextView)convertView.findViewById(R.id.textView); tv.setText(mData.get(position).get("index").toString()); ImageView img = (ImageView)convertView.findViewById(R.id.imageView); img.setBackgroundResource((Integer)mData.get(position).get("img")); return convertView; } }
6、在onCreate函数中实施
listView = (ListView)findViewById(R.id.listView); //---- mData = getData(); myAdapter = new MyAdapter(this); listView.setAdapter(myAdapter);
三、总结
整个实现流程都严格按照MVC的模式去做,首先是自定义自己的元素布局,然后获取数据源,然后重写适配器,加载数据源到适配器中,通过listview显示适配器内容。
以下附上Java代码完整版:
package com.example.ListViewTest; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class MyActivity extends Activity { /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)findViewById(R.id.listView); //---- mData = getData(); myAdapter = new MyAdapter(this); listView.setAdapter(myAdapter); } private List<HashMap<String,Object>> mData; //M private ListView listView; //V private MyAdapter myAdapter; //C private List<HashMap<String,Object>> getData(){ ArrayList<HashMap<String,Object>> arrayList = new ArrayList<HashMap<String, Object>>(); HashMap<String,Object> hashMap = null; for(int i=0;i<10;i++){ hashMap = new HashMap<String, Object>(); hashMap.put("index",Integer.toString(i+1)); hashMap.put("img",R.drawable.my); arrayList.add(hashMap); //添加到数据源中 } return arrayList; } //创建自定义adapter private class MyAdapter extends BaseAdapter{ private LayoutInflater mlayoutInflater; public MyAdapter(Context context) { mlayoutInflater = LayoutInflater.from(context); //动态布局映射 } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = mlayoutInflater.inflate(R.layout.listitem, null); // 根据布局文件实例化view TextView tv = (TextView)convertView.findViewById(R.id.textView); tv.setText(mData.get(position).get("index").toString()); ImageView img = (ImageView)convertView.findViewById(R.id.imageView); img.setBackgroundResource((Integer)mData.get(position).get("img")); return convertView; } } }