1. 自定义ListView,效果图:
2. 代码实现:
(1)res/layout/main.xml实现:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <com.focus.fishme.SelfListView
- android:id="@+id/ListView"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
(2)ListView的Item布局实现:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:orientation = "vertical"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- >
- <TextView
- android:id = "@+id/TextViewOne"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_marginLeft = "10dip"
- android:textSize = "24sp"
- />
- <TextView
- android:id = "@+id/TextViewTwo"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:layout_marginLeft = "10dip"
- />
- <View
- android:layout_height = "1dip"
- android:layout_width = "fill_parent"
- android:background = "#FF0000"
- />
- </LinearLayout>
(3)主Activity实现:
- package com.focus.fishme;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- public class SelfListViewActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /**
- * 设置主布局。
- */
- setContentView(R.layout.main);
- /**
- * ListView数据集。
- */
- List<Map<String , Object>> mSelfData = new ArrayList<Map<String,Object>>();
- /**
- * 获取ListView组件。
- */
- SelfListView mSelfListView = (SelfListView) findViewById(R.id.ListView);
- /**
- * 生成数据。
- */
- for (int i = 0; i < 10; i++) {
- HashMap<String, Object> mMap = new HashMap<String, Object>();
- mMap.put("key_name", "name" + i);
- mMap.put("value_name", "value" + i);
- mSelfData.add(mMap);
- }
- /**
- * 自定义Adapter。
- */
- final SelfAdapter mSelfAdapter = new SelfAdapter(this, mSelfData,
- R.layout.item, new String[] { "key_name", "value_name" }, new int[] { R.id.TextViewOne, R.id.TextViewTwo });
- /**
- * 向ListView设置Adapter。
- */
- mSelfListView.setSelfAdapter(mSelfAdapter);
- }
- }
(4)ListView所用Adapter实现:
- package com.focus.fishme;
- 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.TextView;
- public class SelfAdapter extends BaseAdapter {
- private LayoutInflater mLayoutInflater;
- private int mResource;
- private List<? extends Map<String, ?>> mSelfData;
- private String[] from;
- private int[] to;
- public SelfAdapter(Context context, List<? extends Map<String, ?>> data, int resouce, String[] from, int[] to) {
- this.mSelfData = data;
- this.mResource = resouce;
- this.mSelfData = data;
- this.from = from;
- this.to = to;
- this.mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public int getCount() {
- return mSelfData.size();
- }
- public Object getItem(int position) {
- return mSelfData.get(position);
- }
- public String get(int position, Object key) {
- Map<String, ?> map = (Map<String, ?>) getItem(position);
- return map.get(key).toString();
- }
- public long getItemId(int position) {
- return position;
- }
- /**
- * 生成ListView的Item布局。
- */
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = mLayoutInflater.inflate(mResource, null);
- }
- Map<String, ?> item = mSelfData.get(position);
- int count = to.length;
- for (int i = 0; i < count; i++) {
- View v = convertView.findViewById(to[i]);
- bindView(v, item, from[i]);
- }
- convertView.setTag(position);
- return convertView;
- }
- private void bindView(View view, Map<String, ?> item, String from) {
- Object data = item.get(from);
- if (view instanceof TextView) {
- ((TextView) view).setText(data == null ? "" : data.toString());
- }
- }
- }
(5)自定义ListView实现:
- package com.focus.fishme;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- public class SelfListView extends LinearLayout {
- private BaseAdapter mSelfAdapter;
- public SelfListView(Context context) {
- super(context);
- }
- public SelfListView(Context context, AttributeSet attributeSet) {
- super(context, attributeSet);
- }
- /**
- * 删除ListView中上一次渲染的View,并添加新View。
- */
- private void buildList() {
- if (mSelfAdapter == null) {
- }
- if (getChildCount() > 0) {
- removeAllViews();
- }
- int count = mSelfAdapter.getCount();
- for(int i = 0 ; i < count ; i++) {
- View view = mSelfAdapter.getView(i, null, null);
- if (view != null) {
- addView(view, i);
- }
- }
- }
- public BaseAdapter getSelfAdapter() {
- return mSelfAdapter;
- }
- /**
- * 设置Adapter。
- *
- * @param selfAdapter
- */
- public void setSelfAdapter(BaseAdapter selfAdapter) {
- this.mSelfAdapter = selfAdapter;
- buildList();
- }
- }