基本信息
源码名称:android 区域联动源码下载(用于选择地区以及搜索分类)
源码大小:1.64M
文件格式:.rar
开发语言:Java
更新时间:2016-01-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

package com.zihao.ui;

import com.example.test.R;
import com.zihao.adapter.GroupAdapter;
import com.zihao.adapter.ChildAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	View showPupWindow = null; // 选择区域的view
	/** 一级菜单名称数组 **/
	String[] GroupNameArray = { "全合肥", "蜀山", "庐阳", "包河", "瑶海", "经开", "高新" };
	/** 二级菜单名称数组 **/
	String[][] childNameArray = { {},
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" },
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" },
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" },
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" },
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" },
			{ "全蜀山", "稻香村", "南七里站", "三里庵", "琥珀山庄", "西园新村", "五里墩" } };
	ListView groupListView = null;
	ListView childListView = null;
	GroupAdapter groupAdapter = null;
	ChildAdapter childAdapter = null;

	TranslateAnimation animation;// 出现的动画效果
	// 屏幕的宽高
	public static int screen_width = 0;
	public static int screen_height = 0;
	PopupWindow mPopupWindow = null;
	private LinearLayout areaLayout, wageLayout, classLayout, searchLayout;
	private boolean[] tabStateArr = new boolean[4];// 标记tab的选中状态,方便设置
	private ImageView areaImg, WageImg, classImg;
	private TextView areaText, wageText, classText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取手机屏幕的大小
		screen_width = dm.widthPixels;
		screen_height = dm.heightPixels;

		initView();
	}

	private void initView() {
		areaLayout = (LinearLayout) findViewById(R.id.area_layout);
		wageLayout = (LinearLayout) findViewById(R.id.wage_layout);
		classLayout = (LinearLayout) findViewById(R.id.class_layout);
		searchLayout = (LinearLayout) findViewById(R.id.search_layout);
		areaImg = (ImageView) findViewById(R.id.area_img);
		WageImg = (ImageView) findViewById(R.id.wage_img);
		classImg = (ImageView) findViewById(R.id.class_img);
		areaText = (TextView) findViewById(R.id.area_textView);
		wageText = (TextView) findViewById(R.id.wage_textView);
		classText = (TextView) findViewById(R.id.class_textView);

		areaLayout.setOnClickListener(this);
		wageLayout.setOnClickListener(this);
		classLayout.setOnClickListener(this);
		searchLayout.setOnClickListener(this);

		int[] location = new int[2];
		areaLayout.getLocationOnScreen(location);// 获取控件在屏幕中的位置,方便展示Popupwindow

		animation = new TranslateAnimation(0, 0, -700, location[1]);
		animation.setDuration(500);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.area_layout:
			tabStateArr[0] = !tabStateArr[0];
			setTabState(areaImg, areaText, tabStateArr[0]);
			if (tabStateArr[0]) {// 判断是否需要关闭弹出层
				showPupupWindow();
			} else {
				mPopupWindow.dismiss();
			}
			break;
		case R.id.wage_layout:
			tabStateArr[1] = !tabStateArr[1];
			setTabState(WageImg, wageText, tabStateArr[1]);
			break;
		case R.id.class_layout:
			tabStateArr[2] = !tabStateArr[2];
			setTabState(classImg, classText, tabStateArr[2]);
			break;
		case R.id.search_layout:
			tabStateArr[3] = !tabStateArr[3];
			break;
		}
	}

	/**
	 * 设置tab的状态
	 * 
	 * @param img
	 *            // ImageView对象
	 * @param textView
	 *            // TextView对象
	 * @param state
	 *            // 状态
	 */
	private void setTabState(ImageView img, TextView textView, boolean state) {
		if (state) {// 选中状态
			img.setBackgroundResource(R.drawable.up);
			textView.setTextColor(getResources().getColor(
					R.color.tab_text_pressed_color));
		} else {
			img.setBackgroundResource(R.drawable.down);
			textView.setTextColor(getResources().getColor(
					R.color.tab_text_color));
		}
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 20:
				childAdapter.setChildData(childNameArray[msg.arg1]);
				childAdapter.notifyDataSetChanged();
				groupAdapter.notifyDataSetChanged();
				break;
			default:
				break;
			}

		};
	};

	/**
	 * 初始化 PopupWindow
	 * 
	 * @param view
	 */
	public void initPopuWindow(View view) {
		/* 第一个参数弹出显示view 后两个是窗口大小 */
		mPopupWindow = new PopupWindow(view, screen_width, screen_height);
		/* 设置背景显示 */
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.mypop_bg));
		/* 设置触摸外面时消失 */
		// mPopupWindow.setOutsideTouchable(true);

		mPopupWindow.update();
		mPopupWindow.setTouchable(true);
		/* 设置点击menu以外其他地方以及返回键退出 */
		mPopupWindow.setFocusable(true);

		/**
		 * 1.解决再次点击MENU键无反应问题 2.sub_view是PopupWindow的子View
		 */
		view.setFocusableInTouchMode(true);
	}

	/**
	 * 展示区域选择的对话框
	 */
	private void showPupupWindow() {
		if (mPopupWindow == null) {
			showPupWindow = LayoutInflater.from(this).inflate(
					R.layout.bottom_layout, null);
			initPopuWindow(showPupWindow);

			groupListView = (ListView) showPupWindow
					.findViewById(R.id.listView1);
			childListView = (ListView) showPupWindow
					.findViewById(R.id.listView2);

			groupAdapter = new GroupAdapter(this, GroupNameArray);
			groupListView.setAdapter(groupAdapter);
		}

		groupListView.setOnItemClickListener(new MyItemClick());

		childListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});

		showPupWindow.setAnimation(animation);
		showPupWindow.startAnimation(animation);

		mPopupWindow.showAsDropDown(areaLayout, -5, 10);

	}

	class MyItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			groupAdapter.setSelectedPosition(position);

			if (childAdapter == null) {
				childAdapter = new ChildAdapter(MainActivity.this);
				childListView.setAdapter(childAdapter);
			}

			Message msg = new Message();
			msg.what = 20;
			msg.arg1 = position;
			handler.sendMessage(msg);
		}

	}

}