Android 仿淘宝2018添加地址

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30552993/article/details/80184143

先来看看淘宝2018的添加收货地址的效果

这里写图片描述

分析实现的原理: bottomDialog=tabLayout+RecyclerView

其中的2个特点:

1、tablayout的动态添加的,并且选中item之后会自动跳入下一个tab
2、RecyclerView选中之后,下次选中对应的tab会定位的RecyclerView指定的位置。(比如:选中广州市tab,下面的RecyclerView定位到广州市)

这里的地址数据来自:2018最新area.json(智行火车票)

项目实现的效果:
这里写图片描述

其中BottomDialog.class:

/**
 * Description :
 *
 * @author WSoBan
 * @date 2018/05/03
 */
public class BottomDialog extends Dialog {

    private OnSelectedResultCallBack resultCallBack;
    private LayoutBottomSheetDialogBinding mDialogBinding;

    private AreaAdapter mAdapter;
    private Map<Integer, AreaBean> currentMap = new TreeMap<>();

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

    private void init(Context context) {
        mDialogBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                R.layout.layout_bottom_sheet_dialog, null, false);
        setContentView(mDialogBinding.getRoot());
        initView();

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = DensityUtils.dp2px(context, 400);
        window.setAttributes(params);

        window.setGravity(Gravity.BOTTOM);
    }

    private void initView() {
        mDialogBinding.ivClose.setOnClickListener(v -> dismiss());

        mAdapter = new AreaAdapter(R.layout.item_area);
        mAdapter.setOnSelectedListener((map, pos) -> {
            if (pos >= 2) {
                if (resultCallBack != null) {
                    resultCallBack.onResult(currentMap.get(pos).getNames());
                }
                dismiss();
            } else {
                currentMap = map;
                mDialogBinding.tlTitle.removeAllTabs();
                for (Integer in : map.keySet()) {
                    mDialogBinding.tlTitle.addTab(
                            mDialogBinding.tlTitle.newTab().setText(map.get(in).getName()));
                }
                addChooseTab();
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mDialogBinding.rv.setLayoutManager(manager);
        mDialogBinding.rv.addItemDecoration(new LineAreaItemDecoration(getContext(), 2));
        mDialogBinding.rv.setAdapter(mAdapter);

        mDialogBinding.tlTitle.setTabMode(TabLayout.MODE_SCROLLABLE);
        mDialogBinding.tlTitle.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                if (pos == 0) {
                    mAdapter.setData(pos, AreaParser.getInstance().getProvinceList());
                } else {
                    mAdapter.setData(pos, AreaParser.getInstance().
                                getChildList(currentMap.get(pos - 1).getTid()));
                }
                //移动到指定位置
                mAdapter.moveToPosition(manager);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        addChooseTab();
    }

    private void addChooseTab() {
        mDialogBinding.tlTitle.addTab(mDialogBinding.tlTitle.newTab().setText("请选择"), true);
        TabLayoutUtil.reflex(mDialogBinding.tlTitle);
    }

    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);
    }

    public void setResultCallBack(OnSelectedResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    public interface OnSelectedResultCallBack {

        void onResult(String result);
    }
}

代码太多久不一一写出来了,这里附上源码,了解下

猜你喜欢

转载自blog.csdn.net/qq_30552993/article/details/80184143