【问题标题】:How to create a dropdown within a Navigation Drawer (in Android)?如何在导航抽屉中创建下拉菜单(在 Android 中)?
【发布时间】:2015-05-18 00:40:57
【问题描述】:

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

...基本上概述了HERE

SIMILARLY HERE (though all, not some are dropdowns) .而且喜欢THIS ANSWER

【问题讨论】:

    标签: android android-fragments expandablelistview navigation-drawer


    【解决方案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));
        }
    

    【讨论】:

    • groupItemchildItem 是什么。什么是 NewAdapter,它如何处理 groupItemchildItem 以使某些项目可扩展?您的答案不完整。
    • groupItem 是一个数组,其中包含每个 Group 选项的字符串。上图中的“TopView 2”。 childItem 是一个矩阵/哈希图,其中包含在选择每个组项目后要下拉的项目列表。 NewAdapter 在膨胀每个项目的持有人时将 groupItem 和 childItem 数据集的值分配给视图。
    【解决方案2】:

    你应该有一个类具有所有实现的方法 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]);
                }
            }
        }
    

    ...至于布局视图,这就是我的 group_items.xml 的样子

    <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"/>
    

    不够清楚?,随时评论

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 2014-03-14
      相关资源
      最近更新 更多