我的Android进阶之旅------>Android二级ListView列表的实现
实现例如以下图所看到的的二级列表效果
首先是在布局文件里,布局两个ListView,代码例如以下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#e4e3de" android:choiceMode="singleChoice" android:divider="@drawable/listitem_divide" android:listSelector="#00000000" android:scrollbars="none" > </ListView> <ListView android:id="@+id/subListView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="#e4e3de" android:divider="@drawable/listitem_divide" > </ListView> </LinearLayout>
接着给两个ListView分别写适配器,父类ListView适配器为MyListAdapter,子类ListView适配器为MySubListAdapter。代码例如以下:
/src/com/oyp/multilistview/MyListAdapter.java
package com.oyp.multilistview; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; public class MyListAdapter extends BaseAdapter { Context context; LayoutInflater inflater; String[] categories; int last_item; private int selectedPosition = -1; public MyListAdapter(Context context, String[] categories) { this.context = context; this.categories = categories; inflater = LayoutInflater.from(context); } @Override public int getCount() { return categories.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.mylist_item, null); holder = new ViewHolder(); holder.textView = (TextView) convertView .findViewById(R.id.textView); holder.layout = (LinearLayout) convertView .findViewById(R.id.colorlayout); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (selectedPosition == position) { holder.textView.setTextColor(Color.WHITE); holder.layout.setBackgroundColor(Color.TRANSPARENT); } else { holder.textView.setTextColor(Color.BLUE); holder.layout.setBackgroundColor(Color.LTGRAY); } holder.textView.setText(categories[position]); holder.textView.setTextColor(Color.BLACK); return convertView; } public static class ViewHolder { public TextView textView; public LinearLayout layout; } public void setSelectedPosition(int position) { selectedPosition = position; } }
相应的item布局文件为:/res/layout/mylist_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/colorlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dip" android:layout_marginLeft="8dip" android:layout_marginTop="8dip" android:textSize="16dip" /> </LinearLayout>
/src/com/oyp/multilistview/MySubListAdapter.java
package com.oyp.multilistview; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MySubListAdapter extends BaseAdapter { Context context; LayoutInflater layoutInflater; String[][] sub_categories; public int categoryPoition; public MySubListAdapter(Context context, String[][] sub_categories, int position) { this.context = context; this.sub_categories = sub_categories; layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.categoryPoition = position; } @Override public int getCount() { return sub_categories.length; } @Override public Object getItem(int position) { return getItem(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.mysublist_item, null); viewHolder = new ViewHolder(); viewHolder.textView = (TextView) convertView .findViewById(R.id.subTextView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView.setText(sub_categories[categoryPoition][position]); viewHolder.textView.setTextColor(Color.BLACK); return convertView; } public static class ViewHolder { public TextView textView; } }
相应的item布局文件为:/res/layout/mysublist_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/subTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dip" android:layout_marginLeft="8dip" android:layout_marginTop="8dip" android:textSize="16dip" /> </LinearLayout>
最后是Activity的实现,代码例如以下:
/src/com/oyp/multilistview/MainActivity.java
package com.oyp.multilistview; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private ListView listView; private ListView subListView; private MyListAdapter myAdapter; private MySubListAdapter subAdapter; String sub_categories[][] = new String[][] { new String[] { "所有美食", "本帮江浙菜", "川菜", "粤菜", "湘菜", "东北菜", "台湾菜", "新疆/清真", "素菜", "火锅", "自助餐", "小吃快餐", "日本", "韩国料理", "东南亚菜", "西餐", "面包甜点", "其它" }, new String[] { "所有休闲娱乐", "咖啡厅", "酒吧", "茶馆", "KTV", "电影院", "游乐游艺", "公园", "景点/郊游", "洗浴", "足浴按摩", "文化艺术", "DIY手工坊", "桌球馆", "桌面游戏", "很多其它休闲娱乐" }, new String[] { "所有购物", "综合商场", "服饰鞋包", "运动户外", "珠宝饰品", "化妆品", "数码家电", "亲子购物", "家居建材", "书店", "书店", "眼镜店", "特色集市", "很多其它购物场所", "食品茶酒", "超市/便利店", "药店" }, new String[] { "所有酒店", "经济型酒店", "豪华酒店", "度假酒店", "公寓式酒店", "客栈/青旅/民宿", "主题精品酒店", " 温泉酒店", "亲子酒店" }, new String[] { "所有丽人", "美发", "美容/SPA", "美甲", "瘦身纤体", "个性写真", "瑜伽/舞蹈", "其它" }, new String[] { "所有亲子", "亲子游乐", "亲子摄影", "幼儿教育", "主题公园/游乐场", "展览/演出/赛事", "景点郊游", "DIY手工", "农家乐" }, new String[] { "所有生活", "商场购物劵", "汽车服务", "鲜花婚庆", "体检保健", "宠物服务", "家政服务", "照片冲印", "配眼镜" }, }; String categories[] = new String[] { "美食", "休闲娱乐", "购物", "酒店", "丽人", "亲子", "生活服务" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); subListView = (ListView) findViewById(R.id.subListView); myAdapter = new MyListAdapter(getApplicationContext(), categories); listView.setAdapter(myAdapter); setSubList(0); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { setSubList(position); } }); } public void setSubList(int position) { final int location = position; myAdapter.setSelectedPosition(position); myAdapter.notifyDataSetInvalidated(); subAdapter = new MySubListAdapter(getApplicationContext(), sub_categories, position); subListView.setAdapter(subAdapter); subListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), sub_categories[location][position], Toast.LENGTH_SHORT) .show(); } }); } }
====================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
====================================================================================