序言

如果只需要三级联动目前有现成的库可以使用,但是呢目前需求是需要四级地址选择,没办法只能自己写一个基于Android-PickerView库作为拓展,废话不多说,下面开始贴代码

导入包

api 'com.contrarywind:Android-PickerView:4.1.9'

布局

pv_addressview_options.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><includelayout="@layout/include_pickerview_topbar"android:layout_width="match_parent"android:layout_height="@dimen/pickerview_topbar_height" /><LinearLayoutandroid:id="@+id/optionspicker"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"android:gravity="center"android:minHeight="180dp"android:orientation="horizontal"><com.contrarywind.view.WheelViewandroid:id="@+id/my_address_options1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><com.contrarywind.view.WheelViewandroid:id="@+id/my_address_options2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><com.contrarywind.view.WheelViewandroid:id="@+id/my_address_options3"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><com.contrarywind.view.WheelViewandroid:id="@+id/my_address_options4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout>
</LinearLayout>

代码

  1. listener/OnAddressOptionsSelectChangeListener.kt
package org.fyc.publib.diyPickerview.listener/*** 地址 - 四级联动*/
interface OnAddressOptionsSelectChangeListener {fun onOptionsSelectChanged(options1: Int, options2: Int, options3: Int, options4: Int)
}
  1. options/MyPickerOptions.kt
package org.fyc.publib.diyPickerview.optionsimport com.bigkoo.pickerview.configure.PickerOptions
import org.fyc.publib.diyPickerview.R
import org.fyc.publib.diyPickerview.listener.OnAddressOptionsSelectChangeListenerclass MyPickerOptions(buildType: Int): PickerOptions(buildType) {val TAG_SUBMIT = "submit"val TAG_CANCEL = "cancel"var label4: String = ""var option4 = 0 //默认选中项var x_offset_four = 0//x轴偏移量var cyclic4 = falseinit {if (buildType == TYPE_PICKER_OPTIONS) {layoutRes = R.layout.pv_addressview_options}}var optionsAddSelectChangeListener: OnAddressOptionsSelectChangeListener? = null
}
  1. options/MyAddressWheelOptions.kt
