做社交网站 投入四川有那些网站建设公司
2026/3/12 0:06:07 网站建设 项目流程
做社交网站 投入,四川有那些网站建设公司,广州建站网络公司,湖北长安建设集团股份有限公司网站2.5.5 ExpandableListView(可折叠列表)的基本使用 分类 Android 基础入门教程 本节引言#xff1a; 本节要讲解的Adapter类控件是ExpandableListView#xff0c;就是可折叠的列表#xff0c;它是ListView的子类#xff0c; 在ListView的基础上它把应用中的列表项分为几组…2.5.5 ExpandableListView(可折叠列表)的基本使用分类Android 基础入门教程本节引言本节要讲解的Adapter类控件是ExpandableListView就是可折叠的列表它是ListView的子类 在ListView的基础上它把应用中的列表项分为几组每组里又可包含多个列表项。至于样子 类似于QQ联系人列表他的用法与ListView非常相似只是ExpandableListVivew显示的列表项 需由ExpandableAdapter提供。 下面我们来学习这个控件的基本使用 官方APIExpandableListView1.相关属性android:childDivider指定各组内子类表项之间的分隔条图片不会完全显示 分离子列表项的是一条直线android:childIndicator显示在子列表旁边的Drawable对象可以是一个图像android:childIndicatorEnd子列表项指示符的结束约束位置android:childIndicatorLeft子列表项指示符的左边约束位置android:childIndicatorRight子列表项指示符的右边约束位置android:childIndicatorStart子列表项指示符的开始约束位置android:groupIndicator显示在组列表旁边的Drawable对象可以是一个图像android:indicatorEnd组列表项指示器的结束约束位置android:indicatorLeft组列表项指示器的左边约束位置android:indicatorRight组列表项指示器的右边约束位置android:indicatorStart组列表项指示器的开始约束位置2.实现ExpandableAdapter的三种方式1.扩展BaseExpandableListAdpter实现ExpandableAdapter。2.使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter3.使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter 本节示例使用的是第一个扩展BaseExpandableListAdpter我们需要重写该类中的相关方法 下面我们通过一个代码示例来体验下3.代码示例我们来看下实现的效果图下面我们就来实现上图的这个效果核心是重写BaseExpandableListAdpter其实和之前写的普通的BaseAdapter是类似的 但是BaseExpandableListAdpter则分成了两部分组和子列表具体看代码你就知道了另外有一点要注意的是重写isChildSelectable()方法需要返回true不然不会触发 子Item的点击事件下面我们来写写首先是组和子列表的布局item_exlist_group.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationhorizontal android:padding5dp TextView android:idid/tv_group_name android:layout_widthmatch_parent android:layout_height56dp android:gravitycenter_vertical android:paddingLeft30dp android:textAP android:textStylebold android:textSize20sp / /LinearLayoutitem_exlist_item.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationhorizontal android:padding5dp android:background#6BBA79 ImageView android:idid/img_icon android:layout_width48dp android:layout_height48dp android:srcmipmap/iv_lol_icon1 android:focusablefalse/ TextView android:idid/tv_name android:layout_widthwrap_content android:layout_heightwrap_content android:layout_marginLeft15dp android:layout_marginTop15dp android:focusablefalse android:text提莫 android:textSize18sp / /LinearLayout然后是自定义的Adapter类MyBaseExpandableListAdapter.java/** * Created by Jay on 2015/9/25 0025. */ public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter { private ArrayListGroup gData; private ArrayListArrayListItem iData; private Context mContext; public MyBaseExpandableListAdapter(ArrayListGroup gData,ArrayListArrayListItem iData, Context mContext) { this.gData gData; this.iData iData; this.mContext mContext; } Override public int getGroupCount() { return gData.size(); } Override public int getChildrenCount(int groupPosition) { return iData.get(groupPosition).size(); } Override public Group getGroup(int groupPosition) { return gData.get(groupPosition); } Override public Item getChild(int groupPosition, int childPosition) { return iData.get(groupPosition).get(childPosition); } Override public long getGroupId(int groupPosition) { return groupPosition; } Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } Override public boolean hasStableIds() { return false; } //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象 Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolderGroup groupHolder; if(convertView null){ convertView LayoutInflater.from(mContext).inflate( R.layout.item_exlist_group, parent, false); groupHolder new ViewHolderGroup(); groupHolder.tv_group_name (TextView) convertView.findViewById(R.id.tv_group_name); convertView.setTag(groupHolder); }else{ groupHolder (ViewHolderGroup) convertView.getTag(); } groupHolder.tv_group_name.setText(gData.get(groupPosition).getgName()); return convertView; } //取得显示给定分组给定子位置的数据用的视图 Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolderItem itemHolder; if(convertView null){ convertView LayoutInflater.from(mContext).inflate( R.layout.item_exlist_item, parent, false); itemHolder new ViewHolderItem(); itemHolder.img_icon (ImageView) convertView.findViewById(R.id.img_icon); itemHolder.tv_name (TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(itemHolder); }else{ itemHolder (ViewHolderItem) convertView.getTag(); } itemHolder.img_icon.setImageResource(iData.get(groupPosition).get(childPosition).getiId()); itemHolder.tv_name.setText(iData.get(groupPosition).get(childPosition).getiName()); return convertView; } //设置子列表是否可选中 Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } private static class ViewHolderGroup{ private TextView tv_group_name; } private static class ViewHolderItem{ private ImageView img_icon; private TextView tv_name; } }PS存储子列表的数据不一定要用ArrayListArrayList这种根据自己的需求 定义~最后是MainActivity的布局以及Java代码布局文件:activity_main.xmlRelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:padding5dp tools:context.MainActivity ExpandableListView android:idid/exlist_lol android:layout_widthmatch_parent android:layout_heightmatch_parent android:childDivider#E02D2F/ /RelativeLayoutMainActivity.javapublic class MainActivity extends AppCompatActivity { private ArrayListGroup gData null; private ArrayListArrayListItem iData null; private ArrayListItem lData null; private Context mContext; private ExpandableListView exlist_lol; private MyBaseExpandableListAdapter myAdapter null; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext MainActivity.this; exlist_lol (ExpandableListView) findViewById(R.id.exlist_lol); //数据准备 gData new ArrayListGroup(); iData new ArrayListArrayListItem(); gData.add(new Group(AD)); gData.add(new Group(AP)); gData.add(new Group(TANK)); lData new ArrayListItem(); //AD组 lData.add(new Item(R.mipmap.iv_lol_icon3,剑圣)); lData.add(new Item(R.mipmap.iv_lol_icon4,德莱文)); lData.add(new Item(R.mipmap.iv_lol_icon13,男枪)); lData.add(new Item(R.mipmap.iv_lol_icon14,韦鲁斯)); iData.add(lData); //AP组 lData new ArrayListItem(); lData.add(new Item(R.mipmap.iv_lol_icon1, 提莫)); lData.add(new Item(R.mipmap.iv_lol_icon7, 安妮)); lData.add(new Item(R.mipmap.iv_lol_icon8, 天使)); lData.add(new Item(R.mipmap.iv_lol_icon9, 泽拉斯)); lData.add(new Item(R.mipmap.iv_lol_icon11, 狐狸)); iData.add(lData); //TANK组 lData new ArrayListItem(); lData.add(new Item(R.mipmap.iv_lol_icon2, 诺手)); lData.add(new Item(R.mipmap.iv_lol_icon5, 德邦)); lData.add(new Item(R.mipmap.iv_lol_icon6, 奥拉夫)); lData.add(new Item(R.mipmap.iv_lol_icon10, 龙女)); lData.add(new Item(R.mipmap.iv_lol_icon12, 狗熊)); iData.add(lData); myAdapter new MyBaseExpandableListAdapter(gData,iData,mContext); exlist_lol.setAdapter(myAdapter); //为列表设置点击事件 exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(mContext, 你点击了 iData.get(groupPosition).get(childPosition).getiName(), Toast.LENGTH_SHORT).show(); return true; } }); } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询