您的位置:首页 > 移动开发 > Android开发

Android仿IOS级联菜单_ListView实现区域级联选择效果

2016-11-27 01:57 831 查看
本例子实现了多级菜单选择、可以用于级联地区、级联菜单等功能、实现方法主要利用两个ListView嵌套来完成效果、当点击一个item的时候更新另外一个ListView的内容作到的、其实有些要实现的效果大家大胆点就行了



下面一起来看一下实现代码、首页是onCreate代码

@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();
}


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];
// 获取控件在屏幕中的位置,方便展示Popupwindow
areaLayout.getLocationOnScreen(location);

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


初始化 PopupWindow

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);
}

核心代码都在上面了、其余的代码大家可以下载源代码分析一下、也可以修改修改加入到自己的项目中

源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1dD7vWnZ 密码: dtk2

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: