滚动选择packview风格的三级城市底部弹出菜单
16lz
2021-01-25
本组件主要用到了一个开源项目 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 ;@Overridepublic 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(){@Overridepublic void onClick(View v){// 显示 popupWindowPopupWindow 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的PopupWindowprivate 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(){@Overridepublic 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><!-- Buttonandroid: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应用安全——组件通信安全(Intent)
- Android桌面小组件:最简例子
- Intent和Intent Filter的区别
- 构建自定义组件
- Android基础之Intent和Intent Filter
- Android学习(一)(初学)SharedPreferences数据库的使用
- android 之 Broadcast(广播) BroadcastReceiver(广播接收者)
- Intent 和 Intent Filter
- Android开发常用调试技术记录