二级列表使用方法

本文介绍了一个使用 Android 中 ExpandableListView 控件的具体示例。该示例展示了如何创建一个多级菜单,包括设置适配器、加载布局文件、定义监听器等步骤,并提供了完整的 Java 代码实现。
摘要由CSDN通过智能技术生成
public class MainActivity extends Activity {
    protected static final String TAG = "MainActivity";
    private ExpandableListView exp;
    private AdapterDemo ad;
    String[] group = { "魏", "蜀", "吴" };
    String[][] names = { { "曹胚", "曹冲", "曹植", "曹洪" },
            { "郭嘉", "陈宫", "陈群", "陆抗" }, { "华雄", "费伟", "黄忠", "黄盖", } };
    int[][] image = {
            { R.drawable.cp, R.drawable.cc, R.drawable.cz, R.drawable.ch },
            { R.drawable.gj, R.drawable.cg, R.drawable.cq, R.drawable.lk },
            { R.drawable.hx, R.drawable.fw, R.drawable.hz, R.drawable.hg } };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        ad = new AdapterDemo(group, names, image, this);
        exp.setAdapter(ad);
        exp.setOnChildClickListener(listener);
    }
    // 初始化控件信息
    private void init() {
        exp = (ExpandableListView) findViewById(R.id.exp);
    }

    // 监听
    OnChildClickListener listener = new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            Log.d(TAG,"=====");
            //
            Toast.makeText(MainActivity.this,"你点击了"+names[groupPosition][childPosition],0).show();
            return false;
        }
    };
}
public class AdapterDemo extends BaseExpandableListAdapter {
    private String[] group;
    private String[][] names;
    private int[][] image;
    private Context context;

    public AdapterDemo(String[] group, String[][] names, int[][] image,
            Context context) {
        super();
        this.group = group;
        this.names = names;
        this.image = image;
        this.context = context;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return names[groupPosition][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
        String gp = group[groupPosition];
        convertView = View.inflate(context,
                android.R.layout.simple_expandable_list_item_1, null);
        TextView tx=(TextView) convertView.findViewById(android.R.id.text1);
        tx.setText(gp);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String name=names[groupPosition][childPosition];
        int tp=image[groupPosition][childPosition];
        convertView=View.inflate(context,R.layout.item,null);
        ImageView ima=(ImageView) convertView.findViewById(R.id.image);
        TextView tv=(TextView) convertView.findViewById(R.id.name);
        ima.setImageResource(tp);
        tv.setText(name);
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //
        return true;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值