android 城市列表

项目中采用了三种样式城市选择(无语....),第一种是:

地址: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从可见-》不可见-》可见, 就不可见了。最后放弃了这种方式,采用遍历的方式。

完整代码地址:SelectCityActivity


更新:今天发现上面的思路太费事了,需要遍历listview子布局,而且view没有复用。修改为在Area类添加一个布尔型的check变量,标记当前item是否被选中,然后点击时调用notifyDataSetChanged方法就行了,代码已更新。




Android中,城市二级列表联动是指根据用户在一级列表中所选中的省份,动态显示对应的二级子列表中的城市。这样的功能在很多应用中都会用到,比如选择所在城市、选择配送地址等。 要实现城市二级列表联动,首先我们需要准备城市数据。可以通过使用城市数据库或者本地存储文件的方式来获取城市数据,包括省和市的名称。根据这些数据,我们可以构建一个二维的数据结构,使得每一个省份对应一个城市列表。 接下来,在Android中可以将一级列表使用RecyclerView或者ListView来展示,通过适配器将省份数据绑定到列表上。当用户点击某个省份时,可以通过监听点击事件获取到省份的位置信息,根据位置信息获取对应的城市列表数据。 然后,我们可以将二级城市列表数据展示在另一个RecyclerView或者ListView上。同样,需要使用适配器将城市数据绑定到二级列表上。通过刷新适配器的方式实现二级列表的动态更新。 最后,我们还需要处理联动的逻辑。当用户点击一级列表的省份时,我们需要根据省份的位置信息获取对应的城市列表数据,并将城市列表数据绑定到二级列表的适配器上。这样,在界面上就会实现二级列表的联动效果。 在实现过程中需要考虑的一些问题有:如何获取并加载城市数据、如何处理列表的点击事件、如何实现二级列表的动态更新等。总之,通过使用RecyclerView或者ListView以及相应的适配器,我们可以比较容易地实现Android城市二级列表联动的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值