ExpandableListView 使用示例
来源:互联网 发布:怎么买一个淘宝店铺 编辑:程序博客网 时间:2024/06/10 09:33
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>
阅读全文
0 0
- ExpandableListView的使用示例
- ExpandableListView使用示例
- ExpandableListView 使用示例
- 【Android】ExpandableListView示例
- ExpandableListView使用
- 使用ExpandableListView
- 使用ExpandableListView
- ExpandableListView使用
- ExpandableListView使用
- ExpandableListView使用
- ExpandableListView使用
- ExpandableListView使用
- ExpandableListView的简单代码示例
- ExpandableListView的使用
- ExpandableListView的使用
- android ExpandableListView使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Error:Execution failed for task ':app:validateDebugSigning'. > Keystore file
- Numpy 数据精度
- 向 Android O 迁移应用
- Java集合之ConcurrentHashMap实现原理
- GC是什么? 为什么要有GC?
- ExpandableListView 使用示例
- 制作ext4文件系统
- hibernate教程____hibernate简介
- pptpd基于mysql用户验证的完整操作步骤
- 《自动化测试修炼宝典》-第二章节-测试自动化的前期准备
- 关于TI CC2541和Nordic NRF51822的AES ECB加密
- JavaScript 中验证输入值不为空
- ION-DTN开启CFDP协议传输
- 安卓基础3:数据库和ListView