幾行代碼實現ListView的多級聯動——多級聯動就是如此簡單

一二三四級聯動,ListView聯動,城市選擇聯動,SQLite輕量級數據庫,城市數據庫!!!android

效果以下:
git

多級聯動
多級聯動

項目介紹

  • 一二三四級聯動,ListView聯動,城市選擇聯動,SQLite輕量級數據庫,城市數據庫!!!
  • 基於項目本地的資源文件,SQLite輕量級數據庫實現的城市選擇器,數據庫中城市數據基本齊全。
  • 本Demo是使用的ListView實現的聯動,固然也可以使用Fragment,在這裏我只給出了一種方式。
  • 思路是融匯貫通的,我看網上大多都是三級聯動,因此在這裏給出一個好久以前寫的四級聯動,但願可以幫助到你們。
  • 注意:本Demo並不侷限於數據庫使用,若是您可以理解該思路&邏輯,使用json等其餘方式均可實現更多級的聯動。
/**
     * 設置省
     */
    private void setProvince() {
        provinceValues=getProvince(countryValues.get(countryPosition).getPlaceid());
        if(!(provinceValues.isEmpty())){
            provinceAdapter=new LevelListViewAdapter(this, provinceValues);
            provinceAdapter.setSelectedPositionNoNotify(provincePosition, provinceValues);
            lv_province.setAdapter(provinceAdapter);
            provinceAdapter.setOnItemClickListener(new LevelListViewAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if(provinceNumber!=position){//記錄不是當前點擊的
                        provinceNumber=position;//就記錄當前條目
                        provinceTime=System.currentTimeMillis();//並記錄第一次時間戳
                        Timer timer=new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                provinceNumber=-1;
                                provinceTime=0;
                            }
                        }, 300);
                    }else{//記錄的是當前點擊的
                        long num = System.currentTimeMillis()-provinceTime;//判斷時間差,是否是雙擊
                        if(num<=300){//時間差200毫秒內
                            ToastUtil.showToast(MainActivity.this, provinceValues.get(position).getPlacename());
                        }
                        provinceNumber=-1;//重置過的記錄
                        provinceTime=0;//重置時間的記錄
                    }
                    cityValues.clear();
                    if(!(provinceValues.isEmpty())){
                        cityValues=getCity(provinceValues.get(position).getPlaceid());
                        cityAdapter.notifyDataSetChanged();
                        cityAdapter.setSelectedPositionNoNotify(0, cityValues);
                        lv_city.smoothScrollToPosition(0);
                    }else{
                        cityAdapter.notifyDataSetChanged();
                    }
                }
            });}
    }複製代碼

設置國和州,省的方式是同樣的,這裏不在貼出
佈局很簡單:github

...
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/lv_continent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ffffff"
            android:cacheColorHint="#00000000"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:scrollbars="none" >
        </ListView>
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/lv_country"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:cacheColorHint="#00000000"
                android:divider="@null"
                android:dividerHeight="0dp"
                android:scrollbars="none" >
            </ListView>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#c8c8c8" />
        </LinearLayout>
......複製代碼

參考雙聯動ListView--相似外賣點餐:數據庫

github.com/wjie2014/Do…json

github項目:bash

github.com/QQ986945193…微信

相信本身,沒有作不到的,只有想不到的

若是你以爲此文對您有所幫助,歡迎入羣 QQ交流羣 :644196190
微信公衆號:終端研發部 ide

技術+職場
技術+職場
相關文章
相關標籤/搜索