Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android常用控件(能折疊的ListView)--ExpandableListView的使用模仿QQ好友列表

Android常用控件(能折疊的ListView)--ExpandableListView的使用模仿QQ好友列表

編輯:關於Android編程

簡介

Expandable:可張開的,就是一層ListView,點擊item之後展開,裡面又有一層ListView,實現的原理和ListView是一樣一樣的,首先,ListView需要一個數據源,而ExpandableListView是套的2個ListView,所以他需要2個數據源;其次ListView需要一個Adapter,ExpandableListView也需要一個Adapter,ListView的BaseAdapter裡面重寫4個方法,ExpandableListView的BaseExpandableListViewAdapter重寫了10個方法,其中8個方法是和ListView裡面的一樣的,來看看: LIstView的BaseAdapter:
 @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }

而ExpandableListView的BaseExpandableListViewAdapter裡面重寫的方法:
 @Override
    public int getGroupCount() {
        return 0;
    }

    @Override
    public int getChildrenCount(int i) {
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

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

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

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        return null;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        return null;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
是不是很像,就多了兩個方法,   public boolean hasStableIds(){}; 是否指定分組視圖及其子視圖的ID對應的後台數據改變也會保持該ID.返回:是否相同的ID總是指向同一個對象. public boolean isChildSelectable(groupPosition ,childPosition){}

指定位置的子視圖是否可選擇. 參數:groupPosition包含要取得子視圖的分組位置.childPosition分組中子視圖的位置. 返回:是否子視圖可選擇.

屬性

android:childDivider:指定各組內子類表項之間的分隔條 注:圖片不會完全顯示,分離子列表項的是一條直線

android:childIndicator:顯示在子列表旁邊的Drawable對象  注:可以是一個圖片

android:childIndicatorLeft:子列表項指示符的左邊約束位置   
注:即從左端0位置開始計數,比如,假設指示符是一個圖標,給定這個屬性值為3dip,則表示從左端起3dip開始顯示此圖標

android:childIndicatorRight:子列表項指示符的右邊約束位置   注:表示右端到什麼位置結束

android:groupIndicator:顯示在組列表旁邊的Drawable對象   注:可以是一個圖片。

android:indicatorLeft:組列表項指示器的左邊約束位置  注:表示左端從什麼位置開始。

android:indicatorRight:組列表項指示器的右邊約束位置   注:表示右端到什麼位置結束。

去掉ExpandableListView 默認的箭頭

 

用到ExpandableListView時有個箭頭圖標系統自帶的在你自定義布局也不能去掉只要設置一個屬性即可, settingLists.setGroupIndicator(null);


demo實現

其他的方法和ListView的方法一樣,下面來看看具體demo的實現 首先布局文件很簡單,就一個控件為:


    


Activity的代碼和用ListView的是一樣的,創建一個adapter和兩個 數據源,然後綁定,具體如下:
package com.duanlian.expendablelistviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

import com.duanlian.expendablelistviewdemo.adapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private MyAdapter myAdapter;
    private List groupList;
    private List> childList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        expandableListView = (ExpandableListView) findViewById(R.id.expendablelistview);
        groupList = new ArrayList<>();
        childList = new ArrayList<>();
        addData("幼稚園同學",new String[]{"周傑倫","江一燕 ","佟麗娅","高圓圓","劉詩詩","劉亦菲","angleBaby","張靜初","張含韻",});
        addData("小學同學",new String[]{"光頭強","熊大","熊二","妙蛙種子","比卡丘","雙蛋瓦斯","貪吃蛇"});
        addData("初中同學",new String[]{"蒼井空","小澤瑪利亞","吉澤明步","波多野結衣","愛川美裡菜","小川阿佐美","桃谷繪裡香","泷澤蘿拉","北原多香子","石川施恩惠","北條麻妃","麻倉優","羽田愛","保坂繪裡"});
        addData("高中同學",new String[]{"習近平","胡錦濤","江澤民","毛澤東","秦始皇","李世民","武則天","曹操","劉備","孫權"});
        addData("大學同學",new String[]{"周傑倫","江一燕 ","佟麗娅","高圓圓","劉詩詩","劉亦菲","angleBaby","張靜初","張含韻",});
        addData("研究生同學",new String[]{"光頭強","熊大","熊二","妙蛙種子","比卡丘","雙蛋瓦斯","貪吃蛇"});
        addData("博士同學",new String[]{"蒼井空","小澤瑪利亞","吉澤明步","波多野結衣","愛川美裡菜","小川阿佐美","桃谷繪裡香","泷澤蘿拉","北原多香子","石川施恩惠","北條麻妃","麻倉優","羽田愛","保坂繪裡"});
        addData("教授同事",new String[]{"習近平","胡錦濤","江澤民","毛澤東","秦始皇","李世民","武則天","曹操","劉備","孫權"});
        addData("眾仙家名冊",new String[]{"蒼井空","小澤瑪利亞","吉澤明步","波多野結衣","愛川美裡菜","小川阿佐美","桃谷繪裡香","泷澤蘿拉","北原多香子","石川施恩惠","北條麻妃","麻倉優","羽田愛","保坂繪裡","習近平","胡錦濤","江澤民","毛澤東","秦始皇","李世民","武則天","曹操","劉備","孫權"});
        myAdapter = new MyAdapter(this,groupList,childList);
        expandableListView.setAdapter(myAdapter);

    }

    /**
     * 用來添加數據的方法
     */
    private void addData(String group, String[] friend) {
        groupList.add(group);
        //每一個item打開又是一個不同的list集合
        List childitem = new ArrayList<>();
        for (int i = 0; i < friend.length; i++) {
            childitem.add(friend[i]);
        }
        childList.add(childitem);
    }
}
  然後Adapter裡面需要2個item布局文件: 外層的item:



    
    


裡面那層item布局:



    
    
    

其中的圓形圖片,前面博客已經講過了,就不說了   最總要的還是在Adapter裡面,他和ListView一樣,也有simpleAdapter和ArrayAdapter,和BaseAdapter,我這裡用的是BaseAdapter,他的adapter繼承的是BaseExpandableListAdapter:
package com.duanlian.expendablelistviewdemo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.duanlian.expendablelistviewdemo.R;

import java.util.List;

/**
 * Created by duanlian on 2016/9/12.
 */
public class MyAdapter extends BaseExpandableListAdapter {
    private List groupList;//外層的數據源
    private List> childList;//裡層的數據源
    private Context context;

    public MyAdapter(Context context, List groupList,List> childList ){
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
    }

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

    /**
     * 這個返回的一定要是對應外層的item裡面的List集合的size
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

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

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

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.item_group, null);
        //分組名字
        TextView textView = (TextView) convertView.findViewById(R.id.group_textview);
        //子元素的個數
        TextView number = (TextView) convertView.findViewById(R.id.group_number);
        number.setText(childList.get(groupPosition).size()+"個");
        textView.setText(groupList.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.item_child, null);
        TextView textView = (TextView) view.findViewById(R.id.child_name);
        //外層的分組名字
        textView.setText(childList.get(groupPosition).get(childPosition));
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved