滚轮弹出选择框

一、效果图


滚轮弹出选择框
 
滚轮弹出选择框
 二、主要布局文件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_provincename"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_width="200dp"
        android:layout_height="38dp"
        android:background="@drawable/edittext_backgroud"
        android:drawableRight="@drawable/title_down"
        android:gravity="center"
        android:hint="请选择省份"
        android:maxLines="1"
        android:paddingLeft="8.5dp"
        android:singleLine="true" />

</RelativeLayout>

 commom_single_list_select.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <kankan.wheel.widget.WheelView
        android:id="@+id/select_item_wheel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="360dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/cancel_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消" />

        <Button
            android:id="@+id/ok_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="确定" />
    </LinearLayout>

</LinearLayout>

 wheel_list_item_common.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/wheel_item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

 MainActivity.java

package com.example.wheeltest;

import java.util.ArrayList;
import java.util.List;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.WheelViewAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private View provinceNameAlertView;
	private View provinceNameOkView;
	private View provinceNameCancleView;
	
	private TextView mProvinceTV;
	
	private WheelView provinceNameWheelView;
	
	private AlertDialog mProvinceNameAd;
	
	private WheelViewAdapter provinceAdapter;
	
	private List<ProvinceInfoBean> provinceList;
	
	
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_main);
		dataInit();
		viewInit();
	}
	
	private void dataInit(){
		provinceList = new ArrayList<ProvinceInfoBean>();
		ProvinceInfoBean bean1 = new ProvinceInfoBean();
		bean1.setId(1);
		bean1.setName("北京市");
		ProvinceInfoBean bean2 = new ProvinceInfoBean();
		bean2.setId(2);
		bean2.setName("天津市");
		ProvinceInfoBean bean3 = new ProvinceInfoBean();
		bean3.setId(3);
		bean3.setName("上海市");
		ProvinceInfoBean bean4 = new ProvinceInfoBean();
		bean4.setId(4);
		bean4.setName("广东省");
		provinceList.add(bean1);
		provinceList.add(bean2);
		provinceList.add(bean3);
		provinceList.add(bean4);
	}
	
	private void viewInit(){
		mProvinceTV = (TextView) findViewById(R.id.tv_provincename);
		provinceNameAlertView = LayoutInflater.from(mContext).inflate(R.layout.common_single_list_select, null);
		provinceNameWheelView = (WheelView) provinceNameAlertView.findViewById(R.id.select_item_wheel);
		provinceNameOkView = provinceNameAlertView.findViewById(R.id.ok_btn);
		provinceNameCancleView = provinceNameAlertView.findViewById(R.id.cancel_btn);
		mProvinceNameAd = new AlertDialog.Builder(mContext)
							.setTitle("请选择省份")
							.setView(provinceNameAlertView).create();
		
		provinceAdapter = new ProvinceAdapter();
		provinceNameWheelView.setViewAdapter(provinceAdapter);
		
		mProvinceTV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!mProvinceNameAd.isShowing()){
					mProvinceNameAd.show();
				}
			}
		});
		
		
		provinceNameOkView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(provinceList != null && !provinceList.isEmpty()){
					int index = provinceNameWheelView.getCurrentItem();
					ProvinceInfoBean bean = provinceList.get(index);
					mProvinceTV.setText(bean.getName());
				}
				if(mProvinceNameAd.isShowing()){
					mProvinceNameAd.dismiss();
				}
			}
		});
		
		provinceNameCancleView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mProvinceNameAd.isShowing()){
					mProvinceNameAd.dismiss();
				}
			}
		});
	}
	
	private final class ProvinceAdapter implements WheelViewAdapter{

		@Override
		public int getItemsCount() {
			// TODO Auto-generated method stub
			return provinceList.size();
		}

		@Override
		public View getItem(int index, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = LayoutInflater.from(mContext).inflate(R.layout.wheel_list_item_common, null);
			TextView textView = (TextView) convertView.findViewById(R.id.wheel_item_text);
			ProvinceInfoBean bean = provinceList.get(index);
			textView.setText(bean.getName());
			return convertView;
		}

		@Override
		public View getEmptyItem(View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}
	}

}

三、需要用到额外的项目包wheel 和 源代码

相关推荐