天天看点

自定义BaseAdapter定制ListView

废话不多说,今天以下图效果为例。理解还不够透彻,将我目前的理解都记下来,将来再补充完善。

左侧是一张图片,右侧第一行是名字,最后一行是年龄,中间内容是描述(最多4行)。

自定义BaseAdapter定制ListView

首先,新建一个类,包含list item里的内容作为属性。如此例,新建Animal类。

public class Animal {
	private  int imageId;   //图片id
	private String name;
	private String infor;
	private String age;
	public Animal(int imageId, String name, String infor, String age){
			super();
			this.imageId=imageId;
			this.name=name;		
			this.infor=infor;
			this.age=age;
		}
	protected  void setName(String name){
		this.name=name;
	}
	protected  String getName(){
		return name;
	}
	protected  void setInfor(String infro){
		this.infor=infor;
	}
	protected  String getInfor(){
		return infor;
	}
	protected  void setAge(String age){
		this.age=age;
	}
	protected  String getAge(){
		return age;
	}
	protected  void setImageId(int imageId){
		this.imageId=imageId;
	}
	protected  int getImageId(){
		return imageId;
	}
}
           

然后,就是自定义Adapter了,继承自 BaseAdapter。

import java.util.List;
import java.util.Map;
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.TextView;
public class AniAdapter extends BaseAdapter{
	private List<Animal> data;<span style="white-space:pre">	</span>//一个animal类的空list用来装载数据
	private LayoutInflater layoutInflater;<span style="white-space:pre">	</span>//用来装载布局的布局工厂
	private Context context;<span style="white-space:pre">	</span>//上下文
	private ViewHolder holder;<span style="white-space:pre">	</span>//自定义的一个处理类
	public AniAdapter(Context context, List<Animal> data){<span style="white-space:pre">	</span>//构造类,参数需要上下文和数据集
		this.context=context;
		this.data=data;
	}
	public Context getContext() {
		// TODO Auto-generated method stub
		return context;
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(int position, View convertView,ViewGroup parent) {<span style="white-space:pre">	</span>//getView指的是加载每一个listItem到一个空间中,convertView表示一个listitem的空间
		// TODO Auto-generated method stub
		holder= new ViewHolder();
		Animal animal=(Animal) getItem(position);
		if(convertView==null){
		convertView=layoutInflater.from(getContext()).inflate(R.layout.layout_listitem,null);<span style="white-space:pre">	</span>//设置listitem的布局
		holder.aniImage = (ImageView) convertView.findViewById(R.id.ani_image);		//给空间关联id
		holder.name = (TextView) convertView.findViewById(R.id.text_name);	
		holder.infor = (TextView) convertView.findViewById(R.id.text_infor);
		holder.age = (TextView) convertView.findViewById(R.id.text_age);
		convertView.setTag(holder);
		}
		else{
			holder=(ViewHolder) convertView.getTag();<span style="white-space:pre">	</span>
		}
		if(animal!=null){<span style="white-space:pre">	</span>
		holder.aniImage.setImageResource(animal.getImageId());
		holder.name.setText(animal.getName());
		holder.infor.setText(animal.getInfor());
		holder.age.setText(animal.getAge());
		}
		return convertView;<span style="white-space:pre">	</span>//返回一个装载了数据的listitem空间
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (data==null)?0:data.size();
	}
	public class ViewHolder{<span style="white-space:pre">	</span>//给listitem的各个部分定义空间
		ImageView aniImage;
		TextView name ;
		TextView infor;
		TextView age ;
	}
}
           

再然后,就是MainActivity了,使用AniAdapter将Animal数据以list item的布局样式显示在list view中。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
	private ListView listView;
	private List<Animal> listItems = new ArrayList<Animal>();;
	private AniAdapter aniAdapter;
	private Integer[] imgeIDs = { R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, };
	private String[] aniNames = { "阿大", "阿二", "啊三", "阿四", "阿五", "阿六" };
	private String[] aniInfor = {
			"只见那阿大是个精干枯瘦的老者,双手捧着一柄长剑,赫然便是那柄倚天宝剑。这人身材瘦长,满脸皱纹,愁眉苦脸,似乎刚才给人痛殴了一顿,要不然便是新死了妻子儿女,旁人只要瞧他脸上神情,几乎便要代他伤心落泪。",
			"那阿二同样的枯瘦,身材略矮,头顶心滑油油地,秃得不剩半根头发,两边太阳穴凹了进去,深陷半寸。",
			"那阿三却是精壮结实,虎虎有威,脸上、手上、项颈之中,凡是可见到肌肉处,尽皆盘根虬结,似乎周身都是精力,胀得要爆炸出来,他左颊上有颗黑痣,黑痣上生着一丛长毛。",
			"只见那阿四是个精干枯瘦的老者,双手捧着一柄长剑,赫然便是那柄倚天宝剑。这人身材瘦长,满脸皱纹,愁眉苦脸,似乎刚才给人痛殴了一顿,要不然便是新死了妻子儿女,旁人只要瞧他脸上神情,几乎便要代他伤心落泪。",
			"那阿五同样的枯瘦,身材略矮,头顶心滑油油地,秃得不剩半根头发,两边太阳穴凹了进去,深陷半寸。",
			"那阿六却是精壮结实,虎虎有威,脸上、手上、项颈之中,凡是可见到肌肉处,尽皆盘根虬结,似乎周身都是精力,胀得要爆炸出来,他左颊上有颗黑痣,黑痣上生着一丛长毛。" };
	private String[] aniAge = { "22", "33", "44", "55", "66", "77" };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);<span style="white-space:pre">	</span>//设置为activity不显示标题栏(今天发现,这行代码必须要在setContentView()之前。)
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.list_search);
		init();
		aniAdapter = new AniAdapter(this, listItems);
		listView.setAdapter(aniAdapter);
	}
	public void init() {
		for (int i = 0; i < 6; i++) {
			listItems.add(new Animal(imgeIDs[i], aniNames[i], aniInfor[i],aniAge[i]));
		}
	}
}
           

最后贴出mainActivity的xml【activity_main】

<?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" 
    android:background="@color/main_base_bg">
<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/main_blue"
        android:padding="5dp">  
        <ImageView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/ic_back"/>
           </LinearLayout>
<span style="white-space:pre">	</span><ListView
            android:id="@+id/list_search"
            android:layout_width="match_parent"
            android:layout_height="fill_parent" >
        </ListView>
</LinearLayout>

           
listitem的xml[【layout_listitem】
           
<?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_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">
        <ImageView 
            android:id="@+id/ani_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"/>
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical">
            <TextView
              android:id="@+id/text_name"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:singleLine="true"
              android:gravity="top"/>
            <TextView
              android:id="@+id/text_infor"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:singleLine="false"
              android:maxLines="4"/>
            <TextView
              android:id="@+id/text_age"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:singleLine="true"
              android:gravity="bottom"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>



           

继续阅读