Android实现四级联动地址选择器

本文实例为大家分享了Android实现四级联动地址选择器的具体代码,供大家参考,具体内容如下

效果如下:

代码传送阵:

address_selector.xml

<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:id="@+id/ll_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  android:alpha="0"
  android:background="#b2000000"
  android:orientation="vertical"
  tools:alpha="1">

  <android.support.constraint.ConstraintLayout
    android:id="@+id/cl_container"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selector_bg">

    <TextView
      android:id="@+id/textViewProvince"
      android:layout_width="wrap_content"
      android:layout_height="@dimen/dp_42"
      android:layout_marginStart="@dimen/dp_18"
      android:ellipsize="end"
      android:paddingTop="@dimen/dp_13"
      android:maxWidth="@dimen/dp_80"
      android:maxLines="1"
      android:text="请选择"
      android:textColor="@drawable/selector_item_text_color"
      android:textSize="@dimen/sp_14"
      android:textStyle="bold"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="请选择请选择"
      tools:visibility="visible" />

    <TextView
      android:id="@+id/textViewCity"
      android:layout_width="wrap_content"
      android:layout_height="@dimen/dp_44"
      android:layout_marginStart="@dimen/dp_21"
      android:ellipsize="end"
      android:paddingTop="@dimen/dp_13"
      android:maxWidth="@dimen/dp_80"
      android:maxLines="1"
      android:text="请选择"
      android:textColor="@drawable/selector_item_text_color"
      android:textSize="@dimen/sp_14"
      android:textStyle="bold"
      android:visibility="invisible"
      app:layout_constraintLeft_toRightOf="@id/textViewProvince"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="请选择请选择"
      tools:visibility="visible" />

    <TextView
      android:id="@+id/textViewArea"
      android:layout_width="wrap_content"
      android:layout_height="@dimen/dp_44"
      android:layout_marginStart="@dimen/dp_21"
      android:ellipsize="end"
      android:paddingTop="@dimen/dp_13"
      android:maxWidth="@dimen/dp_80"
      android:maxLines="1"
      android:text="请选择"
      android:textColor="@drawable/selector_item_text_color"
      android:textSize="@dimen/sp_14"
      android:textStyle="bold"
      android:visibility="invisible"
      app:layout_constraintLeft_toRightOf="@id/textViewCity"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="请选择请选择"
      tools:visibility="visible" />

    <TextView
      android:id="@+id/textViewStreet"
      android:layout_width="wrap_content"
      android:layout_height="@dimen/dp_44"
      android:layout_marginStart="@dimen/dp_21"
      android:ellipsize="end"
      android:paddingTop="@dimen/dp_13"
      android:maxWidth="@dimen/dp_80"
      android:maxLines="1"
      android:text="请选择"
      android:textColor="@drawable/selector_item_text_color"
      android:textSize="@dimen/sp_14"
      android:textStyle="bold"
      android:visibility="invisible"
      app:layout_constraintLeft_toRightOf="@id/textViewArea"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="请选择请选择"
      tools:visibility="visible" />

    <android.support.v7.widget.AppCompatImageView
      android:id="@+id/iv_close"
      android:layout_width="@dimen/dp_23"
      android:layout_height="@dimen/dp_23"
      android:layout_marginTop="@dimen/dp_12"
      android:layout_marginEnd="@dimen/dp_12"
      android:padding="@dimen/dp_5"
      android:src="@drawable/address_close"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

    <View
      android:id="@+id/indicator"
      android:layout_width="@dimen/dp_24"
      android:layout_height="@dimen/dp_2"
      android:background="#000000"
      app:layout_constraintLeft_toLeftOf="@id/textViewProvince"
      app:layout_constraintTop_toBottomOf="@id/textViewProvince" />

    <View
      android:id="@+id/divider"
      android:layout_width="match_parent"
      android:layout_height="0.5dp"
      android:background="#e5e5e5"
      app:layout_constraintTop_toBottomOf="@id/indicator" />

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recycler_view"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintTop_toBottomOf="@id/divider" />
  </android.support.constraint.ConstraintLayout>

</LinearLayout>

AddressDialog.java

public class AddressDialog extends DialogFragment {
  @BindView(R.id.textViewProvince)
  TextView textViewProvince;
  @BindView(R.id.textViewCity)
  TextView textViewCity;
  @BindView(R.id.textViewArea)
  TextView textViewArea;
  @BindView(R.id.textViewStreet)
  TextView textViewStreet;
  @BindView(R.id.iv_close)
  AppCompatImageView ivClose;
  @BindView(R.id.indicator)
  View indicator;
  @BindView(R.id.divider)
  View divider;
  @BindView(R.id.recycler_view)
  RecyclerView recyclerView;
  @BindView(R.id.cl_container)
  ConstraintLayout clContainer;
  @BindView(R.id.ll_container)
  LinearLayout llContainer;
  Unbinder unbinder;
  private CompositeDisposable compositeDisposable = new CompositeDisposable();
  private float measuredHeight = 0F;
  // 当前tab 0省1市2区
  private int currentTab = 0;
  // 省
  private ArrayList<County> proviceList = new ArrayList<>();
  // 市
  private ArrayList<County> cityList = new ArrayList<>();
  private ConcurrentHashMap<String, ArrayList<County>> cityMap = new ConcurrentHashMap<>();
  // 区
  private ArrayList<County> areaList = new ArrayList<>();
  private ConcurrentHashMap<String, ArrayList<County>> areaMap = new ConcurrentHashMap<>();
  //街道
  private ArrayList<County> streetList = new ArrayList<>();
  private ConcurrentHashMap<String, ArrayList<County>> streetMap = new ConcurrentHashMap<>();

