Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> ListView自定義

ListView自定義

編輯:關於Android編程

ListView網上資料很多,我所知擴展性最好、最通用的代碼做個備忘。

總體上來講:ListView+BaseAdapter+xml文件實現:

話不多說,先貼個圖,預期效果如下:

\

首先:這樣一個ListView的item包含:圖片、文字,而且布局也比較復雜。首選自定義這樣一個模版的xml文件。

 

\ \ \ \ \

 

第一步、主方法lin.xml,加一個listveiw布局

 



    

 

第二步:自定義item.xml(寫Demo有些懶,稍微簡化了幾個textview)

 




    

    

    

    

    

    

    

第二步、寫一個Databean文件,用來包含所有item的內容集

 

 

import java.io.Serializable;

public class DataBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int imgId;
	String title;
	String address;
	public int getImgId() {
		return imgId;
	}
	public void setImgId(int imgId) {
		this.imgId = imgId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}


 

第三步、寫一個adapter繼承自baseAdapter

 

 

package com.mylistview.listviewdm;

import java.util.ArrayList;

import com.mylistview.bean.DataBean;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

	Context mContext;
	ArrayList mData;

	public ListViewAdapter(Context mContext, ArrayList mData) {
		super();
		this.mContext = mContext;
		this.mData = mData;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item, null);
			holder.img_1 = (ImageView) convertView.findViewById(R.id.img_1);
			holder.title_1 = (TextView) convertView.findViewById(R.id.title_1);
			holder.address = (TextView) convertView.findViewById(R.id.address);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.img_1.setBackgroundResource(mData.get(position).getImgId());
		holder.title_1.setText(mData.get(position).getTitle());
		holder.address.setText(mData.get(position).getAddress());
		return convertView;
	}

	class ViewHolder {
		ImageView img_1;
		TextView title_1;
		TextView address;
	}

}


 

第五步、擬數據到主方法,設置listveiw適配器

 

package com.mylistview.listviewdm;

import java.util.ArrayList;

import com.mylistview.bean.DataBean;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;


public class MainActivity extends Activity {

	private ArrayList mData;
	private ListView list_v;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lin);
        initView();
        initData();
    }
	private void initView() {
		// TODO Auto-generated method stub
		list_v = (ListView)findViewById(R.id.lst_v);
		
	}
	private void initData() {
		// TODO Auto-generated method stub
		mData = new ArrayList();
		setData();
		list_v.setAdapter(new ListViewAdapter(this, mData));
	}
	private void setData() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 4; i++) {
			DataBean mBean2 = new DataBean();
			mBean2.setImgId(R.drawable.img_1);
			mBean2.setTitle("豫蜀房河南燴面");
			mBean2.setAddress("快餐 五棵松");
			mData.add(mBean2);
			
			DataBean mBean = new DataBean();
			mBean.setImgId(R.drawable.img_5);
			mBean.setTitle("三樣菜(阜成路店)");
			mBean.setAddress("川菜 四季青");
			mData.add(mBean);
			
			DataBean mBean3 = new DataBean();
			mBean3.setImgId(R.drawable.img_6);
			mBean3.setTitle("郁陵島炭火烤肉");
			mBean3.setAddress("韓國菜 航天橋");
			mData.add(mBean3);
		}
	}
}
最後貼一張實現的效果圖 \

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved