![](https://i-blog.csdnimg.cn/blog_migrate/1ca6beb77d21ef765ed89d29dea20fce.png)
<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.test16.MainActivity" >
<ListView
android:id="@+id/listView "
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
package com.example.test16;
import java.util.zip.Inflater;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ListView listView;
private int[] imgResIds={
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e
};
private String[] names={
"name1","name2","name3","name4","name5"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgResIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
View view;
if(convertView==null){
// 解析布局文件
LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
view=inflater.inflate(R.layout.list_item, null);
holder=new ViewHolder(view);
view.setTag(holder);
}else{
view=convertView;
holder=(ViewHolder) view.getTag();
}
ImageView image=(ImageView) view.findViewById(R.id.im_img);
image.setImageResource(imgResIds[position]);
TextView tvName=(TextView) view.findViewById(R.id.tv_name);
tvName.setText(names[position]);
return view;
}
}
class ViewHolder{
private View view;
private ImageView image;
private TextView tvName;
public ViewHolder(View view) {
this.view = view;
}
public ImageView getImage() {
if(image==null){
//查找
image=(ImageView) findViewById(R.id.im_img);
}
return image;
}
public TextView getTvName() {
if(tvName==null){
//查找
tvName=(TextView) findViewById(R.id.tv_name);
}
return tvName;
}
}
}
<?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"
android:gravity="center_vertical">
<ImageView
android:id="@+id/im_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="20sp"
/>
</LinearLayout>