如何在导航抽屉中创建下拉列表(在Android中)?

时间:2015-03-15 23:55:51

标签: android android-fragments expandablelistview navigation-drawer

我希望能够创建一个导航抽屉,其中包含一些可扩展,可选择的项目和一些不可扩展的项目。 StackOverflow对类似于我的问题的共识指向ExpandableListView的解决方案(这可能不适用于我的想法)。在大多数情况下,人们要求的是一种方法来分隔导航抽屉中的项目,就像GMail应用程序与标签一样,而不是我试图做的......

...基本上概述了HERE enter image description here

SIMILARLY HERE (though all, not some are dropdowns),如THIS ANSWER

2 个答案:

答案 0 :(得分:1)

在DrawerLayout中使用ExpandableListView,如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_commentary_behind_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="Frame text" />
</FrameLayout>
<!-- The navigation drawer -->
    <ExpandableListView
        android:id="@+id/left_drawer2"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="multipleChoice"
        android:dividerHeight="0dp"
        />

</android.support.v4.widget.DrawerLayout>

然后在代码中初始化:

    private DrawerLayout drawer;
    private ExpandableListView drawerList;
    private CheckBox checkBox;
    private ActionBarDrawerToggle actionBarDrawerToggle;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout2);
        drawerList = (ExpandableListView) findViewById(R.id.left_drawer2);
        drawerList.setAdapter(new NewAdapter(this, groupItem, childItem));
    }

答案 1 :(得分:1)

您应该拥有一个类,其中包含ExpandableListAdapter以及ChildItemsInfo类和GroupItemsInfo类的所有实现方法,MainActivity具有点击侦听器以对项目进行分组他们的孩子

......现在更具体......

您可以将其置于getGroupView()MyExpandableListAdapter内的 View ind = convertView.findViewById(R.id.group_indicator); View ind2 = convertView.findViewById(R.id.group_indicator2); if (ind != null) { ImageView indicator = (ImageView) ind; if (getChildrenCount(groupPosition) == 0) { indicator.setVisibility(View.INVISIBLE); } else { indicator.setVisibility(View.VISIBLE); int stateSetIndex = (isExpanded ? 1 : 0); /*toggles down button to change upwards when list has expanded*/ if(stateSetIndex == 1){ ind.setVisibility(View.INVISIBLE); ind2.setVisibility(View.VISIBLE); Drawable drawable = indicator.getDrawable(); drawable.setState(GROUP_STATE_SETS[stateSetIndex]); } else if(stateSetIndex == 0){ ind.setVisibility(View.VISIBLE); ind2.setVisibility(View.INVISIBLE); Drawable drawable = indicator.getDrawable(); drawable.setState(GROUP_STATE_SETS[stateSetIndex]); } } }

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/group_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:textSize="15sp"
    android:textStyle="bold"/>

<ImageView
    android:id="@+id/group_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_down_float"
    android:layout_alignParentRight="true"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

<ImageView
    android:id="@+id/group_indicator2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_up_float"
    android:layout_alignParentRight="true"
    android:visibility="gone"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

...至于布局视图,这就是我的group_items.xml似乎是

的方式
--src
    --app
    --pages
        --home
            --home.component.ts

不够清楚?,每当评论