如何使用子项实现可扩展的android导航抽屉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23195740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:52:38  来源:igfitidea点击:

How to implement Expandable android navigation drawer with subitems?

androidexpandablelistviewnavigation-drawer

提问by Sagi

How to implement android navigation drawer like this?

如何实现这样的android导航抽屉?

TopLevelView1 ~ TopLevelView4 can select and no children
TopVevelView5 can collaspe

TopLevelView1 ~ TopLevelView4 可以选择并且没有孩子
TopVevelView5 可以折叠

My question is that if my group structure like this for example

我的问题是,如果我的组结构是这样的

All
Stared
Category
----mp3
----txt
----doc
----pdf

所有
凝视
类别
----mp3
----txt
----doc
----pdf

when I select all then show all file.

当我选择全部然后显示所有文件。

when I select stared then show stared file only.

当我选择盯着然后只显示盯着文件。

when I select mp3 then show only mp3 files.

当我选择 mp3 时,只显示 mp3 文件。

and Category can expand and collapse.

和类别可以扩展和折叠。

https://developer.android.com/design/media/navigation_drawer_collapse.png

https://developer.android.com/design/media/navigation_drawer_collapse.png

回答by canova

For navigation:

对于导航:

  • Alternative 1:

    Sliding Menu, which I would definitely go with. Even used by popular application like LinkedIn and Foursquare and easy to implement and use. Full explanation and example source codes: SlidingMenu - GitHub

  • Alternative 2:

    Android Navigation Drawer. If you want to fully customise everything yourself without using any libraries, this is your option. You can check codes and how to do it from Android Developers website: Creating a Navigation Drawer

  • 备选方案 1:

    滑动菜单,我肯定会去。甚至被 LinkedIn 和 Foursquare 等流行应用程序使用,并且易于实现和使用。完整解释和示例源代码:SlidingMenu - GitHub

  • 备选方案 2:

    安卓导航抽屉。如果您想在不使用任何库的情况下完全自定义所有内容,这是您的选择。您可以从 Android 开发者网站查看代码以及如何操作:Creating a Navigation Drawer

View inside your navigation drawer / sliding menu:

在导航抽屉/滑动菜单中查看:

  • Alternative 1:

    Android default ExpandableListView. Links: Android Developers, androidhive

  • Alternative 2:

    AnimatedExpandableListView, which is implemented from ExpandableListView, but when an item is clicked, the expand is done with a smooth animation which you may prefer to use for a better look. AnimatedExpandableListView

  • 备选方案 1:

    Android 默认 ExpandableListView。链接:Android 开发者androidhive

  • 备选方案 2:

    AnimatedExpandableListView,它是从 ExpandableListView 实现的,但是当一个项目被点击时,展开是一个平滑的动画,你可能更喜欢使用它以获得更好的外观。动画可扩展列表视图

回答by Wishmaster

Try something like that

尝试这样的事情

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/drawer_list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start" >

        <ExpandableListView
            android:id="@+id/drawer_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"/>
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

Java code:

爪哇代码:

drawerListView.setAdapter(new ExpandableListAdapter() {

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub

            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onGroupExpanded(int groupPosition) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onGroupCollapsed(int groupPosition) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                TextView view = new TextView(getApplicationContext());
                view.setText("group " + groupPosition);
                return view;
            }

            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }

            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return 5;
            }

            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getCombinedGroupId(long groupId) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getCombinedChildId(long groupId, long childId) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return 5;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                    ViewGroup parent) {
                TextView view = new TextView(getApplicationContext());
                view.setText("child " + groupPosition);
                return view;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition ;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean areAllItemsEnabled() {
                // TODO Auto-generated method stub
                return false;
            }
        });