• android ExpandableListView


    1.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"
            >
           <ExpandableListView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/expandableListView"
                />
    </LinearLayout>

    2.BaseExpandableListAdapter

    public class MyListAdapter extends BaseExpandableListAdapter {


        Context context;
        List<String> groupList = new ArrayList<String>();
        List<List<String>> childList = new ArrayList<List<String>>();

        public MyListAdapter(Context context) {
            this.context = context;

            groupList.add("The one group....");
            groupList.add("The two group....");
            groupList.add("The three group....");

            for (int i = 0; i < groupList.size(); i++) {
                List<String> strings = new ArrayList<String>();
                strings.add("This is one child... " + i);
                strings.add("This is two child... " + i);
                childList.add(strings);
            }

        }

        @Override
        public int getGroupCount() {
            return groupList.size();
        }

        @Override
        public int getChildrenCount(int i) {
       return childList.get(0).size();  //每一个Group 中的Child 数目相同
        }

        @Override
        public Object getGroup(int i) {
            return groupList.get(i);
        }

        @Override
        public Object getChild(int i, int i2) {
            return childList.get(i).get(i2);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i2) {
            return i2;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        //父listview视图
        @Override
        public View getGroupView(int groupPosition, boolean groupIsSelected, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.grouplist, null);
            TextView textView = (TextView) view.findViewById(R.id.groupTextView);
            textView.setText(groupList.get(groupPosition));
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            if (groupIsSelected) {
                imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.select02));
            } else {
                imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.select01));
            }
            return view;
        }

        //子listview视图
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean childIsSelected, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.childlist, null);
            TextView textView = (TextView) view.findViewById(R.id.childTextView);
            textView.setText(childList.get(groupPosition).get(childPosition));
            return view;
        }

        //此方法是用来触发子listview 是否可以点击的  默认为false
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    Activity

    public class IndexActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
            expandableListView.setAdapter(new MyListAdapter(this));
            expandableListView.setGroupIndicator(null); //默认ExpandableListView的Group最前面有一个Indicator  可以去掉 也可以自定义
            //        expandableListView.setGroupIndicator(getResources().getDrawable(R.drawable.select));
            expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
                    Toast.makeText(IndexActivity.this, "第" + groupPosition + "组被点击", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
            expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                    Toast.makeText(IndexActivity.this, "第" + groupPosition + "组第" + childPosition + "个被点击", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

    //        expandableListView.setSelectedGroup(0);                   //设置选择指定的组。
    //        expandableListView.setSelectedChild(0, 0, true);           //设置选择指定的子项。
            expandableListView.expandGroup(0);                         //在分组列表视图中 展开一组
    //        expandableListView.isGroupExpanded(0);                     //判断此组是否展开

        }
    }

  • 相关阅读:
    代码格式化[转]
    ASP.NET错误大杂烩
    Web考勤管理系统 .net 2005 开发
    Ftp 类
    c#中Split等分割字符串的几种方法
    强大的firebug 使用 介绍
    一页面多个文本框回车提交不同事件问题解决
    Ajax电子书下载 发现的好东东贴上了
    编程技术书籍[转]
    推荐下权威的《IT十年经典书系列》1打
  • 原文地址:https://www.cnblogs.com/lianghui66/p/3016275.html
Copyright © 2020-2023  润新知