国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

分享Android中ExpandableListView控件使用教程

2020-04-11 11:08:36
字體:
來源:轉載
供稿:網友

本文采用一個Demo來展示Android中ExpandableListView控件的使用,如如何在組/子ListView中綁定數據源。直接上代碼如下:
程序結構圖:

layout目錄下的 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">   <!-- 我們會自己定義listview的顯示方式(在另外一個布局文件里邊)不用默認的方式       如果自定義listview的顯示方式這里這個android:id="@id/android:list" 必須這樣寫 -->   <!-- android:drawSelectOnTop="false"此屬性用來設置listview上的背景顏色會不會    擋住(覆蓋)內容 , 如果這是為false就表示不會覆蓋掉 -->    <ExpandableListView      android:id="@id/android:list"             android:layout_width="fill_parent"             android:layout_height="wrap_content"            android:layout_weight="1"             android:drawSelectorOnTop="false"/>  </LinearLayout> 

包com.andyidea.demo中ContactsActivity.java源碼如下:

package com.andyidea.demo;  import java.util.ArrayList; import java.util.List;  import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView;  public class ContactsActivity extends ExpandableListActivity {      List<String> group;      //組列表   List<List<String>> child;   //子列表   ContactsInfoAdapter adapter; //數據適配器      /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     requestWindowFeature(Window.FEATURE_NO_TITLE); //設置為無標題     setContentView(R.layout.main);     getExpandableListView().setBackgroundResource(R.drawable.default_bg);          initializeData();     getExpandableListView().setAdapter(new ContactsInfoAdapter());     getExpandableListView().setCacheColorHint(0); //設置拖動列表的時候防止出現黑色背景   }      /**    * 初始化組、子列表數據    */   private void initializeData(){     group = new ArrayList<String>();     child = new ArrayList<List<String>>();          addInfo("Andy",new String[]{"male","138123***","GuangZhou"});     addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});     addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});     addInfo("Tom",new String[]{"female","138123***","ShangHai"});     addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});        }      /**    * 模擬給組、子列表添加數據    * @param g-group    * @param c-child    */   private void addInfo(String g,String[] c){     group.add(g);     List<String> childitem = new ArrayList<String>();     for(int i=0;i<c.length;i++){       childitem.add(c[i]);     }     child.add(childitem);   }      class ContactsInfoAdapter extends BaseExpandableListAdapter{           //-----------------Child----------------//     @Override     public Object getChild(int groupPosition, int childPosition) {       return child.get(groupPosition).get(childPosition);     }          @Override     public long getChildId(int groupPosition, int childPosition) {       return childPosition;     }          @Override     public int getChildrenCount(int groupPosition) {       return child.get(groupPosition).size();     }          @Override     public View getChildView(int groupPosition, int childPosition,         boolean isLastChild, View convertView, ViewGroup parent) {       String string = child.get(groupPosition).get(childPosition);        return getGenericView(string);     }          //----------------Group----------------//     @Override     public Object getGroup(int groupPosition) {       return group.get(groupPosition);     }              @Override     public long getGroupId(int groupPosition) {       return groupPosition;     }            @Override     public int getGroupCount() {       return group.size();     }            @Override     public View getGroupView(int groupPosition, boolean isExpanded,         View convertView, ViewGroup parent) {       String string = group.get(groupPosition);        return getGenericView(string);     }      //創建組/子視圖      public TextView getGenericView(String s) {        // Layout parameters for the ExpandableListView        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(            ViewGroup.LayoutParams.FILL_PARENT, 40);         TextView text = new TextView(ContactsActivity.this);        text.setLayoutParams(lp);        // Center the text vertically        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);        // Set the text starting position        text.setPadding(36, 0, 0, 0);                text.setText(s);        return text;      }                @Override     public boolean hasStableIds() {       // TODO Auto-generated method stub       return false;     }          @Override     public boolean isChildSelectable(int groupPosition, int childPosition) {       // TODO Auto-generated method stub       return true;     }        } } 

最后,程序運行后截圖如下:

希望本文所述對大家學習Android軟件編程有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郯城县| 碌曲县| 衢州市| 灵武市| 德格县| 青冈县| 宜良县| 罗定市| 佛学| 营山县| 娄烦县| 大洼县| 永丰县| 繁昌县| 滨海县| 杭州市| 贺兰县| 建始县| 烟台市| 梧州市| 崇文区| 白玉县| 怀安县| 县级市| 万全县| 通江县| 湄潭县| 报价| 牙克石市| 望城县| 宝丰县| 阆中市| 腾冲县| 华宁县| 安庆市| 屯门区| 福鼎市| 三穗县| 德昌县| 苍溪县| 科技|