Andriod中ExpandableListView的使用結合json數據

在日常開發中,可能會遇到一些需要展開的列表,此時ListView已經不能滿足使用了,今天來介紹ExpandableListView控件的使用。使用它可以很方便的做到二級列表,三級列表。ExpandableListview是ListView的子類,它在普通的ListView的基礎上進行了擴展,它把應用中的列表分爲幾組,每組又包含了多個列表。

Demo中的數據我是拿取了省市區json字符串中的所有省,和每個省對應的市,只做了二級列表的效果,有興趣的朋友可以在這個基礎上增加到三級列表的效果

下面看一下Demo的運行效果

   

json數據格式爲:


Demo的下載地址爲:http://download.csdn.net/detail/shihuiyun/9567034 有需要的可以下載下來看看

主要代碼如下:

//只用來展示省-市
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 解析省市區的json數據
		initData();
	}

	// 解析json數據
	private void initData() {

		List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
		String jsondata = null;// 要解析的數據
		StringBuffer sb = new StringBuffer();
		try {
			InputStream is = getAssets().open("city.json");
			int len = -1;
			byte[] buf = new byte[1024];
			while ((len = is.read(buf)) != -1) {
				sb.append(new String(buf, 0, len, "utf-8"));
			}
			is.close();
			jsondata = sb.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			JSONObject obj = new JSONObject(jsondata);
			JSONArray arr = obj.getJSONArray("citylist");
			Map<String, String> map;
			List<Map<String, String>> list;
			for (int i = 0; i < arr.length(); i++) {
				JSONObject obj1 = arr.getJSONObject(i);
				map = new HashMap<String, String>();
				map.put("group", obj1.getString("p"));
				JSONArray arr1 = obj1.getJSONArray("c");
				Map<String, String> map1;
				list = new ArrayList<Map<String, String>>();
				for (int j = 0; j < arr1.length(); j++) {
					JSONObject obj2 = arr1.getJSONObject(j);
					map1 = new HashMap<String, String>();
					map1.put("child", obj2.getString("n"));
					list.add(map1);
				}

				childs.add(list);
				groups.add(map);
			}

			ExpandableListView elv = (ExpandableListView) findViewById(R.id.mExpandableListView);
			ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groups,
					childs);
			elv.setAdapter(viewAdapter);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
自定義適配器ExpandableAdapter 的代碼如下:
public class ExpandableAdapter extends BaseExpandableListAdapter
{
	  private Context context;
	  List<Map<String, String>> groups;
	  List<List<Map<String, String>>> childs;
	  /*
	   * 構造函數:
	   * 參數1:context對象
	   * 參數2:一級列表數據源
	   * 參數3:二級列表數據源
	   */
	  public ExpandableAdapter(Context context, List<Map<String, String>> groups, List<List<Map<String, String>>> childs)
	  {
	   this.groups = groups;
	   this.childs = childs;
	   this.context = context;
	  }
	  public Object getChild(int groupPosition, int childPosition)
	  {
	   return childs.get(groupPosition).get(childPosition);
	  }
	  public long getChildId(int groupPosition, int childPosition)
	  {
	   return childPosition;
	  }
	  //獲取二級列表的View對象
	  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
	    ViewGroup parent)
	  {
	   @SuppressWarnings("unchecked")
	   String text = ((Map<String, String>) getChild(groupPosition, childPosition)).get("child");
	   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	   //獲取二級列表對應的佈局文件, 並將其各元素設置相應的屬性
	   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.childs, null);
	   TextView tv = (TextView) linearLayout.findViewById(R.id.child_tv);
	   tv.setText(text);
	   ImageView imageView = (ImageView)linearLayout.findViewById(R.id.child_iv);
	   imageView.setImageResource(R.drawable.icon);
	   return linearLayout;
	  }
	  public int getChildrenCount(int groupPosition)
	  {
		  int a;
		  try {
			a=childs.get(groupPosition).size();
		} catch (Exception e) {
			a=0;
		}
		  
	  return a;
	  }
	  public Object getGroup(int groupPosition)
	  {
	   return groups.get(groupPosition);
	  }
	  public int getGroupCount()
	  {
	   return groups.size();
	  }
	  public long getGroupId(int groupPosition)
	  {
	   return groupPosition;
	  }
	  //獲取一級列表View對象
	  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
	  {
	   String text = groups.get(groupPosition).get("group");
	   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	   //獲取一級列表佈局文件,設置相應元素屬性
	   LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
	   TextView textView = (TextView)linearLayout.findViewById(R.id.group_tv);
	   textView.setText(text);
	   
	   ImageView imageView = (ImageView) linearLayout.findViewById(R.id.mImageView);
	   
	   if(getChildrenCount(groupPosition) == 0){//該組下沒有子項
	    //imageView.setVisibility(View.GONE);
	    imageView.setImageResource(R.drawable.down);
	   }else{
	    if(isExpanded == true){//展開狀態
	     imageView.setImageResource(R.drawable.down);
	    }else{//收起狀態
	     imageView.setImageResource(R.drawable.on);
	    }
	   }
	   return linearLayout;
	  }
	  public boolean hasStableIds()
	  {
	   return false;
	  }
	  public boolean isChildSelectable(int groupPosition, int childPosition)
	  {
	   return false;
	  }
	 }
	
幾個佈局文件如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!-- 禁用系統自帶圖標android:groupIndicator="@null" -->
<ExpandableListView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:groupIndicator="@null"
    android:id="@+id/mExpandableListView"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    />
</LinearLayout>
group.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"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/mImageView"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/group_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical|left"
        android:textColor="#000000"
        android:textSize="26sp" />

</LinearLayout>
childs.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"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/child_iv"
        android:layout_width="1dp"
        android:layout_height="20dp"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="2dp"
        android:scaleType="fitXY"
        android:src="@drawable/icon" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_gravity="center_vertical|left"
            android:scaleType="fitXY"
            android:src="@drawable/to" />

        <TextView
            android:id="@+id/child_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|left"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

這樣就實現了簡單是二級列表,圖片與文字的距離你可以自己設定




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章