1. 程式人生 > >android-基礎編程-ExpandableListview

android-基礎編程-ExpandableListview

code dip boolean dap otto nco array count src

ExpandableListView繼承ListView,具有LIstVIew的基本功能。此外具有group/child,由組與子元素組成。

1.布局主要有是三個。

a.主布局:

<ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expandlistview"
        android:dividerHeight="5dp"
        android:background
="#ffffff" android:divider="@drawable/expandchilddivide" android:childDivider="#000000" />

b.Group布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity
="center" android:orientation="vertical" > <TextView android:id="@+id/group_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dip" android:paddingBottom="10dip" android:gravity="center_horizontal"
android:text="122" /> </LinearLayout>

c.Child布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/textOne"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="1"
            />
        <TextView
            android:id="@+id/textTwo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="2"
            />
        <TextView
            android:id="@+id/textThree"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="3"
            />
    </LinearLayout>
</LinearLayout>

2.代碼生成 類似adapter 創建adapter(ExpandableListAdapter、BaseExpandableListAdapter、SimpleExpandableListAdapter 依次繼承關系)

a.創建adapter

//自定義適配器
    class Adapter extends BaseExpandableListAdapter {
        //獲取子元素對象
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        //獲取子元素Id
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //加載子元素並顯示
        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            View view = null;
            ChildHolder childholder = null;
            if (convertView != null) {
                view = convertView;
                childholder = (ChildHolder) view.getTag();
            } else {
                view = View.inflate(ExpandableListViewActi.this, R.layout.expand_child, null);
                childholder = new ChildHolder();
                //childholder.mImage = (ImageView) view.findViewById(R.id.image);
                childholder.mPrice = (TextView) view.findViewById(R.id.textTwo);
                childholder.mStateText = (TextView) view.findViewById(R.id.textOne);
                childholder.mSecondPrice = (TextView) view.findViewById(R.id.textThree);
                view.setTag(childholder);
            }
//            childholder.mImage.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(MainActivity.this, "第"+groupPosition+"組的第"+childPosition+"圖標被點擊了", 0).show();
//                }
//            });
            childholder.mPrice.setText(child_list.get(groupPosition));
            int len = group_list.size();
            System.out.println(len + "-----------------");
            childholder.mStateText.setText(child_list.get(groupPosition));
            childholder.mSecondPrice.setText(child_list.get(groupPosition));
            return view;
        }

        //獲取子元素數目
        @Override
        public int getChildrenCount(int groupPosition) {
            return child_list.size();
        }

        //獲取組元素對象
        @Override
        public Object getGroup(int groupPosition) {
            return group_list.get(groupPosition);
        }

        //獲取組元素數目
        @Override
        public int getGroupCount() {
            return group_list.size();
        }

        //獲取組元素Id
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //加載並顯示組元素
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            View view = null;
            GroupHolder groupholder = null;
            if (convertView != null) {
                view = convertView;
                groupholder = (GroupHolder) view.getTag();
            } else {
                view = View.inflate(ExpandableListViewActi.this, R.layout.expand_group, null);
                groupholder = new GroupHolder();
                groupholder.mSpaceText = (TextView) view.findViewById(R.id.group_text);
                view.setTag(groupholder);
            }
            groupholder.mSpaceText.setText(group_list.get(groupPosition));
            return view;
        }

        @Override
        public boolean hasStableIds() {

            return true;
        }


        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {

            return true;
        }

    }

b.生成主界面代碼,設置adapter .expandable四中點擊相應事件。

private void initView() {
        mListView = (ExpandableListView) findViewById(R.id.expandlistview);
        mInflater = LayoutInflater.from(ExpandableListViewActi.this);
        group_list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            group_list.add("zcx");
            child_list.add("child");
        }
        Adapter adapter = new Adapter();
        //mListView.setGroupIndicator(null);

        /**
         * ExpandableListView的組監聽事件
         */
//        mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//
//            @Override
//            public boolean onGroupClick(ExpandableListView parent, View v,
//                                        int groupPosition, long id) {
//                Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "組被點擊了", 0).show();
//                return true;
//            }
//        });
        /**
         * ExpandableListView的組展開監聽
         */
        mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "組展開", 0).show();
            }
        });
        /**
         * ExpandableListView的組合攏監聽
         */
        mListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "組合攏", 0).show();
            }
        });
        /**
         * ExpandableListView的子元素點擊監聽
         */
        mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "組的第" + childPosition + "被點擊了", 0).show();
                return true;
            }
        });

        mListView.setAdapter(adapter);
//        int groupCount = mListView.getCount();
//        for(int i=0;i<groupCount;i++){
//            mListView.expandGroup(i);
//        }
    }

3.效果圖

技術分享

android-基礎編程-ExpandableListview