正文  UI设计 > ListView >

简单实现一个android listview分类!

最近在做一个关于招聘的APP,里面有选择城市一项,是用listview分类,有的人说两个listview嵌套,但是感觉太麻烦了,比较listview底层太复杂,有的人用expandableli...

最近在做一个关于招聘的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 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下载