Android中ExpandableListView使用示例詳解
更新時間:2021年08月23日 09:21:42 作者:JustingWang_1
這篇文章主要為大家詳細介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了ExpandableListView使用示例,供大家參考,具體內容如下
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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 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 RecycleView實現(xiàn)下拉刷新和上拉加載
這篇文章主要為大家詳細介紹了android RecycleView實現(xiàn)下拉刷新和上拉加載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06Android仿淘寶頭條向上滾動廣告條ViewFlipper
這篇文章主要為大家詳細介紹了Android仿淘寶頭條向上滾動廣告條ViewFlipper,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05實例解析Android系統(tǒng)中的ContentProvider組件用法
這篇文章主要介紹了Android系統(tǒng)中的ContentProvider組件用法,舉例講解了ContentProvider傳遞數(shù)據(jù)及監(jiān)聽ContentProvider數(shù)據(jù)改變的方法,十分詳細,需要的朋友可以參考下2016-04-04Flutter 首頁必用組件NestedScrollView的示例詳解
今天介紹的組件是NestedScrollView,大部分的App首頁都會用到這個組件。對Flutter 首頁必用組件NestedScrollView的相關知識感興趣的一起看看吧2020-05-05ViewPager實現(xiàn)帶引導小圓點與自動跳轉的引導界面
這篇文章主要為大家詳細介紹了ViewPager實現(xiàn)帶引導小圓點與自動跳轉的引導界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11android播放視頻時在立體聲與單聲道之間切換無變化原因分析及解決
使用第三方視頻播放器,有立體聲與單聲道之間切換,發(fā)現(xiàn)切換后無作用,原因是由于在HAL層默認沒有處理上層發(fā)的stereo 轉mono的命令,具體的解決方法如下2013-06-06