zoukankan      html  css  js  c++  java
  • 09 ExpanableListView 的代码例子

    <span style="font-size:18px;">package com.qf.day09_expandablelistview03;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.ExpandableListView.OnGroupClickListener;
    import android.widget.ExpandableListView.OnGroupCollapseListener;
    import android.widget.ExpandableListView.OnGroupExpandListener;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        
        private ExpandableListView expandableListView;
        
        private String[] groupData = {"朋友","同学","同事"};
        
        private String[][] childData ={{"马云","王健林","马化腾"},{"老马","老王","小马"},{"许中","光头","翠"}};
    
        
        private MyAdapter adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        
            
            adapter = new MyAdapter();
        
            expandableListView.setAdapter(adapter);
            
            //分组的点击事件
            expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
                
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                    
                    Toast.makeText(MainActivity.this, "==onGroupClick==", Toast.LENGTH_SHORT).show();
                    
                    return false;
                }
            });
            
            //子元素的点击事件
            expandableListView.setOnChildClickListener(new OnChildClickListener() {
                
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "==onChildClick=="+childData[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
            
            //分组展开的监听
            expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
                
                @Override
                public void onGroupExpand(int groupPosition) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "==onGroupExpand==", Toast.LENGTH_SHORT).show();
                }
            });
            //分组折叠的监听
            expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
                
                @Override
                public void onGroupCollapse(int groupPosition) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "==onGroupCollapse==", Toast.LENGTH_SHORT).show();
                }
            });
            
            
        }
        
        
        public class MyAdapter extends BaseExpandableListAdapter{
    
            /**
             * 获取分组的个数
             */
            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return groupData.length;
            }
    
            /**
             * 获取当前下标分组里的子元素的个数
             */
            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return childData[groupPosition].length;
            }
    
            /**
             * 获取当前下标分组的数据
             */
            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return groupData[groupPosition];
            }
    
            /**
             * 获取分组下标为groupPosition里子元素的下标为childPosition的数据
             */
            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childData[groupPosition][childPosition];
            }
    
            /**
             * 获取下标为groupPosition分组的id
             */
            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }
    
            /**
             * 获取分组下标为groupPosition 里的子元素下标为childPosition元素的id
             */
            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition;
            }
    
            /**
             * 底层设计
             * 分组和子元素是否有稳定的id   底层的数据改变不会影响他们
             */
            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return false;
            }
    
            /**
             * 分组的视图对象
             * 参数1:分组的位置下标
             * 参数2:当前分组是否展开
             * 参数3:复用的视图
             * 参数4:指明父控件 ExpandableListView
             */
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View view = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.item_group, parent,false);
                TextView tv = (TextView) view.findViewById(R.id.tv_item_group);
                tv.setText(groupData[groupPosition]);
                
                return view;
            }
    
            /**
             * 子元素的视图
             *
             * 参数1:当前分组的下标
             * 参数2:当前子元素的下标
             * 参数3:是否是最后一个子元素视图
             * 参数4:子元素复用视图
             * 参数5:指明父控件 ExpandableListView
             */
            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                
                View view = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.item_child, parent,false);
                TextView tv = (TextView) view.findViewById(R.id.tv_item_child);
                tv.setText(childData[groupPosition][childPosition]);
                
                return view;
            }
    
            /**
             * 子元素能否被点击  false不能被点击   true可以被点击
             */
            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return true;
            }
            
        }
    
    
    
    }
    
    </span>


  • 相关阅读:
    c# 使用MySql的MySqlBulkCopy 出现异常 Loading local data is disabled; this must be enabled on both the client and server sides
    DB2 使用EF Core 查询数据 报错 Object reference not set to an instance of an object.
    关于iis部署的网站访问类型设置
    ABP put与delete类型请求异常 TypeErorr: Failed to fetch
    This request has been blocked; the content must be served over HTTPS.
    Mysql Select的字段必须包含在Group By中如何解决
    企业微信小程序-临时登录凭证校验(code2Session)中获取的userid是加密的
    ABP System.ObjectDisposedException: Cannot access a disposed object.
    SQL server数据库文件(mdfldf)的迁移
    A second operation started on this context before a previous operation completed--ABP
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152276.html
Copyright © 2011-2022 走看看