欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

用ExpandableListView做省市二級(jí)聯(lián)動(dòng)

    今天休息時(shí),我在讀一本書(shū),包著書(shū)皮的緣由,項(xiàng)目經(jīng)理不知我在讀什么,遂問(wèn),你讀什么書(shū),我隨口道“biancheng”。項(xiàng)目經(jīng)理聽(tīng)罷,滿面笑容,旋即對(duì)他一側(cè)的一個(gè)人(也是我的同事)講,你看人家知道在休息時(shí)間補(bǔ)充能量,你只知道玩手機(jī),差距就是這樣一點(diǎn)一點(diǎn)落下來(lái)的!我當(dāng)時(shí)偷笑,委實(shí)只能這樣了,我是在讀“biancheng”,只不過(guò)該書(shū)的作者是沈從文。

10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有鄰水免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

    言歸正傳,項(xiàng)目中需要用到ExpandableListView,自己寫(xiě)了一個(gè)Demo,將它記錄于此。

    先看看主要的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="24sp"
                android:text="@string/the_two_provinces_linkage" />

            <!-- 兩個(gè)自定義的expandablelistview控件 -->

            <com.example.expandablelistviewtest.ScrollExpandableListView
                android:id="@+id/provinceList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:childDivider="@drawable/child_bg"   />

            <com.example.expandablelistviewtest.ScrollExpandableListView
                android:id="@+id/cityList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:childDivider="@drawable/child_bg"   />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

    我在布局中使用了“ScrollView”,防止一頁(yè)內(nèi)容過(guò)多,手機(jī)屏幕顯示不下,那么,問(wèn)題就來(lái)了:ScrollView會(huì)和ExpandableListView造成沖突。所以我又自定義了一個(gè)叫做“ScrollExpandableListView”的控件,代碼如下:

package com.example.expandablelistviewtest;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

public class ScrollExpandableListView extends ExpandableListView {

	public ScrollExpandableListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/* 在ScrollView中嵌套使用ExpandableListView,ExpandableListView只會(huì)顯示一行多一點(diǎn)。
	兩者進(jìn)行嵌套,即會(huì)發(fā)生沖突.需要重寫(xiě)OnMesure,對(duì)ListView或者GridView同樣適用*/
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);

	}

}

     同樣做了一些有關(guān)省市的模擬數(shù)據(jù):

package com.example.expandablelistviewtest;

import java.util.ArrayList;
import java.util.HashMap;

public class ProvinceData {

	private ArrayList<String> provinceGroupData;// 定義省級(jí)組數(shù)據(jù)
	private ArrayList<ArrayList<String>> provinceChildrenData;// 定義省級(jí)組中的子數(shù)據(jù)
	private ArrayList<String> cityGroupData;// 定義市級(jí)組數(shù)據(jù)
	private ArrayList<ArrayList<String>> cityChildrenData;// 定義市級(jí)組數(shù)據(jù)中的子數(shù)據(jù)
	private HashMap<String, ArrayList<String>> provinceToCityMap;// 創(chuàng)建省市的鍵值對(duì)

	public ArrayList<String> getProvinceGroupData() {

		provinceGroupData = new ArrayList<String>();
		provinceGroupData.add("省級(jí)名稱");

		return provinceGroupData;

	}

	public ArrayList<ArrayList<String>> getProvinceChildrenData() {

		provinceChildrenData = new ArrayList<ArrayList<String>>();
		ArrayList<String> provinceList = new ArrayList<String>();
		provinceList.add("北京市");
		provinceList.add("上海市");
		provinceList.add("廣東省");
		provinceList.add("浙江省");
		provinceList.add("江蘇省");
		provinceList.add("湖北省");
		provinceList.add("山西省");
		provinceList.add("河北省");
		provinceChildrenData.add(provinceList);

		return provinceChildrenData;

	}

	public ArrayList<String> getCityGroupData() {

		cityGroupData = new ArrayList<String>();
		cityGroupData.add("市級(jí)名稱");

		return cityGroupData;

	}

