阅读 35

android仿IOS的滚轮省市二级联动

实现IOS的省市二级联动

Github地址:https://github.com/lvlife/gangedCitys

实现效果

在这里插入图片描述

代码实现

特别说明 省市的json数据存放在assets目录下 读取本地数据

主要代码MainActivity

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String ENCODING = "UTF-8";
    private static final String TAG = "MainActivity";

    private Button mButton;
    private Area mArea;
    private ArrayList<Area> mDatas;
    private ListView mListView, mListView1;
    private MainAdapter mMainAdapter, mMainAdapter2;
    private ArrayList<Area> provinceName;
    private ArrayList<Area> citysName;
    private PopupWindow mCityPopupWindow;
    private String aa, bb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        provinceName = new ArrayList<>();
        mDatas = new ArrayList<>();
        citysName = new ArrayList<>();
        mButton = this.findViewById(R.id.button);
        initPopWindowForCitys();
        mButton.setOnClickListener(this);
    }

    private void initPopWindowForCitys() {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View layout = inflater.inflate(R.layout.wheelpopupforcity, null, false);
        mListView = layout.findViewById(R.id.list);
        mListView1 = layout.findViewById(R.id.list1);
        layout.getBackground().setAlpha(130);
        //更新UI
        layout.invalidate();
        mCityPopupWindow = new PopupWindow(layout, LinearLayout.LayoutParams.FILL_PARENT, 
        LinearLayout.LayoutParams.FILL_PARENT);
        Button btn_city_canle = layout.findViewById(R.id.pickcitycancle);
        Button pickcityconfirm = layout.findViewById(R.id.pickcityconfirm);
        btn_city_canle.getPaint().setFakeBoldText(true);
        btn_city_canle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mCityPopupWindow.dismiss();
                mCityPopupWindow.setFocusable(false);
            }
        });
        pickcityconfirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String cc = aa + bb;
                mCityPopupWindow.dismiss();
                mCityPopupWindow.setFocusable(false);
                Toast.makeText(MainActivity.this, cc, Toast.LENGTH_LONG).show();
            }
        });
        String str1 = getFromAssets("leibie.json");
        JSONObject js;
        try {
            js = new JSONObject(str1);
            String areaBeans = js.getString("areaBeans");
            JSONArray areaBean = new JSONArray(areaBeans);
            for (int i = 0; i < areaBean.length(); i++) {
                mArea = new Area();
                JSONObject json = (JSONObject) areaBean.get(i);
                mArea.setAreaid(json.optString("areaid"));
                mArea.setName(json.optString("name"));
                mArea.setPinyin(json.optString("pinyin"));
                mArea.setShortpinyin(json.optString("shortpinyin"));
                mArea.setType(json.optString("type"));
                mArea.setParentId(json.optString("parentId"));
                mDatas.add(i, mArea);
            }
            for (int i = 0; i < mDatas.size(); i++) {
                if (mDatas.get(i).getType().equals("s")) {
                    provinceName.add(mDatas.get(i));
                }
            }
            Log.d(TAG, "firstName:" + provinceName.get(0).toString());
            mCityPopupWindow.setFocusable(true);
            mMainAdapter = new MainAdapter(MainActivity.this, provinceName);
            mMainAdapter2 = new MainAdapter(MainActivity.this, citysName);
            mListView.setAdapter(mMainAdapter);
            mListView1.setAdapter(mMainAdapter2);
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                 {
                    citysName.clear();
                    for (int i = 0; i < mDatas.size(); i++) {
                        String id = provinceName.get(arg2).getAreaid();
                        aa = provinceName.get(arg2).getName();
                        if (mDatas.get(i).getType().equals("c")) {
                            if (mDatas.get(i).getParentId().equals(id)) {
                                citysName.add(mDatas.get(i));
                            }
                        }
                    }
                    mMainAdapter2.notifyDataSetChanged();
                }
            });

            mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                 {
                    bb = citysName.get(arg2).getName();
                }
            });

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String getFromAssets(String fileName) {
        String result = "";
        try {
            InputStream inputStream = getResources().getAssets().open(fileName);
            // 获取文件的字节数
            int length = inputStream.available();
            // 创建byte数组
            byte[] buffer = new byte[length];
            // 将文件中的数据读到byte数组中
            inputStream.read(buffer);
            result = EncodingUtils.getString(buffer, ENCODING);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button) {
            mCityPopupWindow.setAnimationStyle(R.style.PopupAnimation);
            mCityPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 0, 0);
            mCityPopupWindow.update();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
}
复制代码

MainAdapter代码

public class MainAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Area> mList;
    private LayoutInflater  mInflater;
    private TextView mTextView;

    public MainAdapter(Context context,ArrayList<Area>list){
        this.mContext =context;
        this.mList =list;
        this.mInflater =LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //判空
        if (convertView==null) {
            mTextView = new TextView(mContext);
        }else {
            mTextView = (TextView) convertView;
        }
        mTextView.setText(mList.get(position).getName());
        mTextView.setTextSize(18);
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setPadding(0,20,0,20);
        mTextView.setTextColor(Color.BLACK);
        return mTextView;
    }
}
复制代码

Model

public class Area {

    private String areaid;
    private String name;
    private String pinyin;
    private String shortpinyin;
    private String type;
    private String parentId;

    public String getAreaid() {
        return areaid;
    }

    public void setAreaid(String areaid) {
        this.areaid = areaid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getShortpinyin() {
        return shortpinyin;
    }

    public void setShortpinyin(String shortpinyin) {
        this.shortpinyin = shortpinyin;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
}
复制代码

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#000000"
    android:gravity="bottom"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="#ffffff" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@drawable/wheelnavie" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dip"
                android:layout_marginTop="5dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/pickcitycancle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/wheelbutton_cancle_off"
                    android:paddingLeft="20dip"
                    android:paddingRight="20dip"
                    android:text="取消"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:layout_weight="1"
                >
                <TextView
                    android:id="@+id/tv_province"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:textColor="#FFFFFF"
                    tools:text="xx省"
                    android:layout_weight="1"
                    android:textSize="18sp"
                    tools:ignore="NestedWeights" />
                <TextView
                    android:id="@+id/tv_city"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:textColor="#FFFFFF"
                    tools:text="xx市"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dip"
                android:layout_marginTop="5dip"
                android:layout_weight="1"
                android:gravity="right" >

                <Button
                    android:id="@+id/pickcityconfirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/wheelbutton_confirm_off"
                    android:paddingLeft="20dip"
                    android:paddingRight="20dip"
                    android:text="完成"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2.0"
        android:background="#ffffff"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:fadingEdge="none"
            android:listSelector="#00000000"
            android:scrollbars="none" >
        </ListView>

        <ListView
            android:id="@+id/list1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:fadingEdge="none"
            android:listSelector="#00000000"
            android:scrollbars="none" >
        </ListView>
    </LinearLayout>

</LinearLayout>
复制代码

总结

android仿IOS的滚轮省市二级联动 实现还是比较简单的 就是2个ListView显示数据
A切换时通知B去做更新 !

文章分类
Android
文章标签