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

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

更多相关文章

  1. [转]Android(安卓)JNI层实现文件的read、write与seek操作
  2. 【Android(安卓)界面效果32】ImageView中XML属性src和background
  3. Android(安卓)实现书籍翻页效果----原理篇
  4. android使用ExpandableListView控件实现小说目录效果的例子
  5. android 围绕中心旋转动画
  6. android view滑动助手类 OverScroller VelocityTracker
  7. Android获取经纬度、计算距离、方位角
  8. Android(安卓)UI之布局
  9. Android定位功能(一)

随机推荐

  1. Android 点击editview以外位置实现隐藏输
  2. Android 代码扫描工具
  3. Android(安卓)EditView输入内容格式验证
  4. 第十节(Activity布局初步三--相对布局)
  5. 动画的顺序播放
  6. Android之IntentService
  7. SignalR在Android上的实践
  8. android怎么判断是否是模拟器
  9. 单元测试,数据存储
  10. 【转】mac os 平台下载并编译android2.3.