美文网首页
第三方开源库之 Android-PickerView 省市区三级

第三方开源库之 Android-PickerView 省市区三级

作者: Kevin_小飞象 | 来源:发表于2021-05-20 11:38 被阅读0次

    目前最新版本是 4.1.9。
    GitHub:https://github.com/Bigkoo/Android-PickerView

    简介

    Android-PickerView 是一款仿 iOS 的 PickerView 控件,带有 3D 圆弧效果,并封装了时间选择和选项选择这两种选择器。

    配置

    1. 在 app/build.gradle 中添加依赖
    implementation 'com.contrarywind:Android-PickerView:4.1.9'
    
    1. 下载 province.json .放到如下assets。

    2. 效果图


      Screenshot_2021-05-20-11-30-37-925_com.hk.hksale.jpg

    代码

    1. 布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".ui.InstalledActivity">
    
        <com.hjq.bar.TitleBar
            android:id="@+id/title_bar"
            android:layout_width="match_parent"
            android:background="@color/purple_500"
            android:layout_height="?android:attr/actionBarSize"
            app:title="@string/main_installed"
            app:titleStyle="bold"
            app:titleSize="18sp"
            app:titleColor="@color/white"
            app:leftIcon="@mipmap/ic_back"/>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/white"
                android:layout_marginTop="2dp"
                android:padding="8dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:textSize="16sp"
                    android:layout_marginLeft="8dp"
                    android:textColor="@color/text_color"
                    android:text="客户名称:"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_E3"
                    android:layout_toLeftOf="@id/iv_arrow_sex"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="8dp"
                    android:text="云蝶"/>
    
                <ImageView
                    android:id="@+id/iv_arrow_sex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_arrow_right"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/white"
                android:layout_marginTop="2dp"
                android:padding="8dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:textSize="16sp"
                    android:layout_marginLeft="8dp"
                    android:textColor="@color/text_color"
                    android:text="业务员:"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_E3"
                    android:layout_toLeftOf="@id/iv_arrow"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="8dp"
                    android:text="丁朗"/>
    
                <ImageView
                    android:id="@+id/iv_arrow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_arrow_right"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/rl_new_contact"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="2dp"
                android:background="@color/white"
                android:padding="10dp">
                <TextView
                    android:id="@+id/tv_text4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:textSize="16sp"
                    android:layout_marginLeft="6dp"
                    android:textColor="@color/text_color"
                    android:text="联 系 人:"/>
    
                <EditText
                    android:id="@+id/edt_new_contact"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/tv_text4"
                    android:layout_marginLeft="8dp"
                    android:textSize="14sp"
                    android:background="@null"
                    android:hint="请输入联系人"/>
    
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/rl_new_method"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="2dp"
                android:background="@color/white"
                android:padding="10dp">
                <TextView
                    android:id="@+id/tv_text5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="6dp"
                    android:textSize="16sp"
                    android:textColor="@color/text_color"
                    android:text="联系方式:"/>
    
                <EditText
                    android:id="@+id/edt_new_method"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/tv_text5"
                    android:layout_marginLeft="8dp"
                    android:textSize="14sp"
                    android:background="@null"
                    android:hint="请输入联系方式"/>
    
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/rl_new_address"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="2dp"
                android:background="@color/white"
                android:padding="10dp">
                <TextView
                    android:id="@+id/tv_text8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="6dp"
                    android:textSize="16sp"
                    android:textColor="@color/text_color"
                    android:text="地    址:"/>
    
                <TextView
                    android:id="@+id/tv_new_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/tv_text8"
                    android:layout_marginLeft="8dp"
                    android:background="@null"
                    android:hint="广东省-深圳市-龙岗区"/>
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_arrow_right"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"/>
    
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/rl_detail_address"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="2dp"
                android:background="@color/white"
                android:padding="10dp">
                <TextView
                    android:id="@+id/tv_text9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:textSize="16sp"
                    android:layout_marginLeft="6dp"
                    android:textColor="@color/text_color"
                    android:text="详细地址:"/>
    
                <EditText
                    android:id="@+id/tv_detail_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/tv_text9"
                    android:layout_marginLeft="8dp"
                    android:textSize="14sp"
                    android:background="@null"
                    android:hint="龙岗智慧家园"/>
    
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/rl_product"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/white"
                android:layout_marginTop="2dp"
                android:padding="8dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:textSize="16sp"
                    android:layout_marginLeft="8dp"
                    android:textColor="@color/text_color"
                    android:text="品名规格:"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_E3"
                    android:layout_toLeftOf="@id/iv_arrow2"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="8dp"
                    android:text="选择"/>
    
                <ImageView
                    android:id="@+id/iv_arrow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_arrow_right"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"/>
            </RelativeLayout>
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            <Button
                android:id="@+id/btn_submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_button_bg"
                android:layout_marginTop="120dp"
                android:layout_marginLeft="34dp"
                android:layout_marginRight="34dp"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:text="提交"
                />
        </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>
    
    1. InstallActivity.java
    public class InstalledActivity extends BaseActivity {
        @BindView(R.id.title_bar)
        TitleBar mTitleBar;
    
        private List<JsonBean> options1Items = new ArrayList<>();
        private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
        private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
        private Thread thread;
        private static final int MSG_LOAD_DATA = 0x0001;
        private static final int MSG_LOAD_SUCCESS = 0x0002;
        private static final int MSG_LOAD_FAILED = 0x0003;
    
        @BindView(R.id.tv_new_address)
        TextView mAddress;
    
        private static boolean isLoaded = false;
        @SuppressLint("HandlerLeak")
        private Handler mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_LOAD_DATA:
                        if (thread == null) {//如果已创建就不再重新创建子线程了
                            thread = new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // 子线程中解析省市区数据
                                    initJsonData();
                                }
                            });
                            thread.start();
                        }
                        break;
    
                    case MSG_LOAD_SUCCESS:
                        isLoaded = true;
                        break;
    
                    case MSG_LOAD_FAILED:
                        ToastUtils.showToast(InstalledActivity.this,"解析失败!");
                        break;
                }
            }
        };
    
        @Override
        protected int getLayoutId() {
            return R.layout.activity_installed;
        }
    
    
        @Override
        protected void initView() {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
            mTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
                @Override
                public void onLeftClick(View view) {
                    finish();
                }
    
                @Override
                public void onTitleClick(View view) {
    
                }
    
                @Override
                public void onRightClick(View view) {
    
                }
            });
        }
    
        @OnClick({R.id.rl_product,R.id.rl_new_address})
        public void onClicked(View view) {
            switch (view.getId()) {
                case R.id.rl_product :
                    readyGo(ProductActivity.class);
                    break;
    
                case R.id.rl_new_address :
                    showPickerView();
                    break;
            }
        }
    
        private void showPickerView() {
    
            OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    //返回的分别是三个级别的选中位置
                    String opt1tx = options1Items.size() > 0 ?
                            options1Items.get(options1).getPickerViewText() : "";
    
                    String opt2tx = options2Items.size() > 0
                            && options2Items.get(options1).size() > 0 ?
                            options2Items.get(options1).get(options2) : "";
    
                    String opt3tx = options2Items.size() > 0
                            && options3Items.get(options1).size() > 0
                            && options3Items.get(options1).get(options2).size() > 0 ?
                            options3Items.get(options1).get(options2).get(options3) : "";
    
                    String tx = opt1tx + "-" + opt2tx + "-" + opt3tx;
                    mAddress.setText(tx);
                }
            })
    
                    .setTitleText("城市选择")
                    .setDividerColor(Color.BLACK)
                    .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                    .setContentTextSize(20)
                    .build();
    
            pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
            pvOptions.show();
        }
    
        private void initJsonData() {//解析数据
    
            /**
             * 注意:assets 目录下的Json文件仅供参考,实际使用可自行替换文件
             * 关键逻辑在于循环体
             *
             * */
            String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据
    
            ArrayList<JsonBean> jsonBean = parseData(JsonData);//用Gson 转成实体
    
            /**
             * 添加省份数据
             *
             * 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
             * PickerView会通过getPickerViewText方法获取字符串显示出来。
             */
            options1Items = jsonBean;
    
            for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
                ArrayList<String> cityList = new ArrayList<>();//该省的城市列表(第二级)
                ArrayList<ArrayList<String>> province_AreaList = new ArrayList<>();//该省的所有地区列表(第三极)
    
                for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
                    String cityName = jsonBean.get(i).getCityList().get(c).getName();
                    cityList.add(cityName);//添加城市
                    ArrayList<String> city_AreaList = new ArrayList<>();//该城市的所有地区列表
                    city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
                    province_AreaList.add(city_AreaList);//添加该省所有地区数据
                }
    
                /**
                 * 添加城市数据
                 */
                options2Items.add(cityList);
    
                /**
                 * 添加地区数据
                 */
                options3Items.add(province_AreaList);
            }
    
            mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    
        }
    
    
        public ArrayList<JsonBean> parseData(String result) {
            ArrayList<JsonBean> detail = new ArrayList<>();
            try {
                JSONArray data = new JSONArray(result);
                Gson gson = new Gson();
                for (int i = 0; i < data.length(); i++) {
                    JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
                    detail.add(entity);
                }
            } catch (Exception e) {
                e.printStackTrace();
                mHandler.sendEmptyMessage(MSG_LOAD_FAILED);
            }
            return detail;
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mHandler != null) {
                mHandler.removeCallbacksAndMessages(null);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:第三方开源库之 Android-PickerView 省市区三级

          本文链接:https://www.haomeiwen.com/subject/jbcyjltx.html