ExpandableListview二级菜单

本文介绍了一个使用Android中的ExpandableListView组件的具体实现案例。通过定义布局文件及对应的MainActivity和适配器代码,展示了如何创建分组列表,并为每个分组设置子项。此示例涵盖了XML布局配置、Java代码逻辑及视图呈现等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XMLzhong

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ExpandableListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/el"
        android:groupIndicator="@drawable/qqq"
        ></ExpandableListView>

</RelativeLayout>

MainActivty中

package com.bawei.day_03_expandablelistview;

import android.os.Bundle;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandableListView el = (ExpandableListView) findViewById(R.id.el);

        el.setAdapter(new El(MainActivity.this));
    }

}

ExpandableListView适配器中

package com.bawei.day_03_expandablelistview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class El extends BaseExpandableListAdapter {

    Context context;

    String[] zu = new String[] { "一组", "二组", "三组" };

    String[][] chengyuan = new String[][] {

    { "QQ", "王五", "呃呃" }, { "人人", "头条" }, { "语言" } };

    public El(Context context) {
        super();
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return zu.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return chengyuan[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View view = View.inflate(context, R.layout.zu, null);
        TextView zu1 = (TextView) view.findViewById(R.id.text_zu);
        zu1.setText("      "+zu[groupPosition]);

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View view = View.inflate(context, R.layout.cheng, null);
        TextView cheng1 = (TextView) view.findViewById(R.id.text_cheng);
        cheng1.setText("       "+chengyuan[groupPosition][childPosition]);

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        if(groupPosition==0){
            return false;
        }

        return true;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值