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

首頁 > 系統(tǒng) > Android > 正文

Android中ExpandableListView使用示例詳解

2019-12-12 01:56:03
字體:
供稿:網(wǎng)友

本文實例為大家分享了ExpandableListView使用示例,供大家參考,具體內(nèi)容如下

MainActivity:

public class Expandable_test extends Activity {  private ExpandableListView listView;  private Map<String, List<String>> dataset = new HashMap<>();  private String[] parentList = new String[]{"第一個菜單", "第二個菜單"};  private ExpandableListViewAdpter adpter;  private Context mContext = this;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_expandable_test);    listView=(ExpandableListView)findViewById(R.id.expandableListViewtext);    adpter=new ExpandableListViewAdpter(this,parentList);    listView.setAdapter(adpter);  }}

MainActivity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context="com.fae.mobile.testActivity.Expandable_test"><ExpandableListView  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/expandableListViewtext"></ExpandableListView></LinearLayout>

ExpandableListViewAdpter:

public class ExpandableListViewAdpter extends BaseExpandableListAdapter {  private Context mContext;  private Map<String, List<String>> dataset = new HashMap<>();  private String[] parentList = new String[]{"第一個菜單", "第二個菜單"};  private String[][] chdrenList=new String[][]      {{"菜單1","菜單1","菜單1","菜單1","菜單1","菜單1"},{"菜單2","菜單2","菜單2","菜單2","菜單2"}};  public ExpandableListViewAdpter(Context context, String[] parentList){    this.mContext=context;    this.dataset=dataset;    this.parentList=parentList;  }  @Override  public int getGroupCount() {    return parentList.length;  }  @Override  public int getChildrenCount(int groupPosition) {    return chdrenList[groupPosition].length;  }  @Override  public Object getGroup(int groupPosition) {    return parentList[groupPosition];  }  @Override  public Object getChild(int groupPosition, int childPosition) {    return chdrenList[groupPosition][childPosition];  }  @Override  public long getGroupId(int groupPosition) {    return groupPosition;  }  @Override  public long getChildId(int groupPosition, int childPosition) {    return childPosition;  }  @Override  public boolean hasStableIds() {    return true;  }  @Override  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {    GroupViewHolder holder=null;    if(convertView==null){      convertView = LayoutInflater.from(mContext).inflate(R.layout.itemlayoutexpandable, null);      holder=new GroupViewHolder();      holder.text=(TextView)convertView.findViewById(R.id.item_text);      convertView.setTag(holder);    }    else {      holder=(GroupViewHolder)convertView.getTag();    }    holder.text.setText(parentList[groupPosition]);    return convertView;  }  @Override  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {    ChildViewHolder holder=null;      if(convertView==null){        convertView = LayoutInflater.from(mContext).inflate(R.layout.itemlayoutexpandable, null);        holder=new ChildViewHolder();        holder.text=(TextView)convertView.findViewById(R.id.item_text);        convertView.setTag(holder);    }    else {        holder=(ChildViewHolder)convertView.getTag();      }      holder.text.setText(chdrenList[groupPosition][childPosition]);    return convertView;  }  @Override  public boolean isChildSelectable(int groupPosition, int childPosition) {    return true;  }  class GroupViewHolder {    TextView text;  }  class ChildViewHolder {    TextView text;  }}

Item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"><TextView  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:gravity="bottom"  android:id="@+id/item_text"/></LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 连山| 青川县| 色达县| 旺苍县| 乐亭县| 乐东| 桃园县| 红安县| 广水市| 望奎县| 宁德市| 万源市| 开原市| 越西县| 湖南省| 哈尔滨市| 沛县| 鹤壁市| 夏河县| 岐山县| 临泉县| 河津市| 怀柔区| 松潘县| 皮山县| 仙居县| 区。| 宁国市| 瑞金市| 曲麻莱县| 通化市| 沂南县| 平顺县| 上虞市| 井陉县| 清原| 封丘县| 剑河县| 双江| 都昌县| 宜昌市|