首页 > 站长在线 > 简单实现一个android listview分类!

简单实现一个android listview分类!

最近在做一个关于招聘的APP,里面有选择城市一项,是用listview分类,有的人说两个listview嵌套,但是感觉太麻烦了,比较listview底层太复杂,有的人用expandablelistview,但是感觉效果一样不好,所以写了一个比较简单的,希望对大家有帮助!

1.首先看MainActivity类!

其实跟普通的代码一样,只是多了一个MyBean类!

 

[java][/java] view plaincopy

  1. package com.zhaoqingyuan.zhaoqingyuan;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.ListView;
  7. public class MainActivity extends Activity {
  8.     private ListView listView;
  9.     private MyAdapter myAdapter;
  10.     private List<MyBean>list=new ArrayList<MyBean>();//装数据的
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         listView=(ListView) findViewById(R.id.listView1);
  16.         //装数据了
  17.         //当前位置
  18.         MyBean myBean=new MyBean("当前位置", "北京");
  19.         list.add(myBean);
  20.         //热门城市
  21.         String[] hotCity={"全国","北京","上海","武汉","广州","天津","太原","南京","深圳","重庆","昆明","拉萨","石家庄","哈尔滨","贵阳"};
  22.         for (int i = 0; i < hotCity.length; i++) {
  23.             MyBean myBean2=new MyBean("热门城市", hotCity[i]);
  24.             list.add(myBean2);
  25.         }
  26.         //热门城市
  27.         String[] province={"河北省","河南省","云南省","辽宁省","黑龙江省","湖南省","安徽省","山东省","江苏省","浙江省",
  28.                 " 江西省","湖北省","甘肃省","山西省","内蒙古","陕西省","吉林省","福建省","贵州省","广东省",
  29.                 "青海省","西藏","四川省","宁夏回族","海南省","台湾省"};
  30.         for (int i = 0; i < province.length; i++) {
  31.             MyBean myBean3=new MyBean("按省份选择城市", province[i]);
  32.             list.add(myBean3);
  33.         }
  34.         myAdapter=new MyAdapter(MainActivity.this, list);
  35.         listView.setAdapter(myAdapter);
  36.     }
  37. }

2.这个是Bean类,里面有get和set方法!

 

 

[java][/java] view plaincopy

  1. package com.zhaoqingyuan.zhaoqingyuan;
  2. public class MyBean {
  3.     private String series;
  4.     private String city;//可以替代省份名称
  5.     public MyBean(String series,String city) {
  6.         setSeries(series);
  7.         setCity(city);
  8.     }
  9.     public String getSeries() {
  10.         return series;
  11.     }
  12.     public void setSeries(String series) {
  13.         this.series = series;
  14.     }
  15.     public String getCity() {
  16.         return city;
  17.     }
  18.     public void setCity(String city) {
  19.         this.city = city;
  20.     }
  21. }

3.接下来就是我们在熟悉不过的适配器了!就是多了两行代码而已!

 

 

[java][/java] view plaincopy

  1. package com.zhaoqingyuan.zhaoqingyuan;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. public class MyAdapter extends BaseAdapter {
  11.     private Context context;
  12.     private List<MyBean>list=new ArrayList<MyBean>();
  13.     public MyAdapter(Context context,List<MyBean>list) {
  14.         this.context=context;
  15.         this.list=list;
  16.     }
  17.     @Override
  18.     public int getCount() {
  19.         return list.size();
  20.     }
  21.     @Override
  22.     public Object getItem(int position) {
  23.         return list.get(position);
  24.     }
  25.     @Override
  26.     public long getItemId(int position) {
  27.         return position;
  28.     }
  29.     Holder holder;
  30.     @Override
  31.     public View getView(int position, View convertView, ViewGroup parent) {
  32.         holder=new Holder();
  33.         if (convertView==null) {
  34.             convertView=LayoutInflater.from(context).inflate(R.layout.item, null);
  35.             holder.tv_series=(TextView) convertView.findViewById(R.id.tv_series);
  36.             holder.tv_city=(TextView) convertView.findViewById(R.id.tv_city);
  37.             convertView.setTag(holder);
  38.         }else {
  39.             holder=(Holder) convertView.getTag();
  40.         }
  41.         String series1=list.get(position).getSeries();
  42.         String series2=position-1>=0?list.get(position-1).getSeries():"";
  43.         if (!series1.equals(series2)) {
  44.             holder.tv_series.setVisibility(View.VISIBLE);
  45.             System.out.println(position);
  46.             holder.tv_series.setText(series1);
  47.             holder.tv_city.setText(list.get(position).getCity());
  48.         }else {
  49.             holder.tv_series.setVisibility(View.GONE);
  50.             holder.tv_city.setText(list.get(position).getCity());
  51.         }
  52.         return convertView;
  53.     }
  54.     private class Holder{
  55.         TextView tv_series,tv_city;
  56.     }
  57. }

4.来看下简单的XML代码

 

 

[html][/html] view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity" >
  6.     <ListView
  7.         android:id="@+id/listView1"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_alignParentLeft="true"
  11.         android:layout_alignParentTop="true">
  12.     </ListView>
  13. </RelativeLayout>

 

[html][/html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     <TextView
  7.         android:id="@+id/tv_series"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:background="@android:color/darker_gray"
  11.         android:visibility="gone"
  12.         android:padding="5dip"
  13.         android:textSize="18dip"
  14.         android:text="TextView" />
  15.     <TextView
  16.         android:id="@+id/tv_city"
  17.         android:layout_width="match_parent"
  18.         android:layout_height="wrap_content"
  19.         android:layout_marginLeft="10dip"
  20.         android:textSize="18dip"
  21.         android:text="TextView" />
  22. </LinearLayout>

OK了,大功告成,让我们看一下效果吧!

源码点击此处下载:http://download.csdn.net/detail/u011310942/6719753


本文固定链接: http://www.devba.com/index.php/archives/3910.html | 开发吧

报歉!评论已关闭.