a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android中ExpandableListView的用法實例

2020-04-11 11:43:05
字體:
來源:轉載
供稿:網友

本文實例講述了Android中ExpandableListView的用法,ExpandableListView是android中可以實現下拉list的一個控件,具體的實現方法如下:

首先:在layout的xml文件中定義一個ExpandableListView

復制代碼 代碼如下:
<LinearLayout  
    android:id="@+id/linearLayout" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    androidrientation="vertical" 
    > 
     
    <ExpandableListView 
    android:id="@+id/expandableListView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
        /> 
</LinearLayout>

定義兩個List,用來存放控件中Group/Child中的String

復制代碼 代碼如下:
private List<String> groupArray; 
private List<List<String>> childArray;

對這兩個List進行初始化,并插入一些數據

復制代碼 代碼如下:
groupArray = new ArrayList<String>(); 
childArray = new ArrayList<List<String>>(); 
 
groupArray.add("第一行"); 
groupArray.add("第二行"); 
 
List<String> tempArray = new ArrayList<String>(); 
tempArray.add("第一條"); 
tempArray.add("第二條"); 
tempArray.add("第三條"); 
 
for(int index = 0; index <groupArray.size(); ++index) 

    childArray.add(tempArray); 
}

定義ExpandableListView的Adapter

復制代碼 代碼如下:
//ExpandableListView的Adapter 
public class ExpandableAdapter extends BaseExpandableListAdapter 

    Activity activity; 
     
    public ExpandableAdapter(Activity a) 
    { 
        activity = a; 
    } 
    public Object getChild(int groupPosition, int childPosition) 
    { 
        return childArray.get(groupPosition).get(childPosition); 
    } 
    public long getChildId(int groupPosition, int childPosition) 
    { 
        return childPosition; 
    } 
    public int getChildrenCount(int groupPosition) 
    { 
        return childArray.get(groupPosition).size(); 
    } 
    public View getChildView(int groupPosition, int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) 
    { 
        String string = childArray.get(groupPosition).get(childPosition); 
        return getGenericView(string); 
    } 
    // group method stub 
    public Object getGroup(int groupPosition) 
    { 
        return groupArray.get(groupPosition); 
    } 
    public int getGroupCount() 
    { 
        return groupArray.size(); 
    } 
    public long getGroupId(int groupPosition) 
    { 
        return groupPosition; 
    } 
    public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parent) 
    { 
        String string = groupArray.get(groupPosition); 
        return getGenericView(string); 
    } 
    // View stub to create Group/Children 's View 
    public TextView getGenericView(String string) 
    { 
        // Layout parameters for the ExpandableListView 
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( 
                ViewGroup.LayoutParams.FILL_PARENT, 64); 
        TextView text = new TextView(activity); 
        text.setLayoutParams(layoutParams); 
        // Center the text vertically 
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
        // Set the text starting position 
        text.setPadding(36, 0, 0, 0); 
        text.setText(string); 
        return text; 
    } 
    public boolean hasStableIds() 
    { 
        return false; 
    } 
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    { 
        return true; 
    } 
}

最后,給定義好的ExpandableListView添加上Adapter

復制代碼 代碼如下:
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView); 
expandableListView.setAdapter(new ExpandableAdapter(Main.this));

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九九久久久| 欧美三级在线视频 | 99热精品久久 | 亚洲精品国产精品乱码不99按摩 | 91精品久久久久久久99 | а天堂中文最新一区二区三区 | 国产精品区二区三区日本 | 超碰成人av | 亚洲一区二区三 | 久久精品99国产精品日本 | 欧美a区 | 国产综合精品一区二区三区 | 成人午夜激情 | 欧美性猛交一区二区三区精品 | 亚洲一区二区三区四区五区午夜 | 激情毛片 | 久久99精品国产麻豆不卡 | 91麻豆精品国产91久久久久久久久 | 91精品久久| 国产一区二区av | 亚洲日日操| 国产精品入口麻豆www | 黄色在线免费 | 精品国产aⅴ一区二区 | 在线观看欧美日韩视频 | 欧洲成人在线视频 | 四虎成人在线播放 | 91精品国产乱码久久久久久久久 | 亚洲毛片在线 | 久久亚洲精品国产一区 | 国产成人精品久久 | 成人av免费在线观看 | 国产www在线 | www.com欧美| 欧美一级特黄aaaaaaa色戒 | 国产成人午夜片在线观看高清观看 | 日本不卡免费新一二三区 | 爱爱免费视频网站 | 日本不卡一二三区 | 色资源 | 久久99久久98精品免观看软件 |