版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wdx_1136346879/article/details/89228667
-
淘宝城市选择编写
代码详细如下:主要逻辑内容在这个类中AreaSelectorDialog.class
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
android:id="@+id/rlTopTip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="配送至"
android:layout_centerInParent="true"
android:textSize="@dimen/text_size_16"
android:textColor="@color/text_color_light"
android:layout_margin="@dimen/space_10"/>
<ImageView
android:id="@+id/ivAreaClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location_close_dia"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:padding="@dimen/space_10"/>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_input_normal"
app:tabTextColor="@color/text_color_light"
app:tabSelectedTextColor="@color/text_color_light"
app:tabMode="scrollable"
app:tabIndicatorColor="@color/color_orange"
>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择"
/>
</android.support.design.widget.TabLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/space_20"/>
TabLayout是动态添加和减少的
//添加一个条目--->请选择前面一个--->tab总数-2
private void addItem(int position) {
TabLayout.Tab tab = tab_layout.newTab();
tab.setTag(listBeans.get(position));
tab.setText(listBeans.get(position).getName());
TLog.INSTANCE.i("加之前tab的个数" + tab_layout.getTabCount());
tab_layout.addTab(tab, tab_layout.getTabCount() - 1);
TLog.INSTANCE.i("加之后tab的个数" + tab_layout.getTabCount());
getAreaList(listBeans.get(position), false);
setTabSelect();
}
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() != tab_layout.getTabCount() - 1) {
if (tab.getTag() == null) {
getAreaList(null, false);
} else {
//获取父级列表
getAreaList((AddressAreaEntity.ListBean) tab.getTag(), true);
}
}
//点击请选择,并且条目大于一个
else if (tab_layout.getTabCount() > 1) {
getAreaList((AddressAreaEntity.ListBean) tab_layout.getTabAt(tab_layout.getTabCount() - 2).getTag(), false);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
ivAreaClose.setOnClickListener(v -> dismiss());
}
recycleview是动态显示城市列表,由接口控制,及上一级城市code