首页 > 代码库 > 自定义ListView

自定义ListView

每个列表项的XML布局文件:item.xml

<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
</LinearLayout>
<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text2"/>

</LinearLayout>

主页面布局文件:activity_main.xml

<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="com.example.listviewdemo.MainActivity" >

<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ListView" ></ListView>

</RelativeLayout>

主activity:

public class MainActivity extends Activity {
ListView listView=null;
List<Map<String, Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.ListView);
list=getdate();

listView.setAdapter(new Myadapter(this,list));
}
public List<Map<String, Object>> getdate() {
// TODO Auto-generated method stub
List<Map<String, Object>> listitem=new ArrayList<Map<String,Object>>();
Map<String, Object> map=new HashMap<String, Object>();
for (int i = 0; i < 22; i++) {
map.put("image", R.drawable.ic_launcher);
map.put("text1", "这是第一个标题");
map.put("text2", "这是第一个内容");
listitem.add(map);
}
return listitem;
}



}

适配器:Myadapter

public class Myadapter extends BaseAdapter {
Context mContext;
List<Map<String, Object>> list;
LayoutInflater mInflater;

public Myadapter(Context context, List<Map<String, Object>> list) {
// TODO Auto-generated constructor stub
this.mContext=context;
this.list=list;
this.mInflater=LayoutInflater.from(context);
}

private class holder {
// TODO Auto-generated method stub
TextView text1;
TextView text2;
ImageView image;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
holder mholder=null;
if (arg1==null) {
mholder=new holder();
arg1=mInflater.inflate(R.layout.item, null);
mholder.image=(ImageView) arg1.findViewById(R.id.image);
mholder.text1=(TextView) arg1.findViewById(R.id.text1);
mholder.text2=(TextView) arg1.findViewById(R.id.text2);
arg1.setTag(mholder);
}
else {
mholder=(holder)arg1.getTag();
}

mholder.image.setBackgroundResource((Integer) list.get(arg0).get("image"));
mholder.text1.setText((String) list.get(arg0).get("text1"));
mholder.text2.setText((String) list.get(arg0).get("text2"));

return arg1;
}

}