import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import com.bawei.zhoukao_21.R; import com.bawei.zhoukao_21.bean.WeatherBean; import com.bawei.zhoukao_21.url.Url; import com.bawei.zhoukao_21.utils.MyAsyncTask; import com.google.gson.Gson; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class MainActivity extends AppCompatActivity { private ExpandableListView mExpandableListView; private ArrayList<WeatherBean.ResultBean> mArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initView() { mExpandableListView = (ExpandableListView) findViewById(R.id.main_explistview); mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { expand_group(groupPosition); return true; } }); } private void initData() { MyAsyncTask asyncTask = new MyAsyncTask() { @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); WeatherBean weatherBean = gson.fromJson(s, WeatherBean.class); //解析完 获得相应的集合 List<WeatherBean.ResultBean> resultList = weatherBean.getResult(); //获得处理后 显示的集合 mArrayList = dispose_list(resultList); MyAdapter myAdapter = new MyAdapter(MainActivity.this, mArrayList); mExpandableListView.setAdapter(myAdapter); } }; asyncTask.execute(Url.url); } //处理 解析出来的集合 private ArrayList<WeatherBean.ResultBean> dispose_list(List<WeatherBean.ResultBean> list) { //new 一个最后需要的集合 ArrayList<WeatherBean.ResultBean> f_list = new ArrayList<WeatherBean.ResultBean>(); for (int i = 0; i < list.size(); ) { WeatherBean.ResultBean resultBean = list.get(i); int id = Integer.parseInt(resultBean.getParentid()); if (id == 0) { resultBean.setChild_list(new ArrayList<WeatherBean.ResultBean>()); f_list.add(resultBean); list.remove(i); } else if (id != 0) { break; } else { i++; } } for (int i = 0; i < f_list.size(); i++) { WeatherBean.ResultBean bean = f_list.get(i); int id = Integer.parseInt(bean.getCityid()); for (int k = 0; k < list.size(); ) { WeatherBean.ResultBean city_child = list.get(k); int child_id = Integer.parseInt(city_child.getParentid()); if (child_id == id) { bean.getChild_list().add(city_child); list.remove(k); } else { k++; } } } return f_list; } //设置 点开一个 关闭其它 private void expand_group(int groupPosition) { for (int i = 0; i < mArrayList.size(); i++) { if (i == groupPosition) { //当前条目是否展开 展开则关闭 关闭则展开 if (mExpandableListView.isGroupExpanded(groupPosition)) { mExpandableListView.collapseGroup(i); return; } else { mExpandableListView.expandGroup(i); } } else { mExpandableListView.collapseGroup(i); } } } }
package com.bawei.zhoukao_21.activity; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.bawei.zhoukao_21.bean.WeatherBean; import java.util.ArrayList; /** * 类用途: * 时间:2017/4/9 21:13 */ public class MyAdapter extends BaseExpandableListAdapter { private Context context; private ArrayList<WeatherBean.ResultBean> arrayList; public MyAdapter(Context context, ArrayList<WeatherBean.ResultBean> arrayList) { this.context = context; this.arrayList = arrayList; } @Override public int getGroupCount() { return arrayList.size(); } @Override public int getChildrenCount(int groupPosition) { return arrayList.get(groupPosition).getChild_list().size(); } @Override public Object getGroup(int groupPosition) { return arrayList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return arrayList.get(groupPosition).getChild_list().get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String city = arrayList.get(groupPosition).getCity(); View view = get_group_TextView(city); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String s = arrayList.get(groupPosition).getChild_list().get(childPosition).getCity(); View view = get_child_TextView(s); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } private View get_group_TextView(String text) { TextView tv = new TextView(context); tv.setGravity(Gravity.CENTER); tv.setPadding(5, 5, 5, 5); tv.setTextSize(25); tv.setText(text); return tv; } private View get_child_TextView(String text) { TextView tv = new TextView(context); tv.setPadding(5, 5, 5, 5); tv.setGravity(Gravity.CENTER); tv.setTextSize(20); tv.setText(text); return tv; } }