zoukankan      html  css  js  c++  java
  • 【Android】ExpandableListView简单的使用方法

    activity_main.xml

    <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"
        >
    
        <ExpandableListView
            android:id="@+id/expandableListView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
        </ExpandableListView>
    
    </RelativeLayout>

    MainActivity.java

    package com.example.expandablelistview;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Gravity;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.Toast;
    import android.widget.ExpandableListView.OnGroupClickListener;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        ExpandableListView expandableListView;
        private List<String> GroupData;//定義組數據    
        private List<List<String>> ChildrenData;//定義組中的子數據  
        private void LoadListDate() {    
            GroupData = new ArrayList<String>();    
            GroupData.add("國家");    
            GroupData.add("人物");    
            GroupData.add("武器");    
        
            ChildrenData = new ArrayList<List<String>>();    
            List<String> Child1 = new ArrayList<String>();    
            Child1.add("蜀國");    
            Child1.add("魏國");  
            Child1.add("吳國");  
            ChildrenData.add(Child1);    
            List<String> Child2 = new ArrayList<String>();    
            Child2.add("關羽");    
            Child2.add("張飛");    
            Child2.add("典韋");    
            Child2.add("呂布");  
            Child2.add("曹操");  
            Child2.add("甘寧");  
            Child2.add("郭嘉");  
            Child2.add("周瑜");  
            ChildrenData.add(Child2);    
            List<String> Child3 = new ArrayList<String>();    
            Child3.add("青龍偃月刀");    
            Child3.add("丈八蛇矛槍");    
            Child3.add("青鋼劍");    
            Child3.add("麒麟弓");    
            Child3.add("銀月槍");    
            ChildrenData.add(Child3);    
        }    
        boolean  flags;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LoadListDate();    
            expandableListView=(ExpandableListView) findViewById(R.id.expandableListView1);
            expandableListView.setAdapter(new ExpandableAdapter()); 
           // ExpandableListView 默认展开
            for(int i = 0; i < 3; i++){            
                   expandableListView.expandGroup(i);                                    
                }
            //去掉ExpandableListView默认的箭头 
            expandableListView.setGroupIndicator(null);
         //flags=false;
    //ExpandableListView点击Group不收缩,群组点击事件屏蔽 expandableListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

              //if (flags==true) {
              //  expandableListView.expandGroup(groupPosition);
              //  flags=false;
              //}else {
              //  expandableListView.collapseGroup(groupPosition);
              //  flags=true;
              //}

                    Toast.makeText(MainActivity.this, "群组:"+groupPosition, Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
            //ExpandableListView点击Child提醒
            expandableListView.setOnChildClickListener(new OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    Toast.makeText(MainActivity.this, "群组:"+groupPosition+"
    "+"小节:"+childPosition, Toast.LENGTH_SHORT).show();
                    return true;
                    
                }
            });
        }
    
        private class ExpandableAdapter extends BaseExpandableListAdapter {    
            @Override  
            public Object getChild(int groupPosition, int childPosition) {  
                return ChildrenData.get(groupPosition).get(childPosition);  
            }  
      
            @Override  
            public long getChildId(int groupPosition, int childPosition) {  
                return 0;  
            }  
      
            @Override  
            public View getChildView(int groupPosition, int childPosition,  
                    boolean isLastChild, View convertView, ViewGroup parent) {  
                TextView myText = null;    
                if (convertView != null) {    
                    myText = (TextView)convertView;    
                    myText.setText(ChildrenData.get(groupPosition).get(childPosition)); 
                    
                } else {    
                    myText = createView(ChildrenData.get(groupPosition).get(childPosition));    
                    
                }    
                return myText;    
            }  
      
            @Override  
            public int getChildrenCount(int groupPosition) {  
                return ChildrenData.get(groupPosition).size();  
            }  
      
            @Override  
            public Object getGroup(int groupPosition) {  
                return GroupData.get(groupPosition);  
            }  
      
            public int getGroupCount() {  
                return GroupData.size();  
            }  
      
            @Override  
            public long getGroupId(int groupPosition) {  
                return 0;  
            }  
      
            @Override  
            public View getGroupView(int groupPosition, boolean isExpanded,  
                    View convertView, ViewGroup parent) {  
                TextView myText = null;    
                if (convertView != null) {    
                    myText = (TextView)convertView;    
                    myText.setText(GroupData.get(groupPosition)); 
                    //设置GroupView内容居中和颜色
                    myText.setGravity(Gravity.CENTER);
                    myText.setBackgroundColor(android.graphics.Color.GRAY);
                } else {    
                    myText = createView(GroupData.get(groupPosition)); 
                    //设置GroupView内容居中和颜色
                    myText.setGravity(Gravity.CENTER);
                    myText.setBackgroundColor(android.graphics.Color.GRAY);
                }    
                return myText;  
            }  
      
            @Override  
            public boolean hasStableIds() {  
                return false;  
            }  
      
            @Override  
            public boolean isChildSelectable(int groupPosition, int childPosition) {  
                //设置小节可被点击
                return true;  
            }    
            private TextView createView(String content) {    
                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(      
                        ViewGroup.LayoutParams.FILL_PARENT, 80);      
                TextView myText = new TextView(MainActivity.this);      
                myText.setLayoutParams(layoutParams);      
                myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);      
                myText.setPadding(80, 0, 0, 0);      
                myText.setText(content);    
                return myText;    
            }  
        }    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }

     下面介绍如何从ExpandableListView中取值:

      //ExpandableListView点击Child提醒
            expandableListView.setOnChildClickListener(new OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    Toast.makeText(NameListActivity.this, ChildrenData.get(groupPosition).get(childPosition)+"
    "+GroupData.get(groupPosition), Toast.LENGTH_SHORT).show();
                    return true;
                    
                }
            });
                              作者:xubuhang                出处:http://www.cnblogs.com/xubuhang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    SP1716 GSS3
    A Simple Problem with Integers题解
    P4528 [CTSC2008]图腾 题解
    P1498 南蛮图腾 题解
    P2024 [NOI2001]食物链 题解
    Windows编程 Windows程序的生与死(中)
    Windows编程 Windows程序的生与死(上)
    C#实现在注册表中保存信息
    沿路径动画(Animation Along a Path)
    倾斜动画(SkewTransform)
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4386343.html
  • Copyright © 2011-2022 走看看