Android 仿淘宝2018添加地址

Posted BandaYung

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 仿淘宝2018添加地址相关的知识,希望对你有一定的参考价值。

先来看看淘宝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);
    

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

以上是关于Android 仿淘宝2018添加地址的主要内容,如果未能解决你的问题,请参考以下文章

仿天猫淘宝的ShopNC好商城原生Android 客户端源码项目

Android中仿淘宝商品详情ViewPager页面数据手动滑动

Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

vue仿淘宝地址选择组件