  private BaseQuickAdapter<County, BaseViewHolder> mAdapter;
  private OnSelectListener listener;
  private String textProvince = "";
  private String textCity = "";
  private String textArea = "";
  private String textStreet = "";
  private ProgressDialog mProgressDialog;
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.address_selector, container, false);
    unbinder = ButterKnife.bind(this, view);
    return view;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, R.style.Style_Dialog_Transparent);
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textViewProvince.setSelected(true);
    float measureText = textViewProvince.getPaint().measureText("请选择");
    indicator.getLayoutParams().width = (int) measureText;
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)clContainer.getLayoutParams();
    layoutParams.topMargin = listener.getHeight() == 0 ? UIUtil.dip2px(MyApplication.applicationContext,180f) :listener.getHeight();
    clContainer.setLayoutParams(layoutParams);
    initData();
    initClick();

    ViewUtils.postMeasured(clContainer, new Runnable() {
      @Override
      public void run() {
        llContainer.animate().alpha(1f).setDuration(150).start();

        float measuredHeight = (float)clContainer.getMeasuredHeight();
        ObjectAnimator translationY = ObjectAnimator.ofFloat(clContainer, "translationY", measuredHeight, 0f);
        translationY.setDuration(150);
        translationY.setInterpolator(new LinearInterpolator());
        translationY.start();
      }
    });

  }

  private void initData() {
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    mAdapter=new BaseQuickAdapter<County, BaseViewHolder>(R.layout.item_area) {
      @Override
      protected void convert(@NonNull BaseViewHolder helper, final County item) {
        helper.setText(R.id.textView,item.name);
        helper.getConvertView().setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            switch (currentTab) {
              case 0:
                getCityList(new Consumer<ArrayList<County>>() {
                  @Override
                  public void accept(ArrayList<County> counties) throws Exception {
                    // 本地是否存在
                    if (cityMap.containsKey(item.code)){
                      ArrayList<County> list = cityMap.get(item.code);
                      if (list==null) {
                        list=new ArrayList<>();
                      }
                      notifyAdapter(list);
                    }else {
                      for (County next : cityList) {
                        if (cityMap.containsKey(item.code) && item.code.equals(next.provinceCode)) {
                          ArrayList<County> list = cityMap.get(item.code);
                          if (list != null) {
                            list.add(next);
                            cityMap.put(next.provinceCode, list);
                          }
                        } else {
                          @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
                          ArrayList<County> listCounty = new ArrayList<>();
                          listCounty.add(next);
                          cityMap.put(next.provinceCode, listCounty);
                        }
                      }
                    }
                    ArrayList<County> list = cityMap.get(item.code);
                    notifyAdapter(list);
                  }
                });
                break;
              case 1:
                getAreaList(new Consumer<ArrayList<County>>() {
                  @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
                  @Override
                  public void accept(ArrayList<County> list) throws Exception {
                    if (areaMap.containsKey(item.code)) {
                      ArrayList<County> arrayList = areaMap.get(item.code);
                      if (arrayList==null) {
                        arrayList =new ArrayList<>();
                      }
                      notifyAdapter(arrayList);
                    }else {
                      for (County county : areaList) {
                        if (areaMap.containsKey(item.code) && item.code.equals(county.cityCode)) {
                          ArrayList<County> countyArrayList = areaMap.get(item.code);
                          if (countyArrayList != null) {
                            countyArrayList.add(county);
                            areaMap.put(county.code, countyArrayList);
                          }
                        }else {
                          ArrayList<County> arrayList = new ArrayList<>();
                          arrayList.add(county);
                          areaMap.put(county.cityCode,arrayList);
                        }
                      }
                    }
                    ArrayList<County> arrayList = areaMap.get(item.code);
                    notifyAdapter(arrayList);
                  }
                });
                break;
              case 2:

                if (mProgressDialog == null) {
                  mProgressDialog = new ProgressDialog(getActivity());
                }

                mProgressDialog.setCanceledOnTouchOutside(false);
                mProgressDialog.setCancelable(true);
                mProgressDialog.show();

                getStreetList(new Consumer<ArrayList<County>>() {
                  @Override
                  public void accept(ArrayList<County> list) throws Exception {

                    if (streetMap.containsKey(item.code)) {
                      ArrayList<County> arrayList = streetMap.get(item.code);
                      if (arrayList==null) {
                        arrayList =new ArrayList<>();
                      }
                      notifyAdapter(arrayList);
                    }else {
                      for (County county : streetList) {
                        if (streetMap.containsKey(item.code) && item.code.equals(county.areaCode)) {
                          ArrayList<County> countyArrayList = streetMap.get(item.code);
                          if (countyArrayList != null) {
                            countyArrayList.add(county);
                            streetMap.put(county.code, countyArrayList);
                          }
                        }else {
                          ArrayList<County> arrayList = new ArrayList<>();
                          arrayList.add(county);
                          streetMap.put(county.areaCode,arrayList);
                        }
                      }
                    }
                    ArrayList<County> arrayList = streetMap.get(item.code);
                    notifyAdapter(arrayList);

                    if (mProgressDialog != null) {
                      ThreadHelper.getInstance().runOnUiPostDelayed(new Runnable() {
                        @Override
                        public void run() {
                          mProgressDialog.dismiss();
                        }
                      },1000);

                    }
                  }
                });
            }

            upTabStatus(item.name);
            indicatorAnim();
          }
        });

      }
    };

    recyclerView.setAdapter(mAdapter);
    getProvinceList(new Consumer<ArrayList<County>>() {
      @Override
      public void accept(ArrayList<County> list) throws Exception {
        notifyAdapter(proviceList);
      }
    });
  }

  private void initClick() {
    ivClose.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        closeDialog();
      }
    });

    llContainer.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        closeDialog();
      }
    });

  }

  private void notifyAdapter(ArrayList<County> list){
    mAdapter.getData().clear();
    mAdapter.addData(list);
  }

  //indicator 动画
  private void indicatorAnim(){
    switch (currentTab){
      case 1:
        float text = textViewProvince.getPaint().measureText(textProvince);
        indicator.animate().translationX(text+UIUtil.dip2pxf(Objects.requireNonNull(getContext()),21f)).setDuration(200).start();
        break;
      case 2:
        float measureText = textViewProvince.getPaint().measureText(textProvince);
        float measureText1 = textViewCity.getPaint().measureText(textCity);
        indicator.animate().translationX(measureText+measureText1+UIUtil.dip2pxf(Objects.requireNonNull(getContext()),42f)).setDuration(200).start();
        break;
      case 3:
        float measure = textViewProvince.getPaint().measureText(textProvince);
        float measure1 = textViewCity.getPaint().measureText(textCity);
        float measure2 = textViewArea.getPaint().measureText(textArea);
        indicator.animate().translationX(measure+measure1+measure2+UIUtil.dip2pxf(Objects.requireNonNull(getContext()),63f)).setDuration(200).start();

    }
  }

  private void upTabStatus(String name){
    switch (currentTab){
      case 0:
        textProvince=name;
        textViewProvince.setText(name);
        textViewCity.setVisibility(View.VISIBLE);
        currentTab =1;
        break;
      case 1:
        textCity=name;
        textViewCity.setText(name);
        textViewArea.setVisibility(View.VISIBLE);
        currentTab =2;
        break;
      case 2:
        textArea=name;
        textViewArea.setText(name);
        textViewStreet.setVisibility(View.VISIBLE);
        currentTab=3;
        break;
      case 3:
        textStreet=name;
        textViewStreet.setText(name);
        dismissAllowingStateLoss();
        break;

    }
    textViewProvince.setSelected(currentTab==0);
    textViewCity.setSelected(currentTab==1);
    textViewArea.setSelected(currentTab==2);
    textViewStreet.setSelected(currentTab==3);
  }

  private void getProvinceList(Consumer<ArrayList<County>> consumer){
    Disposable subscribe1 = Observable.create((new ObservableOnSubscribe() {
      public final void subscribe(@NonNull ObservableEmitter it) {
        if (proviceList.isEmpty()) {
          String json = getJson("address/provinces.json", getContext());
          ArrayList<County> list = fromJson(json,new TypeToken<ArrayList<County>>(){}.getType());
          if (list==null) {
            list =new ArrayList<>();
          }
          proviceList.addAll(list);
        }

        if (!proviceList.isEmpty()) {
          it.onNext(proviceList);
        }
      }
    })).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(consumer, new Consumer<Throwable>() {
      @Override
      public void accept(Throwable throwable) throws Exception {

      }
    });
    compositeDisposable.add(subscribe1);
  }

  private void getCityList(Consumer<ArrayList<County>> consumer){
    Disposable subscribe1 = Observable.create((new ObservableOnSubscribe() {
      public final void subscribe(@NonNull ObservableEmitter it) {
        if (cityList.isEmpty()) {
          String json = getJson("address/cities.json", getContext());
          ArrayList<County> list = fromJson(json,new TypeToken<ArrayList<County>>(){}.getType());
          if (list==null) {
            list =new ArrayList<>();
          }
          cityList.addAll(list);
        }

        if (!cityList.isEmpty()) {
          it.onNext(cityList);
        }
      }
    })).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(consumer, new Consumer<Throwable>() {
      @Override
      public void accept(Throwable throwable) throws Exception {

      }
    });

    compositeDisposable.add(subscribe1);
  }

  private void getAreaList(Consumer<ArrayList<County>> consumer){
    Disposable subscribe1 = Observable.create((new ObservableOnSubscribe() {
      public final void subscribe(@NonNull ObservableEmitter it) {
        if (areaList.isEmpty()) {
          String json = getJson("address/areas.json", getContext());
          ArrayList<County> list = fromJson(json,new TypeToken<ArrayList<County>>(){}.getType());
          if (list==null) {
            list =new ArrayList<>();
          }
          areaList.addAll(list);
        }

        if (!areaList.isEmpty()) {
          it.onNext(areaList);
        }
      }
    })).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(consumer, new Consumer<Throwable>() {
      @Override
      public void accept(Throwable throwable) throws Exception {

      }
    });
    compositeDisposable.add(subscribe1);
  }

  private void getStreetList(Consumer<ArrayList<County>> consumer){
    Disposable subscribe1 = Observable.create((new ObservableOnSubscribe() {
      public final void subscribe(@NonNull ObservableEmitter it) {
        if (streetList.isEmpty()) {
          String json = getJson("address/streets.json", getContext());
          ArrayList<County> list = fromJson(json,new TypeToken<ArrayList<County>>(){}.getType());
          if (list==null) {
            list =new ArrayList<>();
          }
          streetList.addAll(list);
        }

        if (!streetList.isEmpty()) {
          it.onNext(streetList);
        }
      }
    })).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(consumer, new Consumer<Throwable>() {
      @Override
      public void accept(Throwable throwable) throws Exception {

      }
    });
    compositeDisposable.add(subscribe1);

  }

  public String getJson(String fileName, Context context) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
      InputStream is = context.getAssets().open(fileName);
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    return stringBuilder.toString();
  }

  @Nullable
  public <T> T fromJson(String json, Type typeOfT) {
    try {
      return initGson().fromJson(json, typeOfT);
    } catch (JsonSyntaxException e) {
      e.printStackTrace();
    }
    return null;
  }

  private Gson initGson(){
    GsonBuilder gsonBuilder = new GsonBuilder();
    try {
      Class builder = gsonBuilder.getClass();
      Field f = builder.getDeclaredField("instanceCreators");
      f.setAccessible(true);
      Map<Type, InstanceCreator<?>> val = (Map<Type, InstanceCreator<?>>) f.get(gsonBuilder);//得到此属性的值
      //注册数组的处理器
      ConstructorConstructor constructorConstructor = new ConstructorConstructor(val);
//      gsonBuilder.registerTypeAdapterFactory(new ReflectiveTypeAdapterFactory(constructorConstructor, FieldNamingPolicy.IDENTITY,
//          Excluder.DEFAULT,new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor)));
      gsonBuilder.registerTypeAdapterFactory(new MapTypeAdapterFactory(constructorConstructor,false));
      gsonBuilder.registerTypeAdapterFactory(new CollectionTypeAdapterFactory(constructorConstructor));
//      gsonBuilder.registerTypeAdapter(String.class,new StringTypeAdapter());
      gsonBuilder.registerTypeAdapter(int.class,new IntegerTypeAdapter());
      gsonBuilder.registerTypeAdapter(Integer.class,new IntegerTypeAdapter());
//      gsonBuilder.registerTypeAdapter(long.class,new LongTypeAdapter());
//      gsonBuilder.registerTypeAdapter(Long.class,new LongTypeAdapter());
//      gsonBuilder.registerTypeAdapter(double.class,new DoubleTypeAdapter());
//      gsonBuilder.registerTypeAdapter(Double.class,new DoubleTypeAdapter());
//      gsonBuilder.registerTypeAdapter(boolean.class,new BooleanTypeAdapter());
//      gsonBuilder.registerTypeAdapter(Boolean.class,new BooleanTypeAdapter());
//      gsonBuilder.registerTypeAdapter(float.class,new FloadTypeAdapter());
//      gsonBuilder.registerTypeAdapter(Float.class,new FloadTypeAdapter());

    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return gsonBuilder.enableComplexMapKeySerialization().create();
  }

  public void closeDialog() {
      clContainer.animate().translationY(measuredHeight).setDuration(150).withEndAction(new Runnable() {
        @Override
        public void run() {
          dismissAllowingStateLoss();
        }
      }).start();
  }

  public void dismissAllowingStateLoss() {
    super.dismissAllowingStateLoss();
    if (!TextUtils.isEmpty(textProvince) && !TextUtils.isEmpty(textCity) && !TextUtils.isEmpty(textArea)&&!TextUtils.isEmpty(textStreet)) {
      listener.onSelect(textProvince, textCity, textCity,textStreet);
    }
    listener.isMissing();
    compositeDisposable.dispose();
  }

  public void setOnSelectListener(OnSelectListener listener) {
    this.listener = listener;
  }

  public void setHeight(int top) {

  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }

  public interface OnSelectListener {
    int getHeight();

    void onSelect(String province, String city, String area,String street);

    void isMissing();
  }

}

