Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 簡單實現一個android listview分類!

簡單實現一個android listview分類!

編輯:關於Android編程

最近在做一個關於招聘的APP,裡面有選擇城市一項,是用listview分類,有的人說兩個listview嵌套,但是感覺太麻煩了,比較listview底層太復雜,有的人用expandablelistview,但是感覺效果一樣不好,所以寫了一個比較簡單的,希望對大家有幫助!

1.首先看MainActivity類!

其實跟普通的代碼一樣,只是多了一個MyBean類!

package com.zhaoqingyuan.zhaoqingyuan;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView listView;
	private MyAdapter myAdapter;
	private Listlist=new ArrayList();//裝數據的
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView=(ListView) findViewById(R.id.listView1);
		
		//裝數據了
		//當前位置
		MyBean myBean=new MyBean("當前位置", "北京");
		list.add(myBean);
		//熱門城市
		String[] hotCity={"全國","北京","上海","武漢","廣州","天津","太原","南京","深圳","重慶","昆明","拉薩","石家莊","哈爾濱","貴陽"};
		for (int i = 0; i < hotCity.length; i++) {
			MyBean myBean2=new MyBean("熱門城市", hotCity[i]);
			list.add(myBean2);
		}
		//熱門城市
		
		String[] province={"河北省","河南省","雲南省","遼寧省","黑龍江省","湖南省","安徽省","山東省","江蘇省","浙江省",
				" 江西省","湖北省","甘肅省","山西省","內蒙古","陝西省","吉林省","福建省","貴州省","廣東省",
				"青海省","西藏","四川省","寧夏回族","海南省","台灣省"};
		for (int i = 0; i < province.length; i++) {
			MyBean myBean3=new MyBean("按省份選擇城市", province[i]);
			list.add(myBean3);
		}
		
		myAdapter=new MyAdapter(MainActivity.this, list);
		listView.setAdapter(myAdapter);
	}
}
2.這個是Bean類,裡面有get和set方法!

package com.zhaoqingyuan.zhaoqingyuan;

public class MyBean {

	private String series;
	private String city;//可以替代省份名稱
	public MyBean(String series,String city) {
		setSeries(series);
		setCity(city);
	}
	public String getSeries() {
		return series;
	}
	public void setSeries(String series) {
		this.series = series;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	
}
3.接下來就是我們在熟悉不過的適配器了!就是多了兩行代碼而已!

package com.zhaoqingyuan.zhaoqingyuan;

import java.util.ArrayList;
import java.util.List;

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 MyAdapter extends BaseAdapter {
	private Context context;
	private Listlist=new ArrayList();
	public MyAdapter(Context context,Listlist) {
		this.context=context;
		this.list=list;
	}
	
	@Override
	public int getCount() {
		
		return list.size();
	}

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

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	Holder holder;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		holder=new Holder();
		if (convertView==null) {
			convertView=LayoutInflater.from(context).inflate(R.layout.item, null);
			holder.tv_series=(TextView) convertView.findViewById(R.id.tv_series);
			holder.tv_city=(TextView) convertView.findViewById(R.id.tv_city);
			convertView.setTag(holder);
		}else {
			holder=(Holder) convertView.getTag();
		}
		String series1=list.get(position).getSeries();
		String series2=position-1>=0?list.get(position-1).getSeries():"";
		if (!series1.equals(series2)) {
			holder.tv_series.setVisibility(View.VISIBLE);
			System.out.println(position);
			holder.tv_series.setText(series1);
			holder.tv_city.setText(list.get(position).getCity());
		}else {
			holder.tv_series.setVisibility(View.GONE);
			holder.tv_city.setText(list.get(position).getCity());
		}
		
		
		return convertView;
	}
	
	private class Holder{
		TextView tv_series,tv_city;
	}

}
4.來看下簡單的XML代碼



    
    





    

    

OK了,大功告成,讓我們看一下效果吧!

\

\
n塊エky源碼點擊此處下載




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