1、继承 BaseDialog,实现View.OnClickListener接口,传入参数为第二步写的监听器
2、内部接口,用于回调数据,当事件(城市选择完成)后关闭dialog,并调用接口方法,调用者实现接口并处理回调数据
3-5、数据、监听初始化
6、文字选择逻辑,完成并回调
选择样式:
其他效果:
//kotlin
class AddressSelectorDialog(listenner: AddressSelectFinish) : BaseDialog(), View.OnClickListener{
}
//kotlin
interface AddressSelectFinish {
fun getCityString(str: StringBuffer)
}
//kotlin
//内部接口实例
private var listenner: AddressSelectFinish = listenner
//城市数据
lateinit var cityname: MutableList<String>
//布局中城市列表recycleview的adapter
lateinit var adpater: CityListAdapter
//当前已选择的城市字段
lateinit var selectedStr: MutableList<String>
//头部指示标签的view
lateinit var shadows: MutableList<View>
//头部提示文字的view
lateinit var showtext: MutableList<TextView>
//当前执行选择的步数
private var step = 0
companion object {
private lateinit var dialog: AddressSelectorDialog
fun newInstance(listenner: AddressSelectFinish): AddressSelectorDialog {
dialog = AddressSelectorDialog(listenner)
return dialog
}
}
//覆写父类方法获取视图
override fun getLayoutRes(): Int {
return R.layout.fragment_address_selection
}
//初始化关闭按钮的监听事件
override fun initView(v: View) {
v.close_select.setOnClickListener(this)
initAdapterAndData(v)
}
//添加头部文字和标签的view
fun initAdapterAndData(v: View) {
shadows = mutableListOf()
shadows.add(v.choose_shadow)
shadows.add(v.choose_shadow2)
shadows.add(v.choose_shadow3)
showtext = mutableListOf()
showtext.add(v.province_text)
showtext.add(v.city_text)
showtext.add(v.area_text)
//文字点击事件
for (i in 0..2){
showtext[i].setOnClickListener(this)
}
selectedStr = mutableListOf()
cityname = mutableListOf()
//初始化城市数据
getProviceString()
//初始化列表view
v.address_selector.layoutManager = LinearLayoutManager(context)
adpater = CityListAdapter(cityname)
adpater.setItemClickListenner { position, value ->
putStrToSelected(value.text.toString())
}
v.address_selector.adapter = adpater
}
//初始化假数据
fun getProviceString() {
selectedStr.clear()
cityname.clear()
cityname.add("北京")
cityname.add("四川")
cityname.add("广州")
cityname.add("上海")
cityname.add("深圳")
cityname.add("陕西")
cityname.add("内蒙古")
cityname.add("云南")
cityname.add("city9")
cityname.add("city10")
cityname.add("city11")
cityname.add("city12")
}
fun getCityString() {
cityname.clear()
for (i in 1..15) {
cityname.add("城市${i}")
}
adpater?.notifyDataSetChanged()
}
fun getAreaString() {
cityname.clear()
for (i in 1..15) {
cityname.add("区县${i}")
}
adpater?.notifyDataSetChanged()
}
//1.将选择记录,改变头部标签和文字
fun putStrToSelected(str: String) {
selectedStr.add(str)
goToStep(++step)
refreshView(step, str, false)
}
//2.跳到对应的步数
fun goToStep(step: Int) {
when (step) {
0 -> {
getProviceString()
}
1 -> {
getCityString()
}
2 -> {
getAreaString()
}
3 -> {
//选择结束,回调数据
var temp: StringBuffer = StringBuffer("")
for (i in 0..selectedStr.size - 1) {
temp.append(selectedStr.get(i))
}
listenner.getCityString(temp)
dismiss()
}
}
}
//刷新样式,“城市”,“省份”替换为选择的文字,并在下面标示
fun refreshView(step: Int, str: String, goback: Boolean) {
if (step >= 3) {
return
}
for (i in 0..2) {
shadows[i].visibility = View.GONE
}
shadows[step].visibility = View.VISIBLE
if (!goback) {
showtext[step - 1].text = str
}
}
//头部文字点击事件
override fun onClick(v: View?) {
when (v?.id) {
R.id.close_select -> {
dismiss()
}
R.id.province_text -> {
if (step > 0) {
step=0
selectedStr.clear()
showtext[1].text = "城市"
showtext[0].text= "省份"
refreshView(step, "", true)
getProviceString()
adpater.notifyDataSetChanged()
}
}
R.id.city_text -> {
if (step>1){
step=1
selectedStr.removeAt(1)
showtext[1].text = "城市"
refreshView(step,selectedStr[0],false)
getCityString()
adpater.notifyDataSetChanged()
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="500dp"
android:minHeight="500dp"
tools:context=".dialog.AddressSelectorDialog"
android:background="@drawable/address_selection_radius_bg"
android:orientation="vertical">
<ImageView
android:id="@+id/close_select"
android:layout_width="17dp"
android:layout_height="17dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:src="@mipmap/guanbi"/>
<TextView
android:id="@+id/province_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="省份"
android:layout_marginTop="21dp"
android:layout_marginLeft="17dp"
android:textColor="#ff333333"
android:textSize="16sp"/>
<TextView
android:id="@+id/city_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="城市"
android:layout_marginTop="21dp"
android:layout_marginLeft="113dp"
android:textColor="#ff333333"
android:textSize="16sp"/>
<TextView
android:id="@+id/area_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="区县"
android:layout_marginTop="21dp"
android:layout_marginLeft="213dp"
android:textColor="#ff333333"
android:textSize="16sp"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#E9E9E9"
app:layout_constraintTop_toBottomOf="@+id/province_text"
android:layout_marginTop="6dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
<View
android:id="@+id/choose_shadow"
android:layout_width="35dp"
android:layout_height="1dp"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#FF3400"
app:layout_constraintTop_toBottomOf="@+id/province_text"
android:layout_marginTop="5.5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
<View
android:id="@+id/choose_shadow2"
android:layout_width="35dp"
android:layout_height="1dp"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#FF3400"
app:layout_constraintTop_toBottomOf="@+id/province_text"
android:layout_marginTop="5.5dp"
android:layout_marginLeft="111.5dp"
android:layout_marginRight="15dp"
android:visibility="gone"
/>
<View
android:id="@+id/choose_shadow3"
android:layout_width="35dp"
android:layout_height="1dp"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#FF3400"
app:layout_constraintTop_toBottomOf="@+id/province_text"
android:layout_marginTop="5.5dp"
android:layout_marginLeft="211.5dp"
android:layout_marginRight="15dp"
android:visibility="gone"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/address_selector"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/province_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.hotniao.live.bopin.dialog
import android.view.View
import android.widget.CompoundButton
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.hotniao.live.baselibrary.base.BaseDialog
import com.hotniao.live.bopin.adapter.CityListAdapter
import kotlinx.android.synthetic.main.fragment_address_selection.view.*
import com.hotniao.live.bopin.R
class AddressSelectorDialog(listenner: AddressSelectFinish) : BaseDialog(), View.OnClickListener {
//内部接口实例
private var listenner: AddressSelectFinish = listenner
//城市数据
lateinit var cityname: MutableList<String>
//布局中城市列表recycleview的adapter
lateinit var adpater: CityListAdapter
//当前已选择的城市字段
lateinit var selectedStr: MutableList<String>
//头部指示标签的view
lateinit var shadows: MutableList<View>
//头部提示文字的view
lateinit var showtext: MutableList<TextView>
//当前执行选择的步数
private var step = 0
companion object {
private lateinit var dialog: AddressSelectorDialog
fun newInstance(listenner: AddressSelectFinish): AddressSelectorDialog {
dialog = AddressSelectorDialog(listenner)
return dialog
}
}
//头部文字点击事件
override fun onClick(v: View?) {
when (v?.id) {
R.id.close_select -> {
dismiss()
}
R.id.province_text -> {
if (step > 0) {
step=0
selectedStr.clear()
showtext[1].text = "城市"
showtext[0].text= "省份"
refreshView(step, "", true)
getProviceString()
adpater.notifyDataSetChanged()
}
}
R.id.city_text -> {
if (step>1){
step=1
selectedStr.removeAt(1)
showtext[1].text = "城市"
refreshView(step,selectedStr[0],false)
getCityString()
adpater.notifyDataSetChanged()
}
}
}
}
override fun initView(v: View) {
v.close_select.setOnClickListener(this)
initAdapterAndData(v)
}
override fun getLayoutRes(): Int {
return R.layout.fragment_address_selection
}
fun initAdapterAndData(v: View) {
shadows = mutableListOf()
shadows.add(v.choose_shadow)
shadows.add(v.choose_shadow2)
shadows.add(v.choose_shadow3)
showtext = mutableListOf()
showtext.add(v.province_text)
showtext.add(v.city_text)
showtext.add(v.area_text)
for (i in 0..2){
showtext[i].setOnClickListener(this)
}
selectedStr = mutableListOf()
cityname = mutableListOf()
getProviceString()
v.address_selector.layoutManager = LinearLayoutManager(context)
adpater = CityListAdapter(cityname)
adpater.setItemClickListenner { position, value ->
putStrToSelected(value.text.toString())
}
v.address_selector.adapter = adpater
}
fun getProviceString() {
selectedStr.clear()
cityname.clear()
cityname.add("北京")
cityname.add("四川")
cityname.add("广州")
cityname.add("上海")
cityname.add("深圳")
cityname.add("陕西")
cityname.add("内蒙古")
cityname.add("云南")
cityname.add("city9")
cityname.add("city10")
cityname.add("city11")
cityname.add("city12")
}
fun getCityString() {
cityname.clear()
for (i in 1..15) {
cityname.add("城市${i}")
}
adpater?.notifyDataSetChanged()
}
fun getAreaString() {
cityname.clear()
for (i in 1..15) {
cityname.add("区县${i}")
}
adpater?.notifyDataSetChanged()
}
//将选择记录,改变头部标签和文字
fun putStrToSelected(str: String) {
selectedStr.add(str)
goToStep(++step)
refreshView(step, str, false)
}
fun goToStep(step: Int) {
when (step) {
0 -> {
getProviceString()
}
1 -> {
getCityString()
}
2 -> {
getAreaString()
}
3 -> {
//select finish
var temp: StringBuffer = StringBuffer("")
for (i in 0..selectedStr.size - 1) {
temp.append(selectedStr.get(i))
}
listenner.getCityString(temp)
dismiss()
}
}
}
fun refreshView(step: Int, str: String, goback: Boolean) {
if (step >= 3) {
return
}
for (i in 0..2) {
shadows[i].visibility = View.GONE
}
shadows[step].visibility = View.VISIBLE
if (!goback) {
showtext[step - 1].text = str
}
}
interface AddressSelectFinish {
fun getCityString(str: StringBuffer)
}
}
package com.hotniao.live.bopin.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.hotniao.live.bopin.R;
import java.util.List;
public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.ViewHolder> {
private List<String> cityname;
GetSelectedItemListener getSelectedItemListener;
public void setItemClickListenner(GetSelectedItemListener getSelectedItemListener) {
this.getSelectedItemListener = getSelectedItemListener;
}
public CityListAdapter(List<String> cityname) {
this.cityname = cityname;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.city_eara_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.cityName.setText(cityname.get(position));
holder.cityName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSelectedItemListener.onItemClick(position,holder.cityName);
}
});
}
@Override
public int getItemCount() {
return cityname.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView cityName;
public ViewHolder(View view) {
super(view);
cityName = (TextView) view.findViewById(R.id.city_eara_name);
}
}
public interface GetSelectedItemListener {
void onItemClick(int position, TextView value);
}
}