频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
Android[初级教程]第八章 ExpandableListView控件
2011-10-03 12:02:44      个评论      
收藏   我要投稿

这一章我们来学习ExpandableListView控件,ExpandableListView就是可扩展列表控件,还是拿西游记来说,里面唐僧师徙都各有各的本领,每个人的本领都不一样,放一起显示肯定不适合,这里我们就用ExpandableListView控件将他们显示出来.

看main.xml

 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="西游记各主人公能力" android:id="@+id/textView1
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
    <ExpandableListView android:layout_height="wrap_content" 
        android:id="@+id/expandableListView" android:layout_width="match_parent"></ExpandableListView> 
    <TextView android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:text="@string/hello" 
        android:id="@+id/text"></TextView> 
</LinearLayout> 

还是跟前几次一样,定义了一个ExpandableListView控件,下面是java源代码:

 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class ButtonDemoActivity extends Activity 

    private TextView text = null; 
    private int[] image = { R.drawable.tangseng, R.drawable.wukong, 
            R.drawable.bajie, R.drawable.shaseng }; 
    private String[] item = { "唐僧", "孙悟空 ", "猪八戒", "沙和尚" }; 
    private String[][] ability = { { "会念紧箍咒", "会说阿弥陀佛" }, 
            { "会七十二变", "会打妖精", "会腾云驾雾" }, { "会偷懒", "会睡觉" }, { "会挑担子" } }; 
    private ExpandableListView explandListView; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 通过ID查找到main.xml中的TextView控件 
        text = (TextView) findViewById(R.id.text); 
 
        // 通过ID查找到main.xml中的ExpandableListView控件 
        explandListView = (ExpandableListView) findViewById(R.id.expandableListView); 
         
        //设置ExpandableListView适配器 
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() 
        { 
             
            //处理子项目的单击事件 
            @Override 
            public boolean isChildSelectable(int groupPosition, 
                    int childPosition) 
            { 
                String str = item[groupPosition] 
                        + ability[groupPosition][childPosition]; 
                updateText(str); 
                return true; 
            } 
 
            @Override 
            public boolean hasStableIds() 
            { 
                return true; 
            } 
             
            //返回父项目的视图控件 
            @Override 
            public View getGroupView(int groupPosition, boolean isExpanded, 
                    View convertView, ViewGroup parent) 
            { 
                //新建一个线性布局 
                LinearLayout ll = new LinearLayout(ButtonDemoActivity.this); 
                // 设置布局样式为Horizontal 
                ll.setOrientation(0); 
                //设置布局左边距为50像素 
                ll.setPadding(50, 0, 0, 0); 
                //新建一个ImageView对象 
                ImageView imageView = new ImageView(ButtonDemoActivity.this); 
                //设置ImageView要显示的对象ID 
                imageView.setImageResource(image[groupPosition]); 
                //将ImageView加到线性布局中 
                ll.addView(imageView); 
                //使用自定义文本框 
                TextView textView = getTextView(); 
                //设置文本框里显示内容 
                textView.setText(getGroup(groupPosition).toString()); 
                //将TextView加到线性布局中 
                ll.addView(textView); 
                return ll; 
            } 
             
            //返回父控件的ID 
            @Override 
            public long getGroupId(int groupPosition) 
            { 
                return groupPosition; 
            } 
             
            //返回父控件的总数 
            @Override 
            public int getGroupCount() 
            { 
 
                return ability.length; 
            } www.2cto.com
             
            //取得父控件对象 
            @Override 
            public Object getGroup(int groupPosition) 
            { 
                return item[groupPosition]; 
            } 
             
            //取得子控件的数量 
            @Override 
            public int getChildrenCount(int groupPosition) 
            { 
                return ability[groupPosition].length; 
            } 
             
            //取得子控件的视图 
            @Override 
            public View getChildView(int groupPosition, int childPosition, 
                    boolean isLastChild, View convertView, ViewGroup parent) 
            {    
                //使用自定义TextView控件 
                TextView textView = getTextView(); 
                //设置自定义TextView控件的内容 
                textView.setText(getChild(groupPosition, childPosition) 
                        .toString()); 
                return textView; 
            } 
             
            //取得子控件的ID 
            @Override 
            public long getChildId(int groupPosition, int childPosition) 
            { 
                return childPosition; 
            } 
             
            //取得子控件的对象 
            @Override 
            public Object getChild(int groupPosition, int childPosition) 
            { 
                return ability[groupPosition][childPosition]; 
            } 
             
            //自定义文本框 
            public TextView getTextView() 
            { 
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams( 
                        ViewGroup.LayoutParams.FILL_PARENT, 64); 
                TextView textView = new TextView(ButtonDemoActivity.this); 
                textView.setLayoutParams(lp); 
                textView.setPadding(20, 0, 0, 0); 
                //设置TextView控件为向左,水平居中对齐 
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
 
                return textView; 
 
            } 
        }; 
 
        explandListView.setAdapter(adapter); 
 
 
    } 
 
    private void updateText(String string) 
    { 
        // 将文本信息设置给TextView控件显示出来 
        text.setText(string); 
    } 
 

 

 

 

其实ExpandableListView控件使用起来还是比较方便的,只要学会了BaseExpandableListAdapter()的使用方法就可以了,当然,这里面的布局都是在代码部分生成的,为了扩展方便,也可以使用Layout.xml布局文件来生成.这就留到课后,同学们自己想吧?主要是使用LayoutInflater,好了,今天这一课结束了,谢谢

 

摘自:kangkangz4的专栏

点击复制链接 与好友分享!回本站首页
上一篇:Android[初级教程]第七章 GridView控件
下一篇:Android[初级教程] 第九章 Gallery控件和ImageSwitcher控件
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站