	public ArrayList<ArrayList<String>> getcityChildrenData() {

		cityChildrenData = new ArrayList<ArrayList<String>>();

		ArrayList<String> beijingList = new ArrayList<String>();
		beijingList.add("海淀區(qū)");
		beijingList.add("豐臺(tái)區(qū)");
		beijingList.add("昌平區(qū)");
		beijingList.add("密云縣");
		cityChildrenData.add(beijingList);

		ArrayList<String> shanghaiList = new ArrayList<String>();
		shanghaiList.add("嘉定區(qū)");
		shanghaiList.add("浦東新區(qū)");
		cityChildrenData.add(shanghaiList);

		ArrayList<String> guangdongList = new ArrayList<String>();
		guangdongList.add("廣州市");
		guangdongList.add("珠海市");
		guangdongList.add("佛山市");
		guangdongList.add("中山市");
		cityChildrenData.add(guangdongList);

		ArrayList<String> zhejiangList = new ArrayList<String>();
		zhejiangList.add("杭州市");
		zhejiangList.add("寧波市");
		zhejiangList.add("嘉興市");
		cityChildrenData.add(zhejiangList);

		ArrayList<String> jiangsuList = new ArrayList<String>();
		jiangsuList.add("南京市");
		jiangsuList.add("徐州市");
		jiangsuList.add("揚(yáng)州市");
		cityChildrenData.add(jiangsuList);

		ArrayList<String> hubeiList = new ArrayList<String>();
		hubeiList.add("武漢市");
		hubeiList.add("宜昌市");
		hubeiList.add("荊門(mén)市");
		hubeiList.add("黃岡市");
		cityChildrenData.add(hubeiList);

		ArrayList<String> shanxiList = new ArrayList<String>();
		shanxiList.add("太原市");
		shanxiList.add("大同市");
		shanxiList.add("陽(yáng)泉市");
		cityChildrenData.add(shanxiList);

		ArrayList<String> hebeiList = new ArrayList<String>();
		hebeiList.add("石家莊市");
		hebeiList.add("唐山市");
		hebeiList.add("保定市");
		cityChildrenData.add(hebeiList);

		return cityChildrenData;

	}

	public HashMap<String, ArrayList<String>> getProvinceAndCity() {

		ArrayList<ArrayList<String>> cityChildrenList = getcityChildrenData();
		
		provinceToCityMap = new HashMap<String, ArrayList<String>>();
		for (int i = 0; i < provinceChildrenData.get(0).size(); i++) {
		
			provinceToCityMap.put(provinceChildrenData.get(0).get(i),
					cityChildrenList.get(i));
		}

		return provinceToCityMap;

	}
}

    接下來(lái)是主要的一個(gè)類(lèi):

package com.example.expandablelistviewtest;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class ExpandableListViewTest extends Activity {

	private ScrollExpandableListView proviceList;
	private ScrollExpandableListView cityList;
	private ArrayList<String> provinceGroupData;// 定義省級(jí)組數(shù)據(jù)
	private ArrayList<ArrayList<String>> provinceChildrenData;// 定義省級(jí)組中的子數(shù)據(jù)
	private ArrayList<String> cityGroupData;// 定義市級(jí)組數(shù)據(jù)
	private ArrayList<ArrayList<String>> cityChildrenData;// 定義市級(jí)組數(shù)據(jù)中的子數(shù)據(jù)
	private ProvinceData provinceData;
	// 定義一個(gè)兩個(gè)長(zhǎng)度的數(shù)組,存放點(diǎn)中條目的組下標(biāo)和子下標(biāo)
	private int[] provinceIndex = new int[2];
	private int[] cityIndex = new int[2];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expandable_list_view_test);
		provinceIndex[0] = -1;
		provinceIndex[1] = -1;
		provinceData = new ProvinceData();
		provinceGroupData = provinceData.getProvinceGroupData();
		provinceChildrenData = provinceData.getProvinceChildrenData();
		proviceList = (ScrollExpandableListView) findViewById(R.id.provinceList);
		//去掉ExpandableListView默認(rèn)的箭頭,我的手機(jī)貌似不用這一句也沒(méi)有默認(rèn)的箭頭,誰(shuí)能告訴我怎么回事…… 
		proviceList.setGroupIndicator(null);
		final ScrollExpandableListAdapter provinceAdapter = new ScrollExpandableListAdapter(
				this, provinceGroupData, provinceChildrenData, provinceIndex);		
		proviceList.setAdapter(provinceAdapter);
		cityList = (ScrollExpandableListView) findViewById(R.id.cityList);
		cityGroupData = provinceData.getCityGroupData();

		proviceList.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// Expandablelistview 點(diǎn)擊子條目后收起列表
				proviceList.collapseGroup(groupPosition);
				provinceIndex[0] = groupPosition;
				provinceIndex[1] = childPosition;
				cityIndex[0] = -1;
				cityIndex[1] = -1;
				String provinceName = provinceChildrenData.get(0).get(childPosition);

					cityList.setVisibility(View.VISIBLE);
					cityList.setGroupIndicator(null);
					cityChildrenData = new ArrayList<ArrayList<String>>();
					ArrayList<String> cityName = provinceData.getProvinceAndCity().get(
							provinceName);
					cityChildrenData.add(cityName);
					ScrollExpandableListAdapter cityAdapter = new ScrollExpandableListAdapter(
							ExpandableListViewTest.this, cityGroupData, cityChildrenData,cityIndex);
					cityList.setAdapter(cityAdapter);
					cityList.setOnChildClickListener(new OnChildClickListener() {
						
						@Override
						public boolean onChildClick(ExpandableListView parent, View v,
								int groupPosition, int childPosition, long id) {
							cityIndex[0] = groupPosition;
							cityIndex[1] = childPosition;
							cityList.collapseGroup(groupPosition);
							return false;
						}
					});

				return false;
			}
		});
	}

}

    最重要的構(gòu)造器:

