Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android技術基礎 >> 第49章、可伸縮列表ExpandableListView(從零開始學Android)

第49章、可伸縮列表ExpandableListView(從零開始學Android)

編輯:Android技術基礎

如果希望展示的列表可以收縮和展開,就像QQ好友列表一樣,我們可以使用ExpandableListView。

 

一、設計界面

  1、布局文件

  打開activity_main.xml文件。

  輸入以下代碼:

[html] view plain copy  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout   
  3.      xmlns:android="http://schemas.android.com/apk/res/android"  
  4.      android:layout_width="match_parent"  
  5.      android:layout_height="match_parent"  
  6.      android:orientation="vertical" >  
  7.      <ExpandableListView   
  8.          android:id="@+id/province"  
  9.          android:layout_width="match_parent"  
  10.          android:layout_height="match_parent"  
  11.          android:background="#EFEFEF">  
  12.      </ExpandableListView>   
  13.  </LinearLayout>  


 

二、程序文件

  打開“src/com.genwoxue.expandable/MainActivity.java”文件。

  然後輸入以下代碼:

[java] view plain copy  
  1. package com.genwoxue.expandablelistview;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.AbsListView;  
  10. import android.widget.BaseExpandableListAdapter;  
  11. import android.widget.ExpandableListAdapter;  
  12. import android.widget.ExpandableListView;  
  13. import android.widget.ExpandableListView.OnChildClickListener;  
  14. import android.widget.LinearLayout;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.         protected void onCreate(Bundle savedInstanceState) {  
  21.             super.onCreate(savedInstanceState);  
  22.             setContentView(R.layout.activity_main);  
  23.   
  24.             final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {  
  25.                 //設置組視圖的顯示文字  
  26.                 private String[] province = new String[] { "河南省", "河北省", "山東省","山西省" };  
  27.                 //子視圖顯示文字  
  28.                 private String[][] city = new String[][] {  
  29.                         { "鄭州市", "開封市", "新鄉市", "安陽市", "南陽市"},  
  30.                         { "石家莊市", "邯鄲市", "保定市", "廊坊市"},  
  31.                         { "濟南市", "青島市", "日照市", "煙台市", "威海市" },  
  32.                         { "太原市", "大同市", "晉城市", "呂梁市", "長治市" }  
  33.                 };  
  34.                   
  35.                 //自己定義一個獲得文字信息的方法  
  36.                 TextView getTextView() {  
  37.                     AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  38.                             ViewGroup.LayoutParams.MATCH_PARENT, 64);  
  39.                     TextView textView = new TextView(MainActivity.this);  
  40.                     textView.setLayoutParams(lp);  
  41.                     textView.setGravity(Gravity.CENTER_VERTICAL);  
  42.                     textView.setPadding(36, 0, 0, 0);  
  43.                     textView.setTextSize(20);  
  44.                     textView.setTextColor(Color.BLACK);  
  45.                     return textView;  
  46.                 }  
  47.   
  48.                   
  49.                 //重寫ExpandableListAdapter中的各個方法  
  50.                 @Override  
  51.                 public int getGroupCount() {  
  52.                     return province.length;  
  53.                 }  
  54.   
  55.                 @Override  
  56.                 public Object getGroup(int groupPosition) {  
  57.                     return province[groupPosition];  
  58.                 }  
  59.   
  60.                 @Override  
  61.                 public long getGroupId(int groupPosition) {  
  62.                     return groupPosition;  
  63.                 }  
  64.   
  65.                 @Override  
  66.                 public int getChildrenCount(int groupPosition) {  
  67.                     return city[groupPosition].length;  
  68.                 }  
  69.   
  70.                 @Override  
  71.                 public Object getChild(int groupPosition, int childPosition) {  
  72.                     return city[groupPosition][childPosition];  
  73.                 }  
  74.   
  75.                 @Override  
  76.                 public long getChildId(int groupPosition, int childPosition) {  
  77.                     return childPosition;  
  78.                 }  
  79.   
  80.                 @Override  
  81.                 public boolean hasStableIds() {  
  82.                     return true;  
  83.                 }  
  84.   
  85.                 @Override  
  86.                 public View getGroupView(int groupPosition, boolean isExpanded,  
  87.                         View convertView, ViewGroup parent) {  
  88.                     LinearLayout ll = new LinearLayout(MainActivity.this);  
  89.                     ll.setOrientation(0);  
  90.                     TextView textView = getTextView();  
  91.                     textView.setTextColor(Color.BLACK);  
  92.                     textView.setText(getGroup(groupPosition).toString());  
  93.                     ll.addView(textView);  
  94.   
  95.                     return ll;  
  96.                 }  
  97.   
  98.                 @Override  
  99.                 public View getChildView(int groupPosition, int childPosition,  
  100.                         boolean isLastChild, View convertView, ViewGroup parent) {  
  101.                     LinearLayout ll = new LinearLayout(MainActivity.this);  
  102.                     ll.setOrientation(0);  
  103.                     TextView textView = getTextView();  
  104.                     textView.setText(getChild(groupPosition, childPosition).toString());  
  105.                     ll.addView(textView);  
  106.                     return ll;  
  107.                 }  
  108.   
  109.                 @Override  
  110.                 public boolean isChildSelectable(int groupPosition,int childPosition) {  
  111.                     return true;  
  112.                 }  
  113.   
  114.             };  
  115.               
  116.             ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.province);  
  117.             expandableListView.setAdapter(adapter);  
  118.               
  119.             //設置item點擊的監聽器  
  120.             expandableListView.setOnChildClickListener(new OnChildClickListener() {  
  121.   
  122.                 @Override  
  123.                 public boolean onChildClick(ExpandableListView parent, View v,  
  124.                         int groupPosition, int childPosition, long id) {  
  125.   
  126.                     Toast.makeText(  
  127.                             MainActivity.this,  
  128.                             adapter.getChild(groupPosition, childPosition).toString(),  
  129.                             Toast.LENGTH_SHORT).show();  
  130.   
  131.                     return false;  
  132.                 }  
  133.             });  
  134.         }  
  135.     }  


三、運行結果

  

  \ \
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved