日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中ExpandableListView的使用

發(fā)布時間:2025/6/15 Android 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中ExpandableListView的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android中ExpandableListView的使用

ExpandableListView是Android中可以實(shí)現(xiàn)下拉list的一個控件,具體的實(shí)現(xiàn)方法如下:

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

view plaincopy to clipboardprint?
  • <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

    view plaincopy to clipboardprint?
  • private?List<String>?groupArray;??
  • private?List<List<String>>?childArray;??
  • ?

    對這兩個List進(jìn)行初始化,并插入一些數(shù)據(jù)

    view plaincopy to clipboardprint?
  • 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

    view plaincopy to clipboardprint?
  • //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

    view plaincopy to clipboardprint?
  • ExpandableListView?expandableListView?=?(ExpandableListView)findViewById(R.id.expandableListView);??
  • expandableListView.setAdapter(new?ExpandableAdapter(Main.this));??
  • 運(yùn)行即可見效果~~~

    ?

    ----------------------------------------------------------------------------------------------------------------

    Android版手風(fēng)琴(ExpandableListView)

    先看效果,過癮一番。

    ?

    ?

    ?源碼下載:http://files.cnblogs.com/salam/WidgetDemo.rar

    ?

      ExpandableListView是Android中的手風(fēng)琴,本人感覺效果相當(dāng)棒。

      一、ExpandableListView介紹

        一個垂直滾動的顯示兩個級別(Child,Group)列表項(xiàng)的視圖,列表項(xiàng)來自ExpandableListAdapter 。組可以單獨(dú)展開。

      1.重要方法

          expandGroup(int groupPos) :在分組列表視圖中展開一組,

          setSelectedGroup(int groupPosition) :設(shè)置選擇指定的組。

          setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :設(shè)置選擇指定的子項(xiàng)。

          getPackedPositionGroup(long packedPosition) :返回所選擇的組

          getPackedPositionForChild(int groupPosition, int childPosition) :返回所選擇的子項(xiàng)

          getPackedPositionType(long packedPosition) :返回所選擇項(xiàng)的類型(Child,Group)

          isGroupExpanded(int groupPosition) :判斷此組是否展開

      2.代碼:

    ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
    ??String title=((TextView)menuInfo.targetView).getText().toString();
    ??int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
    ??
    ??if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    ??int?groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
    ??int?childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

    二、ExpandableListAdapter

        一個接口,將基礎(chǔ)數(shù)據(jù)鏈接到一個ExpandableListView。此接口的實(shí)施將提供訪問Child的數(shù)據(jù)(由組分類),并實(shí)例化的Child和Group。

      1.重要方法

        getChildId(int groupPosition, int childPosition) 獲取與在給定組給予孩子相關(guān)的數(shù)據(jù)。

        getChildrenCount(int groupPosition) 返回在指定Group的Child數(shù)目。

      2.代碼

    ?public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    ???????? // Sample data set.? children[i] contains the children (String[]) for groups[i].
    ???????? public String[] groups = { "我的好友", "新疆同學(xué)", "親戚", "同事" };
    ???????? public String[][] children = {
    ???????????????? { "胡算林", "張俊峰", "王志軍", "二人" },
    ???????????????? { "李秀婷", "蔡喬", "別高", "余音" },
    ???????????????? { "攤派新", "張愛明" },
    ???????????????? { "馬超", "司道光" }
    ???????? };
    ????????
    ???????? public Object getChild(int groupPosition, int childPosition) {
    ???????????? return children[groupPosition][childPosition];
    ???????? }

    ???????? public long getChildId(int groupPosition, int childPosition) {
    ???????????? return childPosition;
    ???????? }

    ???????? public int getChildrenCount(int groupPosition) {
    ???????????? return children[groupPosition].length;
    ???????? }

    ???????? public TextView getGenericView() {
    ???????????? // Layout parameters for the ExpandableListView
    ???????????? AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    ???????????????????? ViewGroup.LayoutParams.MATCH_PARENT, 64);

    ???????????? TextView textView = new TextView(ExpandableListDemo.this);
    ???????????? textView.setLayoutParams(lp);
    ???????????? // Center the text vertically
    ???????????? textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    ???????????? // Set the text starting position
    ???????????? textView.setPadding(36, 0, 0, 0);
    ???????????? return textView;
    ???????? }
    ????????
    ???????? public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
    ???????????????? View convertView, ViewGroup parent) {
    ???????????? TextView textView = getGenericView();
    ???????????? textView.setText(getChild(groupPosition, childPosition).toString());
    ???????????? return textView;
    ???????? }

    ???????? public Object getGroup(int groupPosition) {
    ???????????? return groups[groupPosition];
    ???????? }

    ???????? public int getGroupCount() {
    ???????????? return groups.length;
    ???????? }

    ???????? public long getGroupId(int groupPosition) {
    ???????????? return groupPosition;
    ???????? }

    ???????? public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    ???????????????? ViewGroup parent) {
    ???????????? TextView textView = getGenericView();
    ???????????? textView.setText(getGroup(groupPosition).toString());
    ???????????? return textView;
    ???????? }

    ???????? public boolean isChildSelectable(int groupPosition, int childPosition) {
    ???????????? return true;
    ???????? }

    ???????? public boolean hasStableIds() {
    ???????????? return true;
    ???????? }

    ???? }

    《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的Android中ExpandableListView的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 亚洲看片网 | 亚洲影视一区 | 九九热精品在线视频 | 日韩经典第一页 | 成人91| 日韩av成人在线观看 | 久久精品视频一区二区三区 | 国产激情视频在线 | 米奇7777狠狠狠狠视频 | 亚洲人一区二区三区 | 亚洲国产精品一区 | 黄黄视频在线观看 | 99热久 | 美女av影院 | www.777奇米 | 亚洲永久无码7777kkk | 亚洲毛片一级 | 色老头一区二区三区在线观看 | 亚洲乱轮视频 | 欧美另类一区二区 | 久久精品国产亚洲av麻豆蜜芽 | 黑人操中国女人视频 | 高级家教课程在线观看 | 亚洲第一视频在线 | 一区二区伊人 | 午夜av电影在线观看 | 毛片aaaa| 制服丝袜国产在线 | 久久成人久久 | 九九热在线精品 | 中文字幕高清在线免费播放 | 熟妇的味道hd中文字幕 | 亚洲国产精品久久久久久久 | 日本国产一区 | 成人福利在线看 | 国产精品一区二区无码免费看片 | 久久精品国产精品亚洲 | 在线不卡免费视频 | 国产一区二区三区在线免费观看 | 久久精品中文闷骚内射 | 亚洲一区中文字幕在线 | 九九九九久久久久 | 日韩手机看片 | 激情91| 中文欧美日韩 | 亚洲成人av电影 | 素人一区二区 | 新版天堂资源中文8在线 | 2020亚洲天堂 | 欧美影院久久 | 欧美日韩在线观看成人 | 亚洲精品久久久久久久蜜桃臀 | 日本欧美久久久久免费播放网 | 免费日韩 | 日本三级日本三级日本三级极 | 亚洲欧洲精品在线 | 欧美第二区| 成人黄色免费在线观看 | 美女被到爽高潮视频 | 91原创视频在线观看 | 特级西西444www大精品视频 | 91精品久久久久久综合五月天 | 国产精品精华液网站 | 欧美成人自拍视频 | 久久久久久久久亚洲 | jizz成熟丰满日本少妇 | 日韩激情电影在线 | 男人的影院 | 欧美精品偷拍 | 好看的中文字幕电影 | 夜夜伊人 | 久久久精品一区二区三区 | 另类天堂av | 午夜精品一区二区三区在线 | 中文字幕av在线播放 | 久久婷婷五月综合色国产香蕉 | 亚洲调教欧美在线 | 亚洲AV无码一区二区三区少妇 | 欧美中文字幕在线播放 | 中文字幕2021 | 黄色片网站在线播放 | 国产精品久久久久久免费 | 亚洲一区二区在线播放 | 爆操少妇 | 爽爽影院在线免费观看 | 精品孕妇一区二区三区 | 露脸丨91丨九色露脸 | 伊人国产女 | 亚洲免费成人av | 天天天操操操 | av地址在线观看 | 免费日本特黄 | 自拍偷拍20p | 国产精品入口麻豆 | 国产精品视频专区 | 国产精品久久在线观看 | 女同性恋毛片 | 国产偷怕| 日本大尺度做爰呻吟 |