package com.example.expandablelistviewtest;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ScrollExpandableListAdapter extends
		BaseExpandableListAdapter {

	private Context context;
	private ArrayList<String> groupList;
	private ArrayList<ArrayList<String>> childList;
	private LayoutInflater layoutInflater;
	// 定義一個(gè)兩個(gè)長(zhǎng)度的數(shù)組,存放點(diǎn)中條目的組下標(biāo)和子下標(biāo)
	private int[] index = new int[2];
	private String groupContent;

	public ScrollExpandableListAdapter(Context context,
			ArrayList<String> groupList,
			ArrayList<ArrayList<String>> childList, int[] index) {
		this.context = context;
		this.groupList = groupList;
		this.childList = childList;
		this.index = index;
		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);

	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder cHolder = new ChildViewHolder();
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_child, null);
			cHolder.childContent = (TextView) convertView
					.findViewById(R.id.childContent);
			cHolder.childCheck = (CheckBox) convertView
					.findViewById(R.id.check);
			convertView.setTag(cHolder);
		} else {
			cHolder = (ChildViewHolder) convertView.getTag();
		}

		cHolder.childContent.setText(childList.get(groupPosition).get(
				childPosition));
		if (index[0] == groupPosition && index[1] == childPosition) {
			cHolder.childCheck.setVisibility(View.VISIBLE);
		} else {
			cHolder.childCheck.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder gHolder = new GroupViewHolder();
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_group, null);
			gHolder.groupName = (TextView) convertView
					.findViewById(R.id.groupName);
			gHolder.groupContent = (TextView) convertView
					.findViewById(R.id.groupContent);
			gHolder.groupPic = (ImageView) convertView
					.findViewById(R.id.groupPic);
			convertView.setTag(gHolder);
		} else {
			gHolder = (GroupViewHolder) convertView.getTag();
		}
		//首次的話用戶沒(méi)有選擇省列表中的子項(xiàng)目,組內(nèi)容中是沒(méi)有數(shù)據(jù)的
		if (index[0] != -1 && index[1] != -1) {
			groupContent = childList.get(index[0]).get(index[1]);
		} else {
			groupContent = "";
		}
		gHolder.groupName.setText(groupList.get(groupPosition));
		gHolder.groupPic.setBackgroundResource(R.drawable.arrow);
		if (!TextUtils.isEmpty(groupContent)) {
			gHolder.groupContent.setText(groupContent);
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		index[0] = groupPosition;
		index[1] = childPosition;
		// 實(shí)現(xiàn)ChildView點(diǎn)擊事件,必須返回true
		return true;
	}

	private class GroupViewHolder {
		TextView groupName;
		TextView groupContent;
		ImageView groupPic;
	}

	private class ChildViewHolder {
		TextView childContent;
		CheckBox childCheck;
	}
}

    組Item與子Item的布局都是自定義的,它們的XML文件分別如下:

    組XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ddd"
    android:gravity="center" >

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <TextView
            android:id="@+id/groupName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="@string/group_name"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/groupContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/groupPic"
            android:textSize="20sp" />

        <ImageView
            android:id="@id/groupPic"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/arrow" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/layout"
        android:background="#ccc" />

</RelativeLayout>

    子X(jué)ML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:id="@+id/childContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/check"
        android:text="BBB"
        android:textSize="20sp" />

    <CheckBox
        android:id="@id/check"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/checked"
        android:button="@null"
        android:visibility="gone" />

</RelativeLayout>

    興許需要講一下設(shè)置ExpandableListView的子Item的分割線的問(wèn)題,先前用“setChildDivider(null);這個(gè)方法來(lái)著,但是會(huì)報(bào)如下空指針錯(cuò)誤:

用ExpandableListView做省市二級(jí)聯(lián)動(dòng)

我還不知道這句代碼應(yīng)寫(xiě)在何處,知道的朋友請(qǐng)告訴一下我。

    我只好寫(xiě)一個(gè)XML文件來(lái)代替了,如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape>
            <!-- 設(shè)置ExpandableListView的Child分割線為透明 -->
            <!-- <solid android:color="#0000" /> -->
           <!-- 設(shè)置ExpandableListView的分割線為紅色 -->
           <solid android:color="#f00" />
        </shape>
    </item>

</layer-list>

    設(shè)置成透明即可實(shí)現(xiàn)沒(méi)有分割線的效果,為了直觀些,此處我改為了紅色。

    事實(shí)上就這段Demo而言,完全可以做成一個(gè)ExpandableListView的,此處用了兩個(gè),是我在項(xiàng)目開(kāi)發(fā)中另有用途。

    最后看一下效果圖吧:

用ExpandableListView做省市二級(jí)聯(lián)動(dòng)

用ExpandableListView做省市二級(jí)聯(lián)動(dòng)

新聞標(biāo)題:用ExpandableListView做省市二級(jí)聯(lián)動(dòng)
文章分享:http://www.chinadenli.net/article20/jcodjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)搜索引擎優(yōu)化定制網(wǎng)站響應(yīng)式網(wǎng)站域名注冊(cè)面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司