Android UI基礎之 ExpanableListView 的實現


ExpandableListView 組件是Android組件中比較常用的組件,當點擊一個父item 的時候將其自item顯示出來,例如QQ 中的好友列表就是如此實現的,使用ExpandableListView 組件的關鍵就是設置其的Adapter,這個Adapter必須繼承自BaseExpanableListAdapter,因此使用ExpandableListView的核心技術就是使用BaseExpanableListAdapter的使用

實現過程:

效果如下:

 

整體思路:

1:給ExpanableListView 設置適配器,必須設置數據源

2:數據源在實現 ExpanableListView的適配器繼承自BaseExpandableListAdapter,其是ExpanableListView的子類, 需要重寫多個方法,數據源中用到了自定義的View' 佈局,此時根據自己的需求,來設置組合子項的佈局樣式

3:數據源設置好後,直接使用ExpanableListView.SetAdapter ()即可實現此收縮功能

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.xiyou.com.expandablelistview.MainActivity">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expandableListView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Layout_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/group_icon"
    android:src="@mipmap/ga"
    />
 <TextView
     android:layout_width="wrap_content"
     android:text="No Date"
     android:id="@+id/group_title"
     android:layout_height="wrap_content" />
</LinearLayout>

Layout_children.xml :

佈局與Layout_group.xml相同。

代碼如下:

package com.xiyou.com.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/*
可擴展的listView
 */
public class MainActivity extends AppCompatActivity {
    private ExpandableListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ExpandableListView) findViewById(R.id.expandableListView);
        listView.setAdapter(new MyExpandableAdapter());

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, childs[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
    private String[] groups = {"我的好友", "黑名單", "其它"};//組所顯示的內容
    private String[][] childs = {{"潘潘", "迪迪", "敦敦"}, {"賣房子", "賣腎"}, {"英雄聯盟", "刀塔"}};//子項所顯示的內容
    // 適配器
    class MyExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return groups.length;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            return childs[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childs[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.group_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.group_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.group_title);
            imageView.setImageResource(R.drawable.ga);
            textView.setText(groups[groupPosition]);
            return convertView;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            int [] imgs = {
                    R.drawable.a,
                    R.drawable.b,
                    R.drawable.c,
                    R.drawable.d,
                    R.drawable.e,
                    R.drawable.f,
            };
            int i = imgs.length;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.child_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.child_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.child_title);

            imageView.setImageResource(imgs[childPosition]);
            textView.setText(childs[groupPosition][childPosition]);
            return convertView;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}

 

 

                   Android UI基礎之ExpandableListView的用法

ExpandableListView 組件是Android組件中比較常用的組件,當點擊一個父item 的時候將其自item顯示出來,例如QQ 中的好友列表就是如此實現的,使用ExpandableListView 組件的關鍵就是設置其的Adapter,這個Adapter必須繼承自BaseExpanableListAdapter,因此使用ExpandableListView的核心技術就是使用BaseExpanableListAdapter的使用

實現過程:

效果如下:

 

整體思路:

1:給ExpanableListView 設置適配器,必須設置數據源

2:數據源在實現 ExpanableListView的適配器繼承自BaseExpandableListAdapter,其是ExpanableListView的子類, 需要重寫多個方法,數據源中用到了自定義的View' 佈局,此時根據自己的需求,來設置組合子項的佈局樣式

3:數據源設置好後,直接使用ExpanableListView.SetAdapter ()即可實現此收縮功能

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.xiyou.com.expandablelistview.MainActivity">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expandableListView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Layout_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/group_icon"
    android:src="@mipmap/ga"
    />
 <TextView
     android:layout_width="wrap_content"
     android:text="No Date"
     android:id="@+id/group_title"
     android:layout_height="wrap_content" />
</LinearLayout>

Layout_children.xml :

佈局與Layout_group.xml相同。

代碼如下:

package com.xiyou.com.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/*
可擴展的listView
 */
public class MainActivity extends AppCompatActivity {
    private ExpandableListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ExpandableListView) findViewById(R.id.expandableListView);
        listView.setAdapter(new MyExpandableAdapter());

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, childs[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
    private String[] groups = {"我的好友", "黑名單", "其它"};//組所顯示的內容
    private String[][] childs = {{"潘潘", "迪迪", "敦敦"}, {"賣房子", "賣腎"}, {"英雄聯盟", "刀塔"}};//子項所顯示的內容
    // 適配器
    class MyExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return groups.length;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            return childs[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childs[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.group_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.group_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.group_title);
            imageView.setImageResource(R.drawable.ga);
            textView.setText(groups[groupPosition]);
            return convertView;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            int [] imgs = {
                    R.drawable.a,
                    R.drawable.b,
                    R.drawable.c,
                    R.drawable.d,
                    R.drawable.e,
                    R.drawable.f,
            };
            int i = imgs.length;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.child_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.child_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.child_title);

            imageView.setImageResource(imgs[childPosition]);
            textView.setText(childs[groupPosition][childPosition]);
            return convertView;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}

 

 

                   Android UI基礎之ExpandableListView的用法

ExpandableListView 組件是Android組件中比較常用的組件,當點擊一個父item 的時候將其自item顯示出來,例如QQ 中的好友列表就是如此實現的,使用ExpandableListView 組件的關鍵就是設置其的Adapter,這個Adapter必須繼承自BaseExpanableListAdapter,因此使用ExpandableListView的核心技術就是使用BaseExpanableListAdapter的使用

實現過程:

效果如下:

 

整體思路:

1:給ExpanableListView 設置適配器,必須設置數據源

2:數據源在實現 ExpanableListView的適配器繼承自BaseExpandableListAdapter,其是ExpanableListView的子類, 需要重寫多個方法,數據源中用到了自定義的View' 佈局,此時根據自己的需求,來設置組合子項的佈局樣式

3:數據源設置好後,直接使用ExpanableListView.SetAdapter ()即可實現此收縮功能

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.xiyou.com.expandablelistview.MainActivity">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expandableListView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Layout_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/group_icon"
    android:src="@mipmap/ga"
    />
 <TextView
     android:layout_width="wrap_content"
     android:text="No Date"
     android:id="@+id/group_title"
     android:layout_height="wrap_content" />
</LinearLayout>

Layout_children.xml :

佈局與Layout_group.xml相同。

代碼如下:

package com.xiyou.com.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/*
可擴展的listView
 */
public class MainActivity extends AppCompatActivity {
    private ExpandableListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ExpandableListView) findViewById(R.id.expandableListView);
        listView.setAdapter(new MyExpandableAdapter());

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, childs[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
    private String[] groups = {"我的好友", "黑名單", "其它"};//組所顯示的內容
    private String[][] childs = {{"潘潘", "迪迪", "敦敦"}, {"賣房子", "賣腎"}, {"英雄聯盟", "刀塔"}};//子項所顯示的內容
    // 適配器
    class MyExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return groups.length;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            return childs[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childs[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.group_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.group_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.group_title);
            imageView.setImageResource(R.drawable.ga);
            textView.setText(groups[groupPosition]);
            return convertView;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            int [] imgs = {
                    R.drawable.a,
                    R.drawable.b,
                    R.drawable.c,
                    R.drawable.d,
                    R.drawable.e,
                    R.drawable.f,
            };
            int i = imgs.length;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.child_layout, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.child_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.child_title);

            imageView.setImageResource(imgs[childPosition]);
            textView.setText(childs[groupPosition][childPosition]);
            return convertView;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章