使用方法

private void addressDialog() {
    AddressDialog addressDialog =new AddressDialog();
    addressDialog.setOnSelectListener(new AddressDialog.OnSelectListener() {
      @Override
      public int getHeight() {
        return 0;
      }

      @SuppressLint("SetTextI18n")
      @Override
      public void onSelect(String province, String city, String area, String street) {
        tvPositionRtrCommunity.setText(province+" "+city+" "+area+" "+street);
      }

      @Override
      public void isMissing() {
        isShowDialog = true;
      }
    });
    addressDialog.show(getSupportFragmentManager(),AddressDialog.class.getSimpleName());
}

资源下载:四级联动地址选择器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android仿手机通讯录地址选择功能

    感觉比较好的一个地址选择设计,而且发现有的App中也用到了.还是先上效果图 思路: 1.效果是仿照网上大神实现的类似通讯录样式做的: 2.右边a-z是自定义的一个bar,设置了点击监听事件,以及对话框弹出 3.关键是adapter,判断了字母显示和隐藏 4.用到汉字转拼音.按首字母排序等工具类 5.3个activity的跳转是用回调来实现,每个activity都实现了回调,这样就有了从区activity直接跳转到首页的效果 6.数据是调用的我本地的接口实现的,如果大家没有数据我可以想办法给你们提

  • Android实现四级联动地址选择器

    本文实例为大家分享了Android实现四级联动地址选择器的具体代码,供大家参考,具体内容如下 效果如下: 代码传送阵: address_selector.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://s

  • 微信小程序三级联动地址选择器的实例代码

    本文介绍了微信小程序三级联动地址选择器的实例代码,分享给大家,有需要的可以一起了解一下 在一些电商类的小程序中,地址选择这个功能一般是必备的,一般的收货信息都需要有一个能选择省市县的控件,当然也有些人为了省事就直接写了一个供输入的input,那么这样做的缺点不言而喻,而且用户体验也不是那么的好,今天的这篇文章就分享一下微信小程序地址选择的实现.省市县的数据以及区域码可以从国家统计局查询到,具体可以自己搜一下.照例先上源码和效果图 源码传送门 picker和picker-view组件 在正式介绍实

  • Android实现拼多多地址选择器

    本文实例为大家分享了Android实现拼多多地址选择器的具体代码,供大家参考,具体内容如下 突然想做一款地区选择器,然后我的弹框用的第三方的,地区数据用的是本地的json文件,解析json文件,然后把数据放在list集合里面,然后设置到弹框里面,问题解决 源码下载地址 1.效果图 2.引用builder implementation 'com.google.code.gson:gson:2.8.0' implementation 'com.contrarywind:Android-PickerV

  • Android省市区三级联动控件使用方法实例讲解

    最近有需求需要实现省市区三级联动,但是发现之前的实现不够灵活,自己做了一些优化.为了方便以后使用,抽离出来放在了github上WheelView.同时把其核心库放在了JCenter中了,可以直接引用.也可以参考项目中的Demo进行引用 下面介绍一下如何使用 如果用的是AndroidStudio那么直接在build.gradle文件中添加依赖: dependencies { compile 'chuck.WheelItemView:library:1.0.1' } 成功引入库之后,可以在需要弹出省

  • Android 高仿QQ图片选择器

    当做一款APP,需要选择本地图片时,首先考虑的无疑是系统相册,但是Android手机五花八门,再者手机像素的提升,大图无法返回等异常因数,导致适配机型比较困难,微信.QQ都相继的在自己的APP里集成了图片选择功能,放弃了系统提供的图片选择器,这里仿造QQ做了一个本地图片选择器,PS:之前有人说"仿"写成"防"了,今儿特意注意了下,求不错. 先上一张效果图,无图无真相啊~~~ 实现的效果大概是这样的: 1.单选:跳转到本地图片选择文件夹,选择文件夹后,进入到该文件夹下

  • Android仿IOS10圆盘时间选择器

    介绍 这是一款仿IOS10(就寝功能)的圆盘时间选择器 项目演示 实现思路 以720度为一个周期,0~360°对应0~12小时,360°~720°对应12~24小时 这里以"开始时间设置按钮"为例来谈谈它的滑动实现: 将"开始时间设置按钮"作为点A,表盘中心作为点O,手指触摸点作为点P.通过反正切公式可以计算出∠AOP的大小,然后随着手指的位置不断变化去更新点A的位置(即点A的角度). // 坐标系的直线表达式 // 直线l1的表达式子:过钟表中心点和开始控件中心点

  • Android 中TeaPickerView数据级联选择器功能的实例代码

    Github地址 YangsBryant/TeaPickerView (Github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!) 引入module allprojects { repositories { google() jcenter() maven { url 'https://www.jitpack.io' } } } implementation 'com.github.YangsBryant:TeaPickerView:1.0.2' 主要代码 public cla

  • Android实现微信的图片选择器

    现在大部分的App都上传图片的功能,比如设置用户头像.聊天发送图片.发表动态.论坛帖子等.上传图片需要先从选择手机中选择要上传的图片,所以图片选择器在App中是很常见的组件,一般的手机都会自带一个图片选择器.不过很多App并不喜欢用手机自带的选择器,而是自己实现一个图片选择器.比如微信的图片选择器就做的很好.所以我也仿照微信的样式和交互效果,自己做了一个图片选择器:ImageSelector.ImageSelector支持图片的单选.限数量的多选和不限数量的多选.支持图片预览和图片文件夹的切换.

  • jQuery插件实现适用于移动端的地址选择器

    最近在工作中需要用到地址选择器,像下面这样的,本想在网上找一个,可是没找到,于是自己写了个jquery插件. 直接上代码吧: var provinces = { "A": { "安徽":["合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市&qu

  • Android编程中selector背景选择器用法实例分析

    本文实例讲述了Android编程中selector背景选择器用法.分享给大家供大家参考,具体如下: 在Android开发过程中,经常对某一View的背景在不同的状态下,设置不同的背景,增强用户体验.如果按钮,在按下时,背景变化,如果在代码中动态设置,相对比较麻烦.Android为我们提供了selector背景选择器可以非常方便的解决这一问题. Selector的结构描述: 1.android:state_pressed="true/false" true:表示按下状态下使用,false

随机推荐