package org.fyc.publib.diyPickerview.optionsimport android.graphics.Typeface
import android.view.View
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.contrarywind.listener.OnItemSelectedListener
import com.contrarywind.view.WheelView
import com.contrarywind.view.WheelView.DividerType
import org.fyc.publib.diyPickerview.R
import org.fyc.publib.diyPickerview.listener.OnAddressOptionsSelectChangeListener// isRestoreItem - 切换时,还原第一项
class MyAddressWheelOptions<T>(private val view: View, private val isRestoreItem: Boolean) {private val wv_option1: WheelView = view.findViewById(R.id.my_address_options1)private val wv_option2: WheelView = view.findViewById(R.id.my_address_options2)private val wv_option3: WheelView = view.findViewById(R.id.my_address_options3)private val wv_option4: WheelView = view.findViewById(R.id.my_address_options4)private var mOptions1Items: List<T>? = nullprivate var mOptions2Items: List<List<T>>? = nullprivate var mOptions3Items: List<List<List<T>>>? = nullprivate var mOptions4Items: List<List<List<List<T>>>>? = nullprivate var linkage = true //默认联动private var isSelectCallback = true // 是否选中回调,默认回调private var wheelListener_option1: OnItemSelectedListener? = nullprivate var wheelListener_option2: OnItemSelectedListener? = nullprivate var wheelListener_option3: OnItemSelectedListener? = nullprivate var optionsSelectChangeListener: OnAddressOptionsSelectChangeListener? = nullfun setPicker(options1Items: List<T>?,options2Items: List<List<T>>?,options3Items: List<List<List<T>>>?,options4Items: List<List<List<List<T>>>>?) {mOptions1Items = options1ItemsmOptions2Items = options2ItemsmOptions3Items = options3ItemsmOptions4Items = options4Items// 选项1wv_option1.adapter = ArrayWheelAdapter(mOptions1Items) // 设置显示数据wv_option1.currentItem = 0 // 初始化时显示的数据// 选项2if (mOptions2Items != null) {wv_option2.adapter = ArrayWheelAdapter(mOptions2Items!![0]) // 设置显示数据}wv_option2.currentItem = wv_option2.currentItem // 初始化时显示的数据// 选项3if (mOptions3Items != null) {wv_option3.adapter = ArrayWheelAdapter(mOptions3Items!![0][0]) // 设置显示数据}wv_option3.currentItem = wv_option3.currentItem// 选项4if (mOptions4Items != null) {wv_option4.adapter = ArrayWheelAdapter(mOptions4Items!![0][0][0]) // 设置显示数据}wv_option4.currentItem = wv_option4.currentItemwv_option1.setIsOptions(true)wv_option2.setIsOptions(true)wv_option3.setIsOptions(true)wv_option4.setIsOptions(true)if (mOptions2Items == null) {wv_option2.visibility = View.GONE} else {wv_option2.visibility = View.VISIBLE}if (mOptions3Items == null) {wv_option3.visibility = View.GONE} else {wv_option3.visibility = View.VISIBLE}if (mOptions4Items == null) {wv_option4.visibility = View.GONE} else {wv_option4.visibility = View.VISIBLE}// 联动监听器wheelListener_option1 =  OnItemSelectedListener { index ->if (mOptions2Items != null) {// 滑动一级默认,选中第一项wv_option2.adapter =ArrayWheelAdapter(mOptions2Items!![index])wv_option2.currentItem = 0// 关联设置三、四级if (mOptions3Items != null) {wv_option3.adapter =ArrayWheelAdapter(mOptions3Items!![index][0])wv_option3.currentItem = 0}if (mOptions4Items != null) {wv_option4.adapter =ArrayWheelAdapter(mOptions4Items!![index][0][0])wv_option4.currentItem = 0}}if (optionsSelectChangeListener != null && isSelectCallback) {optionsSelectChangeListener!!.onOptionsSelectChanged(index, 0, 0, 0)}}wheelListener_option2 = OnItemSelectedListener { index ->if (mOptions3Items != null) {// 滑动二级默认,选中第一项wv_option3.adapter =ArrayWheelAdapter(mOptions3Items!![wv_option1.currentItem][index])wv_option3.currentItem = 0// 设置默认4级if (mOptions4Items != null) {wv_option4.adapter =ArrayWheelAdapter(mOptions4Items!![wv_option1.currentItem][index][0])wv_option4.currentItem = 0}}// 联动数据实时回调if (optionsSelectChangeListener != null && isSelectCallback) {optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem, index, 0, 0)}}wheelListener_option3 = OnItemSelectedListener { index ->if (mOptions4Items != null) {// 滑动三级默认,选中第一项wv_option4.adapter =ArrayWheelAdapter(mOptions4Items!![wv_option1.currentItem][wv_option2.currentItem][index])wv_option4.currentItem = 0}// 联动数据实时回调if (optionsSelectChangeListener != null && isSelectCallback) {optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem, wv_option2.currentItem, index, 0)}}// 添加联动监听if (linkage) {wv_option1.setOnItemSelectedListener(wheelListener_option1)wv_option2.setOnItemSelectedListener(wheelListener_option2)wv_option3.setOnItemSelectedListener(wheelListener_option3)if (optionsSelectChangeListener != null && isSelectCallback) {wv_option4.setOnItemSelectedListener { index ->optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem,wv_option2.currentItem,wv_option3.currentItem,index)}}}}// 不联动情况下fun setNPicker(options1Items: List<T>?,options2Items: List<T>?,options3Items: List<T>?,options4Items: List<T>?) {// 选项1wv_option1.adapter = ArrayWheelAdapter(options1Items) // 设置显示数据wv_option1.currentItem = 0 // 初始化时显示的数据// 选项2if (options2Items != null) {wv_option2.adapter = ArrayWheelAdapter(options2Items) // 设置显示数据}wv_option2.currentItem = wv_option2.currentItem // 初始化时显示的数据// 选项3if (options3Items != null) {wv_option3.adapter = ArrayWheelAdapter(options3Items) // 设置显示数据}wv_option3.currentItem = wv_option3.currentItem// 选项4if (options4Items != null) {wv_option4.adapter = ArrayWheelAdapter(options4Items) // 设置显示数据}wv_option4.currentItem = wv_option4.currentItemwv_option1.setIsOptions(true)wv_option2.setIsOptions(true)wv_option3.setIsOptions(true)wv_option4.setIsOptions(true)optionsSelectChangeListener?.let {wv_option1.setOnItemSelectedListener { index ->optionsSelectChangeListener!!.onOptionsSelectChanged(index,wv_option2.currentItem,wv_option3.currentItem,wv_option4.currentItem)}}if (options2Items == null) {wv_option2.visibility = View.GONE} else {wv_option2.visibility = View.VISIBLEif (optionsSelectChangeListener != null) {wv_option2.setOnItemSelectedListener { index ->optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem,index,wv_option3.currentItem,wv_option4.currentItem)}}}if (options3Items == null) {wv_option3.visibility = View.GONE} else {wv_option3.visibility = View.VISIBLEif (optionsSelectChangeListener != null) {wv_option3.setOnItemSelectedListener { index ->optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem,wv_option2.currentItem,index,wv_option4.currentItem,)}}}if (options4Items == null) {wv_option4.visibility = View.GONE} else {wv_option4.visibility = View.VISIBLEif (optionsSelectChangeListener != null) {wv_option4.setOnItemSelectedListener { index ->optionsSelectChangeListener!!.onOptionsSelectChanged(wv_option1.currentItem,wv_option2.currentItem,wv_option3.currentItem,index,)}}}}fun setTextContentSize(textSize: Int) {wv_option1.setTextSize(textSize.toFloat())wv_option2.setTextSize(textSize.toFloat())wv_option3.setTextSize(textSize.toFloat())wv_option4.setTextSize(textSize.toFloat())}/*** 设置选项的单位** @param label1 单位* @param label2 单位* @param label3 单位* @param label4 单位*/fun setLabels(label1: String?, label2: String?, label3: String?, label4: String?) {if (label1 != null) {wv_option1.setLabel(label1)}if (label2 != null) {wv_option2.setLabel(label2)}if (label3 != null) {wv_option3.setLabel(label3)}if (label4 != null) {wv_option4.setLabel(label4)}}/*** 设置x轴偏移量*/fun setTextXOffset(x_offset_one: Int,x_offset_two: Int,x_offset_three: Int,x_offset_four: Int) {wv_option1.setTextXOffset(x_offset_one)wv_option2.setTextXOffset(x_offset_two)wv_option3.setTextXOffset(x_offset_three)wv_option4.setTextXOffset(x_offset_four)}/*** 设置是否循环滚动** @param cyclic 是否循环*/fun setCyclic(cyclic: Boolean) {wv_option1.setCyclic(cyclic)wv_option2.setCyclic(cyclic)wv_option3.setCyclic(cyclic)wv_option4.setCyclic(cyclic)}/*** 设置字体样式** @param font 系统提供的几种样式*/fun setTypeface(font: Typeface?) {wv_option1.setTypeface(font)wv_option2.setTypeface(font)wv_option3.setTypeface(font)wv_option4.setTypeface(font)}/*** 分别设置第一二三级是否循环滚动** @param cyclic1,cyclic2,cyclic3 是否循环*/fun setCyclic(cyclic1: Boolean, cyclic2: Boolean, cyclic3: Boolean, cyclic4: Boolean) {wv_option1.setCyclic(cyclic1)wv_option2.setCyclic(cyclic2)wv_option3.setCyclic(cyclic3)wv_option4.setCyclic(cyclic4)}/*** 返回当前选中的结果对应的位置数组 因为支持三级联动效果,分三个级别索引,0,1,2。* 在快速滑动未停止时,点击确定按钮,会进行判断,如果匹配数据越界,则设为0,防止index出错导致崩溃。** @return 索引数组*/fun getCurrentItems(): IntArray {val currentItems = IntArray(4)currentItems[0] = wv_option1.currentItemif (!mOptions2Items.isNullOrEmpty()) { //非空判断currentItems[1] =if (wv_option2.currentItem > mOptions2Items!![currentItems[0]].size - 1) 0 else wv_option2.currentItem} else {currentItems[1] = wv_option2.currentItem}if (!mOptions3Items.isNullOrEmpty()) { //非空判断currentItems[2] =if (wv_option3.currentItem > mOptions3Items!![currentItems[0]][currentItems[1]].size - 1) 0 else wv_option3.currentItem} else {currentItems[2] = wv_option3.currentItem}if (!mOptions4Items.isNullOrEmpty()) { //非空判断currentItems[3] =if (wv_option4.currentItem > mOptions4Items!![currentItems[0]][currentItems[1]][currentItems[2]].size - 1) 0 else wv_option4.currentItem} else {currentItems[3] = wv_option4.currentItem}return currentItems}fun setCurrentItems(option1: Int, option2: Int, option3: Int, option4: Int) {if (linkage) {itemSelected(option1, option2, option3, option4)} else {wv_option1.currentItem = option1wv_option2.currentItem = option2wv_option3.currentItem = option3wv_option4.currentItem = option4}}private fun itemSelected(opt1Select: Int, opt2Select: Int, opt3Select: Int, opt4Select: Int) {if (mOptions1Items != null) {wv_option1.currentItem = opt1Select}if (mOptions2Items != null) {wv_option2.adapter = ArrayWheelAdapter(mOptions2Items!![opt1Select])wv_option2.currentItem = opt2Select}if (mOptions3Items != null) {wv_option3.adapter = ArrayWheelAdapter(mOptions3Items!![opt1Select][opt2Select])wv_option3.currentItem = opt3Select}if (mOptions4Items != null) {wv_option4.adapter =ArrayWheelAdapter(mOptions4Items!![opt1Select][opt2Select][opt3Select])wv_option4.currentItem = opt4Select}}/*** 设置间距倍数,但是只能在1.2-4.0f之间** @param lineSpacingMultiplier*/fun setLineSpacingMultiplier(lineSpacingMultiplier: Float) {wv_option1.setLineSpacingMultiplier(lineSpacingMultiplier)wv_option2.setLineSpacingMultiplier(lineSpacingMultiplier)wv_option3.setLineSpacingMultiplier(lineSpacingMultiplier)wv_option4.setLineSpacingMultiplier(lineSpacingMultiplier)}/*** 设置分割线的颜色** @param dividerColor*/fun setDividerColor(dividerColor: Int) {wv_option1.setDividerColor(dividerColor)wv_option2.setDividerColor(dividerColor)wv_option3.setDividerColor(dividerColor)wv_option4.setDividerColor(dividerColor)}/*** 设置分割线的类型** @param dividerType*/fun setDividerType(dividerType: DividerType?) {wv_option1.setDividerType(dividerType)wv_option2.setDividerType(dividerType)wv_option3.setDividerType(dividerType)wv_option4.setDividerType(dividerType)}/*** 设置分割线之间的文字的颜色** @param textColorCenter*/fun setTextColorCenter(textColorCenter: Int) {wv_option1.setTextColorCenter(textColorCenter)wv_option2.setTextColorCenter(textColorCenter)wv_option3.setTextColorCenter(textColorCenter)wv_option4.setTextColorCenter(textColorCenter)}/*** 设置分割线以外文字的颜色** @param textColorOut*/fun setTextColorOut(textColorOut: Int) {wv_option1.setTextColorOut(textColorOut)wv_option2.setTextColorOut(textColorOut)wv_option3.setTextColorOut(textColorOut)wv_option4.setTextColorOut(textColorOut)}/*** Label 是否只显示中间选中项的** @param isCenterLabel*/fun isCenterLabel(isCenterLabel: Boolean) {wv_option1.isCenterLabel(isCenterLabel)wv_option2.isCenterLabel(isCenterLabel)wv_option3.isCenterLabel(isCenterLabel)wv_option4.isCenterLabel(isCenterLabel)}fun setOptionsSelectChangeListener(optionsSelectChangeListener: OnAddressOptionsSelectChangeListener?) {this.optionsSelectChangeListener = optionsSelectChangeListener}// 是否联动,默认联动fun setLinkage(linkage: Boolean) {this.linkage = linkage}// 是否选中回调,默认回调fun setSelectCallback(isSelectCallback: Boolean) {this.isSelectCallback = isSelectCallback}/*** 设置最大可见数目** @param itemsVisible 建议设置为 3 ~ 9之间。*/fun setItemsVisible(itemsVisible: Int) {wv_option1.setItemsVisibleCount(itemsVisible)wv_option2.setItemsVisibleCount(itemsVisible)wv_option3.setItemsVisibleCount(itemsVisible)wv_option4.setItemsVisibleCount(itemsVisible)}fun setAlphaGradient(isAlphaGradient: Boolean) {wv_option1.setAlphaGradient(isAlphaGradient)wv_option2.setAlphaGradient(isAlphaGradient)wv_option3.setAlphaGradient(isAlphaGradient)wv_option4.setAlphaGradient(isAlphaGradient)}
}
  1. view/MyAddressOptionsPickerView.kt
