1.创建名为CustomList的Android工程
2.创建custom_list_cell.xml用于显示一个列表项的具体布局格式。
3.创建一个CustomListCellData的类用于对应一个列表项的数据。
4.创建一个CustomListAdapter适配器继承自BaseAdapter用于适配数据。
5.在res/drawable下放置自己要使用的图片资源
6.在MainActivity中把适配器和列表相关联。
目录结构如下所示:
代码如下:
activity_main.xml
<LinearLayout 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"
>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
custom_list_cell.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="horizontal" >
<ImageView android:id="@+id/iconId"
android:layout_width="120dp"
android:layout_height="120dp"/>
<LinearLayout
android:id="@+id/ll"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
package edu.yg.customlist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
private Context context;
public CustomListAdapter(Context context) {
this.context=context;
}
public Context getContext() {
return context;
}
CustomListCellData[] data={new CustomListCellData("img1",R.drawable.img1,"男"),new CustomListCellData("img2", R.drawable.img2, "女")};
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public CustomListCellData getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll=null;
if(convertView!=null){
ll=(LinearLayout) convertView;
}
else{
ll=(LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.custom_list_cell, null);
}
CustomListCellData dt=getItem(position);
ImageView icon=(ImageView) ll.findViewById(R.id.iconId);
TextView name=(TextView) ll.findViewById(R.id.name);
TextView sex=(TextView) ll.findViewById(R.id.sex);
icon.setImageResource(dt.getIconid());
name.setText(dt.getName());
sex.setText(dt.getSex());
return ll;
}
}
package edu.yg.customlist;
public class CustomListCellData {
public CustomListCellData(String name, int iconid, String sex) {
super();
this.name = name;
this.iconid = iconid;
this.sex = sex;
}
private String name;
private int iconid;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIconid() {
return iconid;
}
public void setIconid(int iconid) {
this.iconid = iconid;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package edu.yg.customlist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView lv=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listview);
lv.setAdapter(new CustomListAdapter(this));
}
}
运行结果如下: