Android进阶之路 - 仿京东地址选择器的详细使用

因为现在项目的地址选择器比较low,自己又比较懒,所以就找到了仿京东的地址选择器,观赏几篇博文之后,发现总是被半路卡死,很难一路走到底,所以找到了作者在github的地址,但是作者的功能简介又介绍的不是很详细,总会有一些小的知识点遗漏,到了最后只能非常无奈的查看了源代码,然后实现了自己的功能所需,一些小坑在下方列出,请君观赏。

小坑:

  • 文中的展示有俩种方式,第一种是自定义添加到Linearlayout布局中,第二种是以弹框形式展示
  • 因作者原始自定义BottomDialog的整体展示不太美观,而代码又限制只又读的权限,故重写了一份BottomDialog,设置了符合自己观赏的BottomDialog
  • android中存在一些自定义的color,使用android.R.color.holo_orange_light进行引用

需掌握知识点:

  • 接口回调
  • RecyclerView、ListView
  • 自定义Dialog

Effect :

第一种
Android进阶之路 - 仿京东地址选择器的详细使用_第1张图片

第二种
Android进阶之路 - 仿京东地址选择器的详细使用_第2张图片

注:
原作者Github地址:https://github.com/smartTop/AddressSelector


无法掌握本文知识,可使用以下这篇博文的地址选择器(使用更简单)

http://blog.csdn.net/qq_20451879/article/details/71450981

build (project) :

allprojects {
    repositories {
        jcenter()
        //这个写不写都可以,我注释之后依旧可以正常使用

//        maven { url 'https://jitpack.io' }
    }
}

build (app):

compile 'com.smarttop:jd-address:1.1.0'

MainActivity :

package com.yl.select.addressselect;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.db.manager.AddressDictManager;
import com.smarttop.library.utils.LogUtil;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.OnAddressSelectedListener;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnAddressSelectedListener, AddressSelector.OnDialogCloseListener {

    private LinearLayout mViewcontent;
    private TextView mSelectorContent;
    private AddressDictManager addressDictManager;
    private String provinceCode;
    private String cityCode;
    private String countyCode;
    private String streetCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSelectorContent = (TextView) findViewById(R.id.tv_selector_area);
        mViewcontent = (LinearLayout) findViewById(R.id.content);

        AddressSelector selector = new AddressSelector(this);
        mSelectorContent.setOnClickListener(this);
        addressDictManager = selector.getAddressDictManager();
        selector.setTextSize(13);

        //设置指针颜色
        selector.setIndicatorBackgroundColor(android.R.color.holo_orange_light);
        //设置字体获得焦点的颜色
        selector.setTextSelectedColor(android.R.color.holo_orange_light);
        //设置字体没有获得焦点的颜色
        selector.setTextUnSelectedColor(android.R.color.holo_blue_light);

        //选择监听,用户选取中和选取完毕之后调用此方法
        selector.setOnAddressSelectedListener(new OnAddressSelectedListener() {
            @Override

            public void onAddressSelected(Province province, City city, County county, Street street) {

                String address = (province == null ? "" : province.name) + (city == null ? "" : city.name) + (county == null ? "" : county.name) +
                        (street == null ? "" : street.name);
//                mSelectorContent.setVisibility(View.VISIBLE);
                mSelectorContent.setText(address);
//                mViewcontent.removeAllViews();
            }
        });

//        如将此处的注释放开,即可全屏显示一次
//        View view = selector.getView();
//        mViewcontent.addView(view);
//        mSelectorContent.setVisibility(View.GONE);
    }

    private BottomDialog dialog;

    @Override
    public void onClick(View v) {
        if (dialog != null) {
            dialog.show();
        } else {
            dialog = new BottomDialog(this);
            //地址选取监听
            dialog.setOnAddressSelectedListener(this);
            //Dialog监听
            dialog.setDialogDismisListener(this);
            //设置字体的大小
            dialog.setTextSize(14);
            //设置指示器的颜色
            dialog.setIndicatorBackgroundColor(android.R.color.holo_orange_light);
            //设置字体获得焦点的颜色
            dialog.setTextSelectedColor(android.R.color.holo_orange_light);
            //设置字体没有获得焦点的颜色
            dialog.setTextUnSelectedColor(android.R.color.holo_blue_light);
            dialog.show();
        }
    }


    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        provinceCode = (province == null ? "" : province.code);
        cityCode = (city == null ? "" : city.code);
        countyCode = (county == null ? "" : county.code);
        streetCode = (street == null ? "" : street.code);

        String address = (province == null ? "" : province.name) + " " + (city == null ? "" : city.name) + " " + (county == null ? "" : county.name) + " " + (street == null ? "" : street.name);

        mSelectorContent.setText("地址 :" + " " + address);

        if (dialog != null) {
            dialog.dismiss();
        }
//        getSelectedArea();
    }

    @Override
    public void dialogclose() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 根据code 来显示选择过的地区
     */

    private void getSelectedArea() {
        String province = addressDictManager.getProvince(provinceCode);
        String city = addressDictManager.getCity(cityCode);
        String county = addressDictManager.getCounty(countyCode);
        String street = addressDictManager.getStreet(streetCode);
    }
}

自定义BottomDialog(这里只修改了一下展示的高度) :

package com.yl.select.addressselect;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.smarttop.library.utils.Dev;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.OnAddressSelectedListener;

/**
 * Created by YongLiu on 2017/7/26.
 */

public class BottomDialog extends Dialog {
    private AddressSelector selector;

    public BottomDialog(Context context) {
        super(context, com.smarttop.library.R.style.bottom_dialog);
        init(context);
    }

    public BottomDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    public BottomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        selector = new AddressSelector(context);
        setContentView(selector.getView());

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = Dev.dp2px(context, 350);
        window.setAttributes(params);

        window.setGravity(Gravity.BOTTOM);
    }

    public void setOnAddressSelectedListener(OnAddressSelectedListener listener) {
        this.selector.setOnAddressSelectedListener(listener);
    }

    public static BottomDialog show(Context context) {
        return show(context, null);
    }

    public static BottomDialog show(Context context, OnAddressSelectedListener listener) {
        BottomDialog dialog = new BottomDialog(context, com.smarttop.library.R.style.bottom_dialog);
        dialog.selector.setOnAddressSelectedListener(listener);
        dialog.show();

        return dialog;
    }
    public void setDialogDismisListener(AddressSelector.OnDialogCloseListener listener){
        this.selector.setOnDialogCloseListener(listener);
    }
    /**
     *设置字体选中的颜色
     */
    public void setTextSelectedColor(int selectedColor){
        this.selector.setTextSelectedColor(selectedColor);
    }
    /**
     *设置字体没有选中的颜色
     */
    public void setTextUnSelectedColor(int unSelectedColor){
        this.selector.setTextUnSelectedColor(unSelectedColor);
    }
    /**
     * 设置字体的大小
     */
    public void setTextSize(float dp){
        this.selector.setTextSize(dp);
    }
    /**
     * 设置字体的背景
     */
    public void setBackgroundColor(int colorId){
        this.selector.setBackgroundColor(colorId);
    }
    /**
     * 设置指示器的背景
     */
    public void setIndicatorBackgroundColor(int colorId){
        this.selector.setIndicatorBackgroundColor(colorId);
    }
    /**
     * 设置指示器的背景
     */
    public void setIndicatorBackgroundColor(String color){
        this.selector.setIndicatorBackgroundColor(color);
    }
}

MainActivity Xml :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.yl.select.addressselect.MainActivity">


    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/tv_selector_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="选择地区" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    LinearLayout>
LinearLayout>

你可能感兴趣的:(#,三方框架)