BaseExpandableListAdapter應用:QQ好友列表,摺疊式列表

佈局使用控件:ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ExpandableListView
            android:id="@+id/ex_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:visibility="visible" />
</RelativeLayout>

代碼實例:
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinaiat.ciatwild.R;

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

/**
 * Created by Bob on 2016-04-11
 */
public class LinePlanAdapter extends BaseExpandableListAdapter {
    private Context mContext;
//    private ArrayList<String> childData = null;
    private ArrayList<ArrayList<String>> groupData = null;

    public LinePlanAdapter(Context context, ArrayList<ArrayList<String>> groupData) {
        this.mContext = context;
        this.groupData = groupData;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView tv_group = new TextView(mContext);
        tv_group.setPadding(50,0,0,0);
        tv_group.setTextSize(18f);
        tv_group.setText("方案" + (groupData.get(groupPosition).size()));
        return tv_group;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        TextView tv_child=new TextView(mContext);
        tv_child.setText(groupData.get(groupPosition).get(childPosition));
        return tv_child;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupData.get(groupPosition).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupData.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 false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
發佈了29 篇原創文章 · 獲贊 43 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章