吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4778|回复: 1
收起左侧

[会员申请] 申请会员ID:zylvip

[复制链接]
吾爱游客  发表于 2019-9-17 10:11

申 请 I D:zylvip
个人邮箱:zylvip@163.com
原创技术文章:转自本人博客:https://blog.csdn.net/zylvip

使用Android-PickerView选择器实现三级选择器

实现效果

地址三级联动

  1. 导入依赖

    api 'com.contrarywind:Android-PickerView:4.1.6'
  2. 导入最新地址数据Json文件到与assets目录(若没有assets则新建,目录与res同级)

  3. 创建实体类(用来Json2Bean)

    import com.contrarywind.interfaces.IPickerViewData
    
    //存放省以及所属市
    data class PCACodePO(
            val code: String,
            val name: String,
            val children: MutableList<CCodePO>
    )
    
    //存放市以及所属辖区
    data class CCodePO(
            val code: String,
            val name: String,
            val children: MutableList<AddressInfoPO>
    )
    
    //用于显示PickerView显示
    data class AddressInfoPO(
            //地区编码
            val code: String,
            //地区名称
            val name: String
    ) : IPickerViewData {
        override fun getPickerViewText(): String = name
    }
  4. 编写获取json文件内容工具类

    import android.content.Context
    import android.content.res.AssetManager
    import java.io.*
    
    object FileUtil{
        fun getAssetsFileText(context: Context,fileName:String):String{
            val strBuilder=StringBuilder()
            val assetManager=context.assets
            val bf =BufferedReader(InputStreamReader(assetManager.open(fileName)))
            bf.use { strBuilder.append(it.readLine())}
            bf.close()
            return strBuilder.toString()
        }
    }
    
  5. 显示PickerView

    /**
     * 显示地址选择
     */
    override fun showAddressPicker(provinceItems: MutableList<AddressInfoPO>,
                                   cityItems: MutableList<MutableList<AddressInfoPO>>,
                                   areaItems: MutableList<MutableList<MutableList<AddressInfoPO>>>) {
        val addressPv =
                OptionsPickerBuilder(this, OnOptionsSelectListener { options1, options2, options3, v ->
                    //省份
                    provinceItems[options1]
                    //城市
                    cityItems[options1][options2]
                    //辖区
                    areaItems[options1][options2][options3]
                })
                        .setTitleText(pickerEnum.title)
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build<AddressInfoPO>()
        addressPv.setPicker(provinceItems, cityItems, areaItems)
        addressPv.show()
    }
  6. 初始化三级联动所需数据

    /**
     * 初始化地址数据
     */
    fun initAddressPicker() {
        val provinceItems = mutableListOf<AddressInfoPO>()
        val cityItems = mutableListOf<MutableList<AddressInfoPO>>()
        val areaItems = mutableListOf<MutableList<MutableList<AddressInfoPO>>>()
        //Json2Bean
        val pcaCodeList = Gson().fromJson<MutableList<PCACodePO>>(FileUtil.getAssetsFileText(mApplication, "pcacode.json"), object : TypeToken<MutableList<PCACodePO>>() {}.type)
        //遍历省
        pcaCodeList.forEach {pcaCode ->
            //存放省内市区
            val cityList= mutableListOf<AddressInfoPO>()
            //存放省内所有辖区
            val areaList= mutableListOf<MutableList<AddressInfoPO>>()
            //遍历省内市区
            pcaCode.children.forEach { cCode ->
                //添加省内市区
                cityList.add(AddressInfoPO(cCode.code,cCode.name))
                //存放市内辖区
                val areas= mutableListOf<AddressInfoPO>()
                //添加市内辖区
                cCode.children.forEach {addressInfo->
                    areas.add(addressInfo)
                }
                areaList.add(areas)
            }
            //添加省份
            provinceItems.add(AddressInfoPO(pcaCode.code,pcaCode.name))
            //添加市区
            cityItems.add(cityList)
            //添加辖区
            areaItems.add(areaList)
        }
        //显示选择器
        mRootView.showAddressPicker(provinceItems,cityItems,areaItems)
    }

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2019-9-17 10:19
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 02:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表