Adroid 展開收起效果實現

Layout
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".ExpandableListViewActivity">

    <ExpandableListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ecpandable"
        />
</RelativeLayout>
JAVA代碼

package com.example.wequst.test2;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ExpandableListViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view);
        /**BaseExpandableListAdapter實現了ExpandableListAdapter*/
        final ExpandableListAdapter adapter = new BaseExpandableListAdapter(){

            /**----------定義數組-------------------------------------------------------------------*/
            private int[] images = new int[]{
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher
            };
            private String[] armTypes = new String[]{
                    "神族","蟲族","人族"
            };
            private String[][] arms = new String[][]{
                    {"狂戰士","龍騎士","黑暗聖堂"},
                    {"小狗","飛龍","自爆妃子"},
                    {"步兵","傘兵","護士mm"}
            };

/*===========組元素表示可摺疊的列表項,子元素表示列表項展開後看到的多個子元素項=============*/

            /**----------獲得armTypes和arms中每個元素的ID-------------------------------------------*/

            //獲取組在給定的位置編號,即armTypes中元素的ID
            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            //獲取在給定的組的兒童的ID,就是arms中元素的ID
            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            /**----------根據上面獲得的ID的值,來獲得armTypes和arms中元素的個數 ------------------------*/

            //獲取的羣體數量,獲得armTypes裏元素的個數
            @Override
            public int getGroupCount() {
                return armTypes.length;
            }

            //取得指定組中的兒童人數,就是armTypes中每個種族它軍種的個數
            @Override
            public int getChildrenCount(int groupPosition) {
                return arms[groupPosition].length;
            }

            /**----------利用上面getGroupId獲得ID,從而根據ID獲得armTypes中的數據,並填到TextView中 -----*/

            //獲取與給定的組相關的數據,獲得數組armTypes中元素的數據
            @Override
            public Object getGroup(int groupPosition) {
                return armTypes[groupPosition];
            }

            //獲取一個視圖顯示給定組,存放armTypes
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent) {
                TextView textView = getTextView();//調用定義的getTextView()方法
                textView.setText(getGroup(groupPosition).toString());//添加數據
                return textView;
            }

            /**----------利用上面getChildId獲得ID,從而根據ID獲得arms中的數據,並填到TextView中---------*/

            //獲取與孩子在給定的組相關的數據,獲得數組arms中元素的數據
            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return arms[groupPosition][childPosition];
            }

            //獲取一個視圖顯示在給定的組 的兒童的數據,就是存放arms
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                     View convertView, ViewGroup parent) {
                LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
                ImageView logo = new ImageView(ExpandableListViewActivity.this);
                logo.setImageResource(images[groupPosition]);//添加圖片
                ll.addView(logo);
                TextView textView = getTextView();//調用定義的getTextView()方法
                textView.setText(getChild(groupPosition,childPosition).toString());//添加數據
                ll.addView(textView);
                return ll;
            }

            /**------------------自定義一個設定TextView屬性的方法----------------------------------------------*/

            //定義一個TextView
            private TextView getTextView(){
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,100);
                TextView textView = new TextView(ExpandableListViewActivity.this);
                textView.setLayoutParams(lp);
                textView.setPadding(70, 0, 0, 0);
                textView.setTextSize(20);
                return textView;
            }

            /**-------------------其餘設置-------------------------------------------------------------------*/

            //孩子在指定的位置是可選的,即:arms中的元素是可點擊的
            @Override
            public boolean isChildSelectable(int groupPosition,
                                             int childPosition) {
                return true;
            }

            //表示孩子是否和組ID是跨基礎數據的更改穩定
            public boolean hasStableIds() {
                return true;
            }
        };

        /**使用適配器*/
        final ExpandableListView expandListView = (ExpandableListView) this.findViewById(R.id.ecpandable);
        expandListView.setAdapter(adapter);

        expandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                return true;
            }
        });

        expandListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {

                int i=adapter.getChildrenCount(groupPosition);
                if(i==0)
                {

                }
                return false;   //默認爲false,設爲true時,點擊事件不會展開Group
            }
        });
    }

}
相關文章
相關標籤/搜索