Android中ExpandableListView使用示例詳解
更新時間:2021年08月23日 09:21:42 作者:JustingWang_1
這篇文章主要為大家詳細介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android ExpandableListView雙層嵌套實現(xiàn)三級樹形菜單
- Android ExpandableListView實現(xiàn)下拉刷新和加載更多效果
- Android ExpandableListView單選以及多選實現(xiàn)代碼
- Android ScrollView嵌套ExpandableListView顯示不正常的問題的解決辦法
- Android listview ExpandableListView實現(xiàn)多選,單選,全選,edittext實現(xiàn)批量輸入的實例代碼
- Android中使用Expandablelistview實現(xiàn)微信通訊錄界面
- Android 關于ExpandableListView刷新問題的解決方法
- Android ExpandableListView使用方法案例詳解
相關文章
Android開發(fā)使用WebView打造web app示例代碼
這篇文章主要介紹了Android開發(fā)使用WebView打造web app的關鍵示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03
anroid開發(fā)教程之spinner下拉列表的使用示例
這篇文章主要介紹了anroid的spinner下拉列表的使用示例,需要的朋友可以參考下2014-04-04
Android序列化之Parcelable和Serializable的使用詳解
本篇文章主要介紹了Android序列化之Parcelable和Serializable的使用詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
淺談Android Classloader動態(tài)加載分析
這篇文章主要介紹了淺談Android Classloader動態(tài)加載分析,詳細的介紹了ClassLoader概念、分類,具有一定的參考價值,有興趣的可以了解一下2018-03-03
Kotlin協(xié)程Dispatchers原理示例詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Dispatchers原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Android 利用廣播監(jiān)聽usb連接狀態(tài)(變化情況)
這篇文章主要介紹了Android 利用廣播監(jiān)聽usb連接狀態(tài),需要的朋友可以參考下2017-06-06

