ExpandableListView(可展開的列表組件)的說明以及其用法

    摘要提到了,我以前自定義的控件繼承於ListView。顧名思義,ExpandableListView是ListView的子類。它在普通的ListView的基礎上進行了拓展,它把應用中的列表分爲幾組,每組又包含多個列表項。java

ExpandableListView的用法和ListView很是像,只是其所顯示的列表項應該由ExpandableListAdapter提供,下面是它的xml屬性及說明:ide

然而,接下來是用事實說話了:佈局

--------------------------------------------------------------------------------this

一、該項目的佈局文件很是簡單,和ListView差很少,此處就不貼出代碼了。excel

二、ExpandableListViewActivity.java關鍵代碼以下:code

(要定義一個全局的私有的listView變量)server

listView = (ExpandableListView) findViewById(R.id.list);

MyExpandableListAdapter adapter = new MyExpandableListAdapter();
listView.setAdapter(adapter);

三、MyExpandableListAdapter代碼以下:xml

public class MyExpandableListAdapter implements ExpandableListAdapter {
    int[] logos = new int[]{
            R.drawable.word,
            R.drawable.excel,
            R.drawable.email,
            R.drawable.ppt
    };
    private String[] armTypes = new String[]{
            "WORD", "EXCEL", "EMAIL", "PPT"
    };
    private String[][] arms = new String[][]{
            {"文檔編輯", "文檔排版", "文檔處理", "文檔打印"},
            {"表格編輯", "表格排版", "表格處理", "表格打印"},
            {"收發郵件", "管理郵箱", "登陸登出", "註冊綁定"},
            {"演示編輯", "演示排版", "演示處理", "演示打印"},
    };

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return armTypes.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arms[groupPosition].length;
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arms[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 true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ImageView logo = new ImageView(ExpandableListViewActivity.this);
        logo.setImageResource(logos[groupPosition]);
        logo.setPadding(36, 15, 0, 0);
        ll.addView(logo);
        TextView textView = getTextView();
        textView.setText(getGroup(groupPosition).toString());
        textView.setPadding(10, 0, 0, 0);
        ll.addView(textView);
        return ll;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }

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

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

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

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    private TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
        TextView textView = new TextView(ExpandableListViewActivity.this);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36, 0, 0, 0);
        textView.setTextSize(20);
        return textView;
    }
}

代碼上面沒有作多少註釋,在這裏說明一下吧:對象

該adapter有兩個關鍵方法:getChildView()和getGroupView();繼承

        ----------前者返回的view對象做爲子列表項,後者返回的view做爲組列表項。

四、下面是該程序的運行界面了:

  五、 就到這兒告終束吧,     謝謝觀看。

相關文章
相關標籤/搜索