项目中采用了三种样式城市选择(无语....),第一种是:
地址:https://github.com/yanxing/android-util sortlistviewlibrary
第二种是:
第三种是:
我用两个水平的ListView实现的(可以用ListFragment,也可以两个Fragment),布局代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ListView android:id="@+id/province" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:divider="@color/line_xx" android:dividerHeight="1px" android:layout_weight="2" android:choiceMode="singleChoice" android:scrollbars="none"/> <ListView android:id="@+id/city" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" android:divider="@color/line_xx" android:dividerHeight="1px" android:layout_weight="1" android:scrollbars="none"/> </LinearLayout>ListView需要单选模式
android:choiceMode="singleChoice"
Activity代码:
@AfterViews @Override protected void afterInstanceView() { //构造当前地区 Area currentArea=new Area(); currentArea.setName("当前地区"); ArrayList<Area.CityBean> currentCity=new ArrayList<Area.CityBean>(); Area.CityBean cityBean=new Area.CityBean(); String currentCityStr=getIntent().getStringExtra("currentCity"); cityBean.setName(currentCityStr); currentCity.add(cityBean); currentArea.setCity(currentCity); mAreaList.add(currentArea); mAreaList.addAll(ParseJson.getArea(getApplicationContext())); mProvinceAdapter=new ProvinceAdapter(); mProvince.setAdapter(mProvinceAdapter); mProvince.setOnItemClickListener(this); new Handler().postDelayed(() -> { //选中第一项 mProvince.performItemClick(mProvince.getChildAt(0),0,mProvince.getItemIdAtPosition(0)); },700); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mIsClick=true; mIndex=position; for (int i=0;i<mProvince.getChildCount();i++){ if (mIndex!=i){ View view1=mProvince.getChildAt(i); view1.findViewById(R.id.current).setVisibility(View.GONE); } } view.findViewById(R.id.current).setVisibility(View.VISIBLE); if (mCityAdapter==null){ mCityAdapter=new CommonAdapter<Area.CityBean>(mAreaList.get(0).getCity(),R.layout.adapter_city) { @Override public void onBindViewHolder(ViewHolder holder, int index) { if (mIndex==0){ holder.findViewById(R.id.current).setVisibility(View.VISIBLE); }else { holder.findViewById(R.id.current).setVisibility(View.GONE); } holder.setText(R.id.city,mAreaList.get(mIndex).getCity().get(index).getName()); } }; mCity.setAdapter(mCityAdapter); }else { mCityAdapter.update(mAreaList.get(mIndex).getCity()); } }
ListView子View布局背景选择器代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/white"/> <item android:state_selected="true" android:drawable="@color/white"/> <item android:state_activated="true" android:drawable="@color/white"/> <item android:drawable="@color/list_line"/> </selector>一开始我没有采用遍历的方式(上面红色那段代码),而是子View中的ImageView 用选择器,代码如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@mipmap/current"/> <item android:state_selected="true" android:drawable="@mipmap/current"/> <item android:state_activated="true" android:drawable="@mipmap/current"/> </selector>但是发现两个bug,一是:ListView选中ItemClick其他的子View中的 没有GONE。二是上、下拉ListView选中的Item从可见-》不可见-》可见, 就不可见了。最后放弃了这种方式,采用遍历的方式。