本组件主要用到了一个开源项目 Android Wheel Control 和 android的popWindow弹窗技术;
实现效果如下图:
不说废话,直接上代码
核心处理代码:
package com.example.poptest; import kankan.wheel.widget.OnWheelChangedListener; import kankan.wheel.widget.OnWheelScrollListener; import kankan.wheel.widget.WheelView; import kankan.wheel.widget.adapters.AbstractWheelTextAdapter; import kankan.wheel.widget.adapters.ArrayWheelAdapter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; /**** * @production packview风格的三级城市底部弹出菜单组件 * @company android技术帮--【技术成就梦想】 * @department 群号:85506951 * @author 大灰狼叔叔(qq:953486326) * {@docRoot 利用Android Wheel Control 实现的packview风格的三级城市底部弹出菜单 } * @version 1.0 * @since 2013\4\15 * @see 欢迎对android感兴趣的童鞋加入android技术帮,共同学习,共同进步! * ****/ public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private Button button,button_ok; RelativeLayout test_pop_layout; int width,height; private TextView tt ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取屏幕的高度和宽度 Display display = this.getWindowManager().getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); // 获取弹出的layout test_pop_layout = (RelativeLayout)findViewById(R.id.test_pop_layout); tt = (TextView)findViewById(R.id.tpop2_tv); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 显示 popupWindow PopupWindow popupWindow = makePopupWindow(MainActivity.this); int[] xy = new int[2]; test_pop_layout.getLocationOnScreen(xy); popupWindow.showAtLocation(test_pop_layout,Gravity.CENTER|Gravity.BOTTOM, 0, -height); } }); } // Scrolling flag private boolean scrolling = false; private TextView tv; // 创建一个包含自定义view的PopupWindow private PopupWindow makePopupWindow(Context cx) { final PopupWindow window; window = new PopupWindow(cx); View contentView = LayoutInflater.from(this).inflate(R.layout.cities_layout, null); window.setContentView(contentView); tv = (TextView)contentView.findViewById(R.id.tv_cityName); final WheelView country = (WheelView) contentView.findViewById(R.id.country); country.setVisibleItems(3); country.setViewAdapter(new CountryAdapter(this)); final String cities[][] = AddressData.CITIES; final String ccities[][][] = AddressData.COUNTIES; final WheelView city = (WheelView) contentView.findViewById(R.id.city); city.setVisibleItems(0); country.addChangingListener(new OnWheelChangedListener() { public void onChanged(WheelView wheel, int oldValue, int newValue) { if (!scrolling) { updateCities(city, cities, newValue); } } }); country.addScrollingListener( new OnWheelScrollListener() { public void onScrollingStarted(WheelView wheel) { scrolling = true; } public void onScrollingFinished(WheelView wheel) { scrolling = false; updateCities(city, cities, country.getCurrentItem()); tv.setText( AddressData.PROVINCES[country.getCurrentItem()] ); } }); // 地区选择 final WheelView ccity = (WheelView) contentView.findViewById(R.id.ccity); ccity.setVisibleItems(0); city.addChangingListener(new OnWheelChangedListener() { public void onChanged(WheelView wheel, int oldValue, int newValue) { if (!scrolling) { updatecCities(ccity, ccities, country.getCurrentItem(),newValue); } } }); city.addScrollingListener( new OnWheelScrollListener() { public void onScrollingStarted(WheelView wheel) { scrolling = true; } public void onScrollingFinished(WheelView wheel) { scrolling = false; updatecCities(ccity, ccities, country.getCurrentItem(), city.getCurrentItem()); tv.setText( AddressData.PROVINCES[country.getCurrentItem()] + "-" + AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()]); } }); ccity.addScrollingListener( new OnWheelScrollListener() { public void onScrollingStarted(WheelView wheel) { scrolling = true; } public void onScrollingFinished(WheelView wheel) { scrolling = false; tv.setText( AddressData.PROVINCES[country.getCurrentItem()] + "-" + AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()] + "-" + AddressData.COUNTIES[country.getCurrentItem()][city.getCurrentItem()][ccity.getCurrentItem()]); } }); country.setCurrentItem(1); // 点击事件处理 button_ok = (Button)contentView.findViewById(R.id.button_ok); button_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tt.setText(AddressData.PROVINCES[country.getCurrentItem()] + "-" + AddressData.CITIES[country.getCurrentItem()][city.getCurrentItem()] + "-" + AddressData.COUNTIES[country.getCurrentItem()][city.getCurrentItem()][ccity.getCurrentItem()]); window.dismiss(); // 隐藏 } }); window.setWidth(width); window.setHeight(height/2); // 设置PopupWindow外部区域是否可触摸 window.setFocusable(true); //设置PopupWindow可获得焦点 window.setTouchable(true); //设置PopupWindow可触摸 window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸 return window; } /** * Updates the city wheel */ private void updateCities(WheelView city, String cities[][], int index) { ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this, cities[index]); adapter.setTextSize(18); city.setViewAdapter(adapter); city.setCurrentItem(cities[index].length / 2); } /** * Updates the ccity wheel */ private void updatecCities(WheelView city, String ccities[][][], int index,int index2) { ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this, ccities[index][index2]); adapter.setTextSize(18); city.setViewAdapter(adapter); city.setCurrentItem(ccities[index][index2].length / 2); } /** * Adapter for countries */ private class CountryAdapter extends AbstractWheelTextAdapter { // Countries names private String countries[] = AddressData.PROVINCES; /** * Constructor */ protected CountryAdapter(Context context) { super(context, R.layout.country_layout, NO_RESOURCE); setItemTextResource(R.id.country_name); } @Override public View getItem(int index, View cachedView, ViewGroup parent) { View view = super.getItem(index, cachedView, parent); return view; } @Override public int getItemsCount() { return countries.length; } @Override protected CharSequence getItemText(int index) { return countries[index]; } } }
核心布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/layout_bg" android:layout_width="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_cityName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginLeft="5dp" android:text="请选择城市" android:layout_weight="2"/> <Button android:id="@+id/button_ok" android:layout_width="12dip" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:text="确定" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:paddingLeft="12dp" android:paddingRight="12dp" android:paddingTop="4dp" android:layout_marginTop="8dp" android:orientation="horizontal"> <kankan.wheel.widget.WheelView android:id="@+id/country" android:layout_height="110dp" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1"/> <kankan.wheel.widget.WheelView android:id="@+id/city" android:layout_height="110dp" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_weight="1"/> <kankan.wheel.widget.WheelView android:id="@+id/ccity" android:layout_height="110dp" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_weight="1"/> </LinearLayout> <!-- Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="18dp" android:textSize="18sp" android:text=" Next > "/--> </LinearLayout>
也可以到csdn下载:
http://download.csdn.net/detail/helloworldjavaee/5260020
相关推荐
利用Android Wheel Control 实现的packview风格的三级城市底部弹出菜单;模仿iphone中的packview风格,滑动点选功能;
当滚动条滑到最低端显示div
1、选择器增加标题 2、可以拖动选中 3、可以点击选中 4、拖动以及点击都有动画
遮罩优先级在弹出框之下; 弹出框内标签的设置; 滚动栏滚动条的隐藏 如何解决? 弹性布局,横向,三者平分整栏; 状态监听点击事件,数据控制hide或者show,通过rgba设置透明度 弹出框设置z-index; 弹性布局...
网页模板——jQuery实现滚动到网站页面底部动画弹出对话框
这是网上的demo城市三级滚动选择器,感觉不错大家可以下载下来
jQuery拉到底部弹出对话框是一款滚动到网站页面底部,动画弹出信息框,带有关闭按钮。
jquery弹出层当鼠标点击文字或图片弹出层,弹出层浮动在屏幕上可以随浏览器上下滚动,弹出层可自动居中在页面中间
jQuery.UI.Impromptu.js jQuery 弹出层 浮动层 提示框 并随页面滚动而滚动 实现弹出层不再难,只要引用jQuery.UI.Impromptu.js这个js文件后,想要弹出哪个tag都行了 如:我想要这里是弹出浮动遮罩层</div> 并随页面...
NFine 快速开发框架 实现 三级滑动菜单,替换原CSS及JS文件即可
android 输入法键盘弹出与否判断和输入法键盘弹出时将界面滚动到指定位置demo
省份城市区三级联动滚动选择
jquery ui页面滚动底部显示浮动弹出层图文列表展示
基于QT的弹出右侧菜单弹窗的实现,采用QML的方式实现的
jQuery鼠标滚动条到页面底部浮动层滑动弹出信息 jQuery鼠标滚动条到页面底部浮动层滑动弹出信息
移动端省市区三级联动,非select,固定底部,滑动选择省市区,在手机上效果就是点击区域,底部出现省市区选择,并排显示,滑动选择
jQuery实现滚动到网站页面底部动画弹出对话框.zip
JS仿腾讯今日头条,触发滚动条来弹出DIV层。能够控制滚动条滚动的距离。
JS 纵向弹出菜单效果(很漂亮哦)
Android 采用TAB的方式实现微信底部菜单,并且配合单击操作实现主界面的文字滚动。 这里创建了ViewPager mViewPager用来放置界面切换,初始化View适配器后,新建ArrayList,用来存放微信底部菜单的4个Tab,四个Tab...