Android中ListView分類

1. 引言 java

 

    在Android開發過程當中每每有這樣的需求,將ListView中的內容按年,月,日進行分類顯示,要實現這樣的效果咱們可能有不少種方法, android

 

    如:多ListView拼合,自定義ListView組件等,下面介紹一種比較簡單,並且實現結構清晰的實現方式,效果圖及實現以下。 app

 

2. 效果圖 ide

    ListView分類

3. 功能實現 佈局

 (1) 主佈局(main.xml)實現: this


<?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"
    >
    <ListView
		android:id = "@+id/categoryList"
		android:layout_width = "fill_parent" 
		android:layout_height = "fill_parent"
		/>
</LinearLayout>&nbsp;
  (2) 主Activity實現:



package com.flora;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewCategoryActivity extends Activity {
	
	private String [] mContacts = {"馬英才", "張三", "李四"};
	private String [] mMusic = {"素顔", "廬州月", "半城煙沙"};
	private String [] mEBook = {"拆掉思惟裏的牆", "淡定力", "人脈決定命脈"};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mCategoryAdapter.addCategory("人名", new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mContacts));
		
		mCategoryAdapter.addCategory("音樂",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMusic));
		
		mCategoryAdapter.addCategory("書籍",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mEBook));
		
		ListView categoryList = (ListView) findViewById(R.id.categoryList);
		
		categoryList.setAdapter(mCategoryAdapter);
    }
    
    private CategoryAdapter mCategoryAdapter = new CategoryAdapter() {
		@Override
		protected View getTitleView(String title, int index, View convertView, ViewGroup parent) {
			TextView titleView;
			
			if (convertView == null) {
				titleView = (TextView)getLayoutInflater().inflate(R.layout.title, null);
			} else {
				titleView = (TextView)convertView;
			}
			
			titleView.setText(title);
			
			return titleView;
		}
	};
	
}
    (3) Adapter實現:



package com.flora;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
public abstract class CategoryAdapter extends BaseAdapter {
	private List<Category> categories = new ArrayList<Category>();
	
	public void addCategory(String title, Adapter adapter) {
		categories.add(new Category(title, adapter));
	}
	
	@Override
	public int getCount() {
		int total = 0;
		
		for (Category category : categories) {
			total += category.getAdapter().getCount() + 1;
		}
		
		return total;
	}
	@Override
	public Object getItem(int position) {
		for (Category category : categories) {
			if (position == 0) {
				return category;
			}
			
			int size = category.getAdapter().getCount() + 1;
			if (position < size) {
				return category.getAdapter().getItem(position-1);
			}
			position -= size;
		}
		
		return null;
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public int getViewTypeCount() {
		int total = 1;
		
		for (Category category : categories) {
			total += category.getAdapter().getViewTypeCount();
		}
		
		return total;
	}
	public int getItemViewType(int position) {
		int typeOffset = 1;
		
		for (Category category : categories) {
			if (position == 0) {
				return 0;
			}
			
			int size = category.getAdapter().getCount() + 1;
			if (position < size) {
				return typeOffset + category.getAdapter().getItemViewType(position - 1);
			}
			position -= size;
			
			typeOffset += category.getAdapter().getViewTypeCount();
		}
		
		return -1;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int categoryIndex = 0;
		
		for (Category category : categories) {
			if (position == 0) {
				return getTitleView(category.getTitle(), categoryIndex,convertView, parent);
			}
			int size = category.getAdapter().getCount()+1;
			if (position < size) {
				return category.getAdapter().getView(position - 1, convertView, parent);
			}
			position -= size;
			
			categoryIndex++;
		}
		
		return null;
	}
	
	protected abstract View getTitleView(String caption,int index,View convertView,ViewGroup parent);
	
}&nbsp;

   (4) 分類ValueBean實現: spa

package com.flora;
import android.widget.Adapter;
public class Category {
	private String mTitle;
	
	private Adapter mAdapter;
	public Category(String title, Adapter adapter) {
		mTitle = title;
		mAdapter = adapter;
	}
	
	public void setTile(String title) {
		mTitle = title;
	}
	
	public String getTitle() {
		return mTitle;
	}
	
	public void setAdapter(Adapter adapter) {
		mAdapter = adapter;
	}
	
	public Adapter getAdapter() {
		return mAdapter;
	}
	
}&nbsp;
  (5) 分類Title實現:

<?xml version="1.0" encoding="utf-8"?>
<TextView
	xmlns:android = "http://schemas.android.com/apk/res/android"
	android:layout_width = "match_parent"
	android:layout_height = "match_parent"
	android:minHeight = "30dip"
	android:gravity = "center_vertical"
	android:paddingLeft = "10dip"
	android:background = "@color/title_background_color"
	/>&nbsp;
相關文章
相關標籤/搜索