天天看點

自定義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>



           

繼續閱讀