package org.fyc.publib.diyPickerview.viewimport android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.bigkoo.pickerview.view.BasePickerView
import org.fyc.publib.diyPickerview.R
import org.fyc.publib.diyPickerview.options.MyAddressWheelOptions
import org.fyc.publib.diyPickerview.options.MyPickerOptions/*** 地址条件选择器*/
class MyAddressOptionsPickerView<T>(private val pickerOptions: MyPickerOptions): BasePickerView(pickerOptions.context), View.OnClickListener {private var wheelOptions: MyAddressWheelOptions<T>? = nullinit {mPickerOptions = pickerOptionsinitView(pickerOptions.context)}private fun initView(context: Context) {setDialogOutSideCancelable()initViews()initAnim()initEvents()if (pickerOptions.customListener == null) {LayoutInflater.from(context).inflate(pickerOptions.layoutRes, contentContainer)//顶部标题val tvTitle = findViewById(R.id.tvTitle) as TextViewval rv_top_bar = findViewById(R.id.rv_topbar) as RelativeLayout//确定和取消按钮val btnSubmit = findViewById(R.id.btnSubmit) as Buttonval btnCancel = findViewById(R.id.btnCancel) as ButtonbtnSubmit.tag = pickerOptions.TAG_SUBMITbtnCancel.tag = pickerOptions.TAG_CANCELbtnSubmit.setOnClickListener(this)btnCancel.setOnClickListener(this)//设置文字btnSubmit.text =if (TextUtils.isEmpty(pickerOptions.textContentConfirm)) context.resources.getString(R.string.pickerview_submit) else pickerOptions.textContentConfirmbtnCancel.text =if (TextUtils.isEmpty(pickerOptions.textContentCancel)) context.resources.getString(R.string.pickerview_cancel) else pickerOptions.textContentCanceltvTitle.text =if (TextUtils.isEmpty(pickerOptions.textContentTitle)) "" else pickerOptions.textContentTitle //默认为空//设置colorbtnSubmit.setTextColor(pickerOptions.textColorConfirm)btnCancel.setTextColor(pickerOptions.textColorCancel)tvTitle.setTextColor(pickerOptions.textColorTitle)rv_top_bar.setBackgroundColor(pickerOptions.bgColorTitle)//设置文字大小btnSubmit.textSize = pickerOptions.textSizeSubmitCancel.toFloat()btnCancel.textSize = pickerOptions.textSizeSubmitCancel.toFloat()tvTitle.textSize = pickerOptions.textSizeTitle.toFloat()} else {pickerOptions.customListener.customLayout(LayoutInflater.from(context).inflate(pickerOptions.layoutRes, contentContainer))}// ----滚轮布局val optionsPicker = findViewById(R.id.optionspicker) as LinearLayoutoptionsPicker.setBackgroundColor(pickerOptions.bgColorWheel)wheelOptions = MyAddressWheelOptions(optionsPicker, pickerOptions.isRestoreItem)if (pickerOptions.optionsAddSelectChangeListener != null) {wheelOptions?.setOptionsSelectChangeListener(pickerOptions.optionsAddSelectChangeListener)}wheelOptions?.setTextContentSize(pickerOptions.textSizeContent)wheelOptions?.setItemsVisible(pickerOptions.itemsVisibleCount)wheelOptions?.setAlphaGradient(pickerOptions.isAlphaGradient)wheelOptions?.setLabels(pickerOptions.label1, pickerOptions.label2, pickerOptions.label3, pickerOptions.label4)wheelOptions?.setTextXOffset(pickerOptions.x_offset_one,pickerOptions.x_offset_two,pickerOptions.x_offset_three,pickerOptions.x_offset_four,)wheelOptions?.setCyclic(pickerOptions.cyclic1,pickerOptions.cyclic2,pickerOptions.cyclic3,pickerOptions.cyclic4)wheelOptions?.setTypeface(pickerOptions.font)setOutSideCancelable(pickerOptions.cancelable)wheelOptions?.setDividerColor(pickerOptions.dividerColor)wheelOptions?.setDividerType(pickerOptions.dividerType)wheelOptions?.setLineSpacingMultiplier(pickerOptions.lineSpacingMultiplier)wheelOptions?.setTextColorOut(pickerOptions.textColorOut)wheelOptions?.setTextColorCenter(pickerOptions.textColorCenter)wheelOptions?.isCenterLabel(pickerOptions.isCenterLabel)}/*** 动态设置标题** @param text 标题文本内容*/fun setTitleText(text: String?) {val tvTitle = findViewById(R.id.tvTitle) as TextViewtvTitle.text = text}/*** 设置默认选中项** @param option1*/fun setSelectOptions(option1: Int) {mPickerOptions.option1 = option1reSetCurrentItems()}private fun reSetCurrentItems() {if (wheelOptions != null) {wheelOptions!!.setCurrentItems(pickerOptions.option1,pickerOptions.option2,pickerOptions.option3,pickerOptions.option4)}}fun setPicker(optionsItems: List<T>?, isSelectCallback: Boolean = true) {this.setPicker(optionsItems, null, isSelectCallback)}fun setPicker(options1Items: List<T>?, options2Items: List<List<T>>?, isSelectCallback: Boolean = true) {this.setPicker(options1Items, options2Items, null, isSelectCallback)}fun setPicker(options1Items: List<T>?,options2Items: List<List<T>>?,options3Items: List<List<List<T>>>?,isSelectCallback: Boolean = true) {this.setPicker(options1Items, options2Items, options3Items, null, isSelectCallback)}fun setPicker(options1Items: List<T>?,options2Items: List<List<T>>?,options3Items: List<List<List<T>>>?,options4Items: List<List<List<List<T>>>>?,isSelectCallback: Boolean = true) {if (!isSelectCallback) {wheelOptions?.setSelectCallback(false)}wheelOptions?.setPicker(options1Items, options2Items, options3Items, options4Items)reSetCurrentItems()}//不联动情况下调用fun setNPicker(options1Items: List<T>?,options2Items: List<T>?,options3Items: List<T>?,options4Items: List<T>?) {wheelOptions?.setLinkage(false)wheelOptions?.setNPicker(options1Items, options2Items, options3Items, options4Items)reSetCurrentItems()}override fun onClick(v: View) {val tag = v.tag as Stringif (tag == pickerOptions.TAG_SUBMIT) {returnData()} else if (tag == pickerOptions.TAG_CANCEL) {if (pickerOptions.cancelListener != null) {pickerOptions.cancelListener.onClick(v)}}dismiss()}//抽离接口回调的方法private fun returnData() {if (pickerOptions.optionsAddSelectChangeListener != null && wheelOptions != null) {val optionsCurrentItems = wheelOptions!!.getCurrentItems()pickerOptions.optionsAddSelectChangeListener?.onOptionsSelectChanged(optionsCurrentItems[0],optionsCurrentItems[1],optionsCurrentItems[2],optionsCurrentItems[3])}}override fun isDialog(): Boolean {return pickerOptions.isDialog}
}
  1. builder/MyAddressOptionsPickerBuilder.kt
package org.fyc.publib.diyPickerview.builderimport android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import com.bigkoo.pickerview.configure.PickerOptions
import com.bigkoo.pickerview.listener.CustomListener
import com.contrarywind.view.WheelView.DividerType
import org.fyc.publib.diyPickerview.listener.OnAddressOptionsSelectChangeListener
import org.fyc.publib.diyPickerview.options.MyPickerOptions
import org.fyc.publib.diyPickerview.view.MyAddressOptionsPickerViewclass MyAddressOptionsPickerBuilder(context: Context, listener: OnAddressOptionsSelectChangeListener) {// 配置类private val mPickerOptions: MyPickerOptions = MyPickerOptions(PickerOptions.TYPE_PICKER_OPTIONS)init {mPickerOptions.context = contextmPickerOptions.optionsAddSelectChangeListener = listener}//Optionfun setSubmitText(textContentConfirm: String): MyAddressOptionsPickerBuilder {mPickerOptions.textContentConfirm = textContentConfirmreturn this}fun setCancelText(textContentCancel: String): MyAddressOptionsPickerBuilder {mPickerOptions.textContentCancel = textContentCancelreturn this}fun setTitleText(textContentTitle: String): MyAddressOptionsPickerBuilder {mPickerOptions.textContentTitle = textContentTitlereturn this}fun isDialog(isDialog: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.isDialog = isDialogreturn this}fun addOnCancelClickListener(cancelListener: View.OnClickListener): MyAddressOptionsPickerBuilder {mPickerOptions.cancelListener = cancelListenerreturn this}fun setSubmitColor(textColorConfirm: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textColorConfirm = textColorConfirmreturn this}fun setCancelColor(textColorCancel: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textColorCancel = textColorCancelreturn this}/*** [.setOutSideColor] instead.** @param backgroundId color resId.*/@Deprecated("")fun setBackgroundId(backgroundId: Int): MyAddressOptionsPickerBuilder {mPickerOptions.outSideColor = backgroundIdreturn this}/*** 显示时的外部背景色颜色,默认是灰色** @param outSideColor color resId.* @return*/fun setOutSideColor(outSideColor: Int):MyAddressOptionsPickerBuilder {mPickerOptions.outSideColor = outSideColorreturn this}/*** ViewGroup 类型* 设置PickerView的显示容器** @param decorView Parent View.* @return*/fun setDecorView(decorView: ViewGroup?): MyAddressOptionsPickerBuilder {mPickerOptions.decorView = decorViewreturn this}fun setLayoutRes(res: Int, listener: CustomListener?): MyAddressOptionsPickerBuilder {mPickerOptions.layoutRes = resmPickerOptions.customListener = listenerreturn this}fun setBgColor(bgColorWheel: Int): MyAddressOptionsPickerBuilder {mPickerOptions.bgColorWheel = bgColorWheelreturn this}fun setTitleBgColor(bgColorTitle: Int): MyAddressOptionsPickerBuilder {mPickerOptions.bgColorTitle = bgColorTitlereturn this}fun setTitleColor(textColorTitle: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textColorTitle = textColorTitlereturn this}fun setSubCalSize(textSizeSubmitCancel: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textSizeSubmitCancel = textSizeSubmitCancelreturn this}fun setTitleSize(textSizeTitle: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textSizeTitle = textSizeTitlereturn this}fun setContentTextSize(textSizeContent: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textSizeContent = textSizeContentreturn this}fun setOutSideCancelable(cancelable: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.cancelable = cancelablereturn this}fun setLabels(label1: String = "", label2: String = "", label3: String = "",  label4: String = ""): MyAddressOptionsPickerBuilder {mPickerOptions.label1 = label1mPickerOptions.label2 = label2mPickerOptions.label3 = label3mPickerOptions.label4 = label4return this}/*** 设置Item 的间距倍数,用于控制 Item 高度间隔** @param lineSpacingMultiplier 浮点型,1.0-4.0f 之间有效,超过则取极值。*/fun setLineSpacingMultiplier(lineSpacingMultiplier: Float): MyAddressOptionsPickerBuilder {mPickerOptions.lineSpacingMultiplier = lineSpacingMultiplierreturn this}/*** Set item divider line type color.** @param dividerColor color resId.*/fun setDividerColor(@ColorInt dividerColor: Int): MyAddressOptionsPickerBuilder {mPickerOptions.dividerColor = dividerColorreturn this}/*** Set item divider line type.** @param dividerType enum Type [WheelView.DividerType]*/fun setDividerType(dividerType: DividerType?): MyAddressOptionsPickerBuilder {mPickerOptions.dividerType = dividerTypereturn this}/*** Set the textColor of selected item.** @param textColorCenter color res.*/fun setTextColorCenter(textColorCenter: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textColorCenter = textColorCenterreturn this}/*** Set the textColor of outside item.** @param textColorOut color resId.*/fun setTextColorOut(@ColorInt textColorOut: Int): MyAddressOptionsPickerBuilder {mPickerOptions.textColorOut = textColorOutreturn this}fun setTypeface(font: Typeface?): MyAddressOptionsPickerBuilder {mPickerOptions.font = fontreturn this}fun setCyclic(cyclic1: Boolean, cyclic2: Boolean, cyclic3: Boolean, cyclic4: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.cyclic1 = cyclic1mPickerOptions.cyclic2 = cyclic2mPickerOptions.cyclic3 = cyclic3mPickerOptions.cyclic4 = cyclic4return this}fun setSelectOptions(option1: Int): MyAddressOptionsPickerBuilder {mPickerOptions.option1 = option1return this}fun setSelectOptions(option1: Int, option2: Int): MyAddressOptionsPickerBuilder {mPickerOptions.option1 = option1mPickerOptions.option2 = option2return this}fun setSelectOptions(option1: Int, option2: Int, option3: Int): MyAddressOptionsPickerBuilder {mPickerOptions.option1 = option1mPickerOptions.option2 = option2mPickerOptions.option3 = option3return this}fun setSelectOptions(option1: Int, option2: Int, option3: Int, option4: Int): MyAddressOptionsPickerBuilder {mPickerOptions.option1 = option1mPickerOptions.option2 = option2mPickerOptions.option3 = option3mPickerOptions.option4 = option4return this}fun setTextXOffset(xoffset_one: Int,xoffset_two: Int,xoffset_three: Int,x_offset_four: Int): MyAddressOptionsPickerBuilder {mPickerOptions.x_offset_one = xoffset_onemPickerOptions.x_offset_two = xoffset_twomPickerOptions.x_offset_three = xoffset_threemPickerOptions.x_offset_four = x_offset_fourreturn this}fun isCenterLabel(isCenterLabel: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.isCenterLabel = isCenterLabelreturn this}/*** 设置最大可见数目** @param count 建议设置为 3 ~ 9之间。*/fun setItemVisibleCount(count: Int): MyAddressOptionsPickerBuilder {mPickerOptions.itemsVisibleCount = countreturn this}/*** 透明度是否渐变** @param isAlphaGradient true of false*/fun isAlphaGradient(isAlphaGradient: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.isAlphaGradient = isAlphaGradientreturn this}/*** 切换选项时,是否还原第一项** @param isRestoreItem true:还原; false: 保持上一个选项* @return TimePickerBuilder*/fun isRestoreItem(isRestoreItem: Boolean): MyAddressOptionsPickerBuilder {mPickerOptions.isRestoreItem = isRestoreItemreturn this}/*** @param listener 切换item项滚动停止时,实时回调监听。* @return*/fun setOptionsSelectChangeListener(listener: OnAddressOptionsSelectChangeListener): MyAddressOptionsPickerBuilder {mPickerOptions.optionsAddSelectChangeListener = listenerreturn this}fun <T> build(): MyAddressOptionsPickerView<T> {return MyAddressOptionsPickerView(mPickerOptions)}
}

测试

package com.fyc.test.myaddressviewtestimport android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import org.fyc.publib.diyPickerview.builder.MyAddressOptionsPickerBuilder
import org.fyc.publib.diyPickerview.listener.OnAddressOptionsSelectChangeListenerclass MainActivity : AppCompatActivity() {private lateinit var buttonOne: Buttonoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)initView()initClick()}private fun initView() {buttonOne = findViewById(R.id.btn_one)}private val item1Array = mutableListOf("广东省", "广西省")private val item2Array = mutableListOf<List<String>>(mutableListOf("广州市", "韶关市", "深圳市"),mutableListOf("南宁市"))private val item3Array = mutableListOf<List<List<String>>>(mutableListOf(mutableListOf("荔湾区"), mutableListOf("越秀区", "aa区"), mutableListOf("福田区")),mutableListOf(mutableListOf("兴宁区")))private val item4Array = mutableListOf<List<List<List<String>>>>(mutableListOf(mutableListOf(mutableListOf("x1街道", "x2街道")),mutableListOf(mutableListOf("y1街道", "y2街道"), mutableListOf("y3街道", "y4街道")), mutableListOf(mutableListOf("z1街道", "z2街道"))),mutableListOf(mutableListOf(mutableListOf("t1街道"))))private fun initClick() {buttonOne.setOnClickListener {testOne()}}private fun testOne() {val pvOptions = MyAddressOptionsPickerBuilder(this, object : OnAddressOptionsSelectChangeListener {override fun onOptionsSelectChanged(options1: Int,options2: Int,options3: Int,options4: Int) {//                Log.e("fyc", "MyAddressOptionsPickerBuilder >>> options1 = $options1, options2 = $options2, options3 = $options3, options4 = $options4")Log.e("fyc", "MyAddressOptionsPickerBuilder >>> options1 = ${item1Array[options1]}, options2 = ${item2Array[options1][options2]}, options3 = ${item3Array[options1][options2][options3]}, options4 = ${item4Array[options1][options2][options3][options4]}")}}).build<String>()pvOptions.setPicker(item1Array, item2Array, item3Array, item4Array, false)pvOptions.show()}private fun testTwo() {//条件选择器val pvOptions = OptionsPickerBuilder(this@MainActivity) { options1, options2, options3, _ -> //返回的分别是三个级别的选中位置Log.e("fyc", "MyAddressOptionsPickerBuilder >>> options1 = $options1, options2 = $options2, options3 = $options3")}.build<String>()pvOptions.setPicker(item1Array, item2Array, item3Array)pvOptions.show()}
}

总结

遇到问题还是优先尝试解决问题吧。

Android 仿ios四级联动地址选择器相关推荐

  1. android 仿ios三级联动,仿iOS的PickerView控件,有时间选择和选项选择并支持一二三级联动效果...

    Android-PickerView 注意事项.详请使用方式.更新日志等,请查看 Wiki文档 Wiki文档,Wiki文档,Wiki文档 !~ 重要的事情说三遍 对于使用上有任何疑问或优化建议等,欢迎 ...

  2. Android仿京东收货地址

    Android仿京东三级联动收货地址 1.在本地新建assets目录,存放三级联动json数据,取本地json数据作为数据源 String data = com.miles.zcstc.fingerd ...

  3. android rebound平移,Android 仿 IOS 拖拽回弹之进阶 ReboundFrameLayout

    Android 仿 IOS 拖拽回弹之进阶 ReboundFrameLayout 前言 IOS 拖拽回弹给用户的体验不得不赞然后 Android 原生的 API 各种不支持, 于是乎出现的很多仿 IO ...

  4. Android仿IOS吸边弹簧阻尼移动组件SpringMovingView-自定义view系列(3)

    () Android仿ios吸边弹簧阻尼效果的移动组件SpringMovingView 功能简介 Android技术生活交流 Gif演示 实现步骤 java代码 Android技术生活交流 更多其他页 ...

  5. android仿ios弹框_在“提示”框中:iOS外观(在Android上运行),Google Maps作为Time Machine,下载Wii游戏保存...

    android仿ios弹框 Once a week we round up some great reader tips and share them with everyone. Read on t ...

  6. php地址选择插件,微信小程序中关于三级联动地址选择器的实例分享

    本文介绍了微信小程序三级联动地址选择器的实例代码,分享给大家,有需要的可以一起了解一下 在一些电商类的小程序中,地址选择这个功能一般是必备的,一般的收货信息都需要有一个能选择省市县的控件,当然也有些人 ...

  7. android 仿ios动画效果代码,Android仿IOS上拉下拉弹性效果的实例代码

    用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好:Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果.先看效果,感觉有些时候还 ...

  8. android 密码解锁程序,android 仿ios数字密码解锁界面的实例

    如下所示: 每个Android开发人员都知道,现在android的解锁最常用的就是九宫格解锁,ios的解锁常用的是数字密码解锁.而我们在开发工程中,很多时候,都需要android和ios进行结合.有的 ...

  9. Android仿IOS解锁密码界面-自定义view系列(6)

    Android仿IOS解锁密码界面-自定义view系列 功能简介 主要实现步骤-具体内容看github项目里的代码 xml相关属性设置 Android Studio 代码 Android技术生活交流 ...

  10. android 按钮回弹效果,Android仿IOS回弹效果 支持任何控件

    本文实例为大家分享了Android仿IOS回弹效果的具体代码,供大家参考,具体内容如下 效果图: 导入依赖: dependencies { // ... compile 'me.everything: ...

最新文章

  1. java学习笔记十一——对象转型
  2. 如何在firefox下获取下列框选中option的text
  3. Linux挂载iso文件步骤
  4. 《笨办法学Python》笔记33-----一个项目骨架(转)
  5. SQL中Convert()函数的使用方法
  6. cad道路里程桩号标注_甲级设计院CAD制图技巧与画法讲解! 超实用,值得你收藏~...
  7. 微信小程序物流仓库平台+后台管理系统|前后分离VUE
  8. 专利挖掘和撰写(京东技术资质申请和创造专利挖掘)
  9. 数量关系-经济利润问题
  10. Quartus II中关于IP核的破解
  11. dubbo异常源码分析处理
  12. 信息学奥赛一本通:1153:绝对素数
  13. photoshop卸载不彻底--论如何删除ADMUI3.fon
  14. matlab用ode23解决参数方程,matlab变参量微分方程处理
  15. 在线随机密码生成器源码
  16. [ITIL]-ITIL4考点考题
  17. i believe i can fly
  18. Word论文格式-页眉页脚页码
  19. 微软小娜关闭服务器,Win10关闭小娜服务的两个方法
  20. Excel分段求平均值

热门文章

  1. 小程序轮播图测试用例
  2. linux ext4限制,linux – 限制ext4文件系统中文件的最大大小
  3. 信息与计算机课件,第一章 信息技术与计算机ppt课件.ppt
  4. android_adb pm和adb am +启动/杀死app进程
  5. web技术分享| WebRTC 实现屏幕共享
  6. WPS Office 国际版 添加中文语言包 及相关问题
  7. jQuery视频格式的验证
  8. 编译安装libmodbus库
  9. 如何设置Idea字体颜色
  10. win7安装IIS后如何远程访问IIS