摩洛哥网站后缀,网站图片自动轮换怎么做的,好的app制作公司,网站制作工具本文採用一个Demo来展示Android中ExpandableListView控件的使用#xff0c;如怎样在组/子ListView中绑定数据源。直接上代码例如以下#xff1a; 程序结构图#xff1a; layout文件夹下的 main.xml 文件源代码例如以下#xff1a; ?xml version1.0 encodi… 本文採用一个Demo来展示Android中ExpandableListView控件的使用如怎样在组/子ListView中绑定数据源。直接上代码例如以下 程序结构图 layout文件夹下的 main.xml 文件源代码例如以下 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationverticalandroid:layout_widthfill_parentandroid:layout_heightfill_parent!-- 我们会自定义listview的显示方式在另外一个布局文件中边不用默认的方式 假设自定义listview的显示方式这里这个android:idid/android:list 必须这样写 --!-- android:drawSelectOnTopfalse此属性用来设置listview上的背景颜色会不会挡住覆盖内容 , 假设这是为false就表示不会覆盖掉 -- ExpandableListView android:idid/android:list android:layout_widthfill_parent android:layout_heightwrap_content android:layout_weight1 android:drawSelectorOnTopfalse/
/LinearLayout包 com.andyidea.demo中ContactsActivity.java源代码例如以下 package com.andyidea.demo;import java.util.ArrayList;
import java.util.List;import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;public class ContactsActivity extends ExpandableListActivity {ListString group; //组列表ListListString child; //子列表ContactsInfoAdapter adapter; //数据适配器/** Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题setContentView(R.layout.main);getExpandableListView().setBackgroundResource(R.drawable.default_bg);initializeData();getExpandableListView().setAdapter(new ContactsInfoAdapter());getExpandableListView().setCacheColorHint(0); //设置拖动列表的时候防止出现黑色背景}/*** 初始化组、子列表数据*/private void initializeData(){group new ArrayListString();child new ArrayListListString();addInfo(Andy,new String[]{male,138123***,GuangZhou});addInfo(Fairy,new String[]{female,138123***,GuangZhou});addInfo(Jerry,new String[]{male,138123***,ShenZhen});addInfo(Tom,new String[]{female,138123***,ShangHai});addInfo(Bill,new String[]{male,138231***,ZhanJiang});}/*** 模拟给组、子列表加入数据* param g-group* param c-child*/private void addInfo(String g,String[] c){group.add(g);ListString childitem new ArrayListString();for(int i0;ic.length;i){childitem.add(c[i]);}child.add(childitem);}class ContactsInfoAdapter extends BaseExpandableListAdapter{//-----------------Child----------------//Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {String string child.get(groupPosition).get(childPosition); return getGenericView(string);}//----------------Group----------------//Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);} Overridepublic long getGroupId(int groupPosition) {return groupPosition;} Overridepublic int getGroupCount() {return group.size();} Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {String string group.get(groupPosition); return getGenericView(string);}//创建组/子视图 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40);TextView text new TextView(ContactsActivity.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;} Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}}
} 最后程序执行后截图例如以下 转载于:https://www.cnblogs.com/bhlsheji/p/4182934.html