实例介绍
【实例简介】
【实例截图】

【核心代码】
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);
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论