天天看點

Android之通過BaseAdapter自定義擴充卡的使用

通過BaseAdapter建立自定義擴充卡。在所有的擴充卡中,通過BaseAdapter定義的擴充卡非常好用,可以自定義ListView每行布局的樣式,使用非常的廣泛,是開發過程中必不可少的。

下面看一個效果圖:

Android之通過BaseAdapter自定義擴充卡的使用

接下來一起來實作聊天清單:

1.主布局代碼:

Android之通過BaseAdapter自定義擴充卡的使用
<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"
    tools:context="com.example.baseadapterdemo.MainActivity" >
    
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fc424c"
        android:padding="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="清單"
            android:textColor="#fff"
            android:textSize="25sp" />

    </RelativeLayout>
    
    <ListView 
        android:id="@+id/main_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/relativeLayout"
        android:divider="#f00"
        ></ListView>

</RelativeLayout>
           

2.建立一個xml布局,是listView每行的布局:

Android之通過BaseAdapter自定義擴充卡的使用
<?xml version="1.0" encoding="utf-8"?>
<!--行布局檔案-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:padding="4dp"
    android:orientation="vertical" >

    <ImageView
        android:padding="3dp"
        android:id="@+id/item_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/item_time"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:text="時間" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/item_time"
        android:layout_toRightOf="@id/item_img"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textSize="20sp"
            android:text="張三" />

        <TextView
            android:textSize="14sp"
            android:id="@+id/item_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="在做什麼呢?" />
    </LinearLayout>

</RelativeLayout>
           

3.建立一個實體類,用于封裝資料: 每行布局中包括一個圖檔、姓名、聊天内容、時間,是以要将這幾個資料進行封裝

package com.example.vo;
/**
 * 實體類
 * 将行布局的内容進行封裝
 * @author Dell
 *
 */
public class ListVO {
	//圖檔
	private int mImg;
	//姓名
	private String mName;
	//内容
	private String mContent;
	//時間
	private String mTime;

	//無參
	public ListVO() {
		super();
	}

	//有參
	public ListVO(int mImg, String mName, String mContent, String mTime) {
		super();
		this.mImg = mImg;
		this.mName = mName;
		this.mContent = mContent;
		this.mTime = mTime;
	}

	//setter,getter
	public int getmImg() {
		return mImg;
	}

	public void setmImg(int mImg) {
		this.mImg = mImg;
	}

	public String getmName() {
		return mName;
	}

	public void setmName(String mName) {
		this.mName = mName;
	}

	public String getmContent() {
		return mContent;
	}

	public void setmContent(String mContent) {
		this.mContent = mContent;
	}

	public String getmTime() {
		return mTime;
	}

	public void setmTime(String mTime) {
		this.mTime = mTime;
	}

}
           

4.建立自定義擴充卡類,繼承BaseAdapter: a.定義2個參數(context,和List<實體類>),通過構造方法進行指派. b.重寫4個方法。 ①getCount()方法,傳回動态數組中有多少資料。 ②getView()方法,,通過LayoutInflater對象綁定行布局檔案,建立出一個View對象;通過View對象findViewByID()獲得行布局控件,設定文本與圖檔; 傳回View對象。

package com.example.adapter;
/**
 * 自定義擴充卡。
 */
import java.util.List;

import com.example.baseadapterdemo.R;
import com.example.vo.ListVO;

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 MyBaseAdapter extends BaseAdapter{
	//定義兩個參數,一個是Context對象,一個是ListVO實體類型動态數組
	private Context mContext;
	private List<ListVO> mList;
	

	//有參構造
	public MyBaseAdapter(Context mContext, List<ListVO> mList) {
		super();
		this.mContext = mContext;
		this.mList = mList;
	}

	//總行數
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	//布局
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.item_main, null);
		}else {
			view = convertView;
		}
		//綁定布局
		ImageView img = (ImageView) view.findViewById(R.id.item_img);
		TextView name = (TextView) view.findViewById(R.id.item_name);
		TextView content = (TextView) view.findViewById(R.id.item_content);
		TextView time = (TextView) view.findViewById(R.id.item_time);
		//設定内容
		img.setImageResource(mList.get(position).getmImg());
		name.setText(mList.get(position).getmName());
		content.setText(mList.get(position).getmContent());
		time.setText(mList.get(position).getmTime());
		return view;
	}


}
           

5.主類  a.聲明擴充卡控件并綁定ID; b.聲明自定義擴充卡并執行個體化; c.添加資料; d.綁定擴充卡。

package com.example.baseadapterdemo;
import java.util.ArrayList;
import java.util.List;

import com.example.adapter.MyBaseAdapter;
import com.example.vo.ListVO;

/**
 * 主類
 */
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;

public class MainActivity extends Activity {
	//擴充卡控件ListView
	private ListView mListView;
	//擴充卡
	private MyBaseAdapter mAdapter;
	//動态數組
	private List<ListVO> mList = new ArrayList<ListVO>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		bindID();
		//添加資料
		addData();
		//執行個體化擴充卡
		mAdapter = new MyBaseAdapter(MainActivity.this, mList);
		//綁定擴充卡
		mListView.setAdapter(mAdapter);
	}

	private void addData() {
		//建立對象
		ListVO v1 = new ListVO(R.drawable.a1, "張三", "最近在做什麼事情呢", "11:11");
		ListVO v2 = new ListVO(R.drawable.a2, "李四", "王五說她不愛我了", "12:21");
		ListVO v3 = new ListVO(R.drawable.a3, "王五", "我抛棄了他", "13:13");
		ListVO v4 = new ListVO(R.drawable.a4, "趙六", "哈哈哈", "14:41");
		ListVO v5 = new ListVO(R.drawable.a5, "小明", "擴充卡好簡單", "16:06");
		ListVO v6 = new ListVO(R.drawable.a6, "小剛", "6666666666", "18:11");
		ListVO v7 = new ListVO(R.drawable.a7, "李華", "英語作文被我包了", "18:51");
		//添加到數組
		mList.add(v1);
		mList.add(v2);
		mList.add(v3);
		mList.add(v4);
		mList.add(v5);
		mList.add(v6);
		mList.add(v7);
		//置為空
		v1 = null;
		v2 = null;
		v3 = null;
		v4 = null;
		v5 = null;
		v6 = null;
		v7 = null;
	}

	//綁定id
	private void bindID() {
		mListView = (ListView) findViewById(R.id.main_listView);
	}
}
           

這樣就完成了。

檔案打包:http://download.csdn.net/detail/qq_26239671/9715728

繼續閱讀