在android裡用ExpandableListView實現二層和三層列表源碼

來源:互聯網
上載者:User

二層列表是直接用androidAPI中的ExpandableListView即可實現,三層列表其實是對二層列表的一個嵌套,實現起來會相對繁瑣。

部分代碼:

SuperTreeViewAdapter.java

public class SuperTreeViewAdapter extends BaseExpandableListAdapter {    static public class SuperTreeNode {        Object parent;        //二級樹形菜單的結構體        List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();    }    private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();    private Context parentContext;    private OnChildClickListener stvClickEvent;//外部回呼函數        public SuperTreeViewAdapter(Context view,OnChildClickListener stvClickEvent) {        parentContext = view;        this.stvClickEvent=stvClickEvent;    }    public List<SuperTreeNode> GetTreeNode() {        return superTreeNodes;    }    public void UpdateTreeNode(List<SuperTreeNode> node) {        superTreeNodes = node;    }        public void RemoveAll()    {        superTreeNodes.clear();    }        public Object getChild(int groupPosition, int childPosition) {        return superTreeNodes.get(groupPosition).childs.get(childPosition);    }    public int getChildrenCount(int groupPosition) {        return superTreeNodes.get(groupPosition).childs.size();    }    public ExpandableListView getExpandableListView() {        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                ViewGroup.LayoutParams.FILL_PARENT, TreeViewAdapter.ItemHeight);        ExpandableListView superTreeView = new ExpandableListView(parentContext);        superTreeView.setLayoutParams(lp);        return superTreeView;    }    /**     * 三層樹結構中的第二層是一個ExpandableListView     */        public View getChildView(int groupPosition, int childPosition,            boolean isLastChild, View convertView, ViewGroup parent) {        // 是         final ExpandableListView treeView = getExpandableListView();        final TreeViewAdapter treeViewAdapter = new TreeViewAdapter(this.parentContext,0);        List<TreeNode> tmp = treeViewAdapter.getTreeNode();//臨時變數取得TreeViewAdapter的TreeNode集合,可為空白        final TreeNode treeNode=(TreeNode) getChild(groupPosition, childPosition);        tmp.add(treeNode);        treeViewAdapter.updateTreeNode(tmp);        treeView.setAdapter(treeViewAdapter);                //關鍵點:取得選中的二級樹形菜單的父子節點,結果返回給外部回呼函數        treeView.setOnChildClickListener(this.stvClickEvent);                /**         * 關鍵點:第二級菜單展開時通過取得節點數來設定第三級菜單的大小         */        treeView.setOnGroupExpandListener(new OnGroupExpandListener() {            @Override            public void onGroupExpand(int groupPosition) {                                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                        ViewGroup.LayoutParams.FILL_PARENT,                        (treeNode.childs.size()+1)*TreeViewAdapter.ItemHeight + 10);                treeView.setLayoutParams(lp);            }        });                /**         * 第二級菜單回收時設定為標準Item大小         */        treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {            @Override            public void onGroupCollapse(int groupPosition) {                                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,                        TreeViewAdapter.ItemHeight);                treeView.setLayoutParams(lp);            }        });        treeView.setPadding(TreeViewAdapter.PaddingLeft*2, 0, 0, 0);        return treeView;    }    /**     * 三級樹結構中的首層是TextView,用於作為title     */    public View getGroupView(int groupPosition, boolean isExpanded,            View convertView, ViewGroup parent) {        TextView textView = TreeViewAdapter.getTextView(this.parentContext);        textView.setText(getGroup(groupPosition).toString());        textView.setPadding(TreeViewAdapter.PaddingLeft*2, 0, 0, 0);        return textView;    }    public long getChildId(int groupPosition, int childPosition) {        return childPosition;    }    public Object getGroup(int groupPosition) {        return superTreeNodes.get(groupPosition).parent;    }    public int getGroupCount() {        return superTreeNodes.size();    }    public long getGroupId(int groupPosition) {        return groupPosition;    }    public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }    public boolean hasStableIds() {        return true;    }}

ExpandableListViewActivity.java

List<SuperTreeViewAdapter.SuperTreeNode> superNodeTree = superAdapter.GetTreeNode();                for (int i = 0; i < parent.length; i++) {                    SuperTreeViewAdapter.SuperTreeNode superNode = new SuperTreeViewAdapter.SuperTreeNode();                    superNode.parent = parent[i];    for (int j = 0; j < child_grandchild.length; j++) {                        TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();                        node.parent = child_grandchild[j][0][0];                       for (int k = 0; k < child_grandchild[j][1].length; k++) {                          node.childs.add(child_grandchild[j][1][k]);                        }                          superNode.childs.add(node);                      }                      superNodeTree.add(superNode);                  }          superAdapter.UpdateTreeNode(superNodeTree);                expandableListView.setAdapter(superAdapter);    

main.xml

<LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center" >        <Button            android:id="@+id/button1"            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="二層結構" />        <Button            android:id="@+id/button2"            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="三層結構" />    </LinearLayout>    <ExpandableListView        android:id="@+id/expandablelistview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        >    </ExpandableListView>

三層列表

 

二層列表

 源碼下載:ExpandableListView.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.