Android(安卓)仿淘宝2018添加地址
16lz
2021-01-24
先来看看淘宝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); }}
代码太多久不一一写出来了,这里附上源码,了解下
更多相关文章
- [转]Android(安卓)JNI层实现文件的read、write与seek操作
- 【Android(安卓)界面效果32】ImageView中XML属性src和background
- Android(安卓)实现书籍翻页效果----原理篇
- android使用ExpandableListView控件实现小说目录效果的例子
- android 围绕中心旋转动画
- android view滑动助手类 OverScroller VelocityTracker
- Android获取经纬度、计算距离、方位角
- Android(安卓)UI之布局
- Android定位功能(一)