【问题标题】:Put a ListView and TextView in a NavigationDrawerFragment将 ListView 和 TextView 放入 NavigationDrawerFragment
【发布时间】:2015-08-27 14:28:51
【问题描述】:

我成功创建了一个ListView,并使用AndroidStudio提供的示例创建滑动菜单,将其显示为滑动菜单

现在我想在 ListView 上方添加一个布局(显示登录用户的名称和图像,但为了简化我现在只想放置一个 TextView)。

我尝试了以下方法,但没有成功:

layout_menu.xml(当我想显示菜单时出现的布局。已修改为放置 textview)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NavigationDrawerFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="User logged in"/>

    <ListView
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/fondo_menu"
        android:id="@+id/listaMenu"
        tools:context=".NavigationDrawerFragment" />
</LinearLayout>

activity_base.xml(显示菜单的activity使用的,未修改)

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".BaseActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container">
    </ViewFlipper>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.proyecto.cutcsa.cutcsa.Interfaz.ElementosUI.NavigationDrawerFragment"
        tools:layout="@layout/layout_menu"/><!--tools:layout="@layout/fragment_navigation_drawer" -->

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

NavigationDrawerFragment 类(仅修改 onCreateView 以显示新菜单)

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        /*start modified code*/
        View view = inflater.inflate(R.layout.layout_menu, container, false);
        mDrawerListView = (ListView) view.findViewById(R.id.listaMenu);
        /*finish modified code*/
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
    }

但我收到了java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 此错误出现在 onCreate() 上,尤其是在我要显示菜单的活动中的 setContentView(R.layout.activity_base) 行上。

我定义的onCreate方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Bundle bundle = getIntent().getExtras();

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        try{
            mTitle = bundle.getString("title_menu");
        }catch(Exception ex){}

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

我做错了什么?

【问题讨论】:

  • 你的 Logcat 说异常发生在哪一行,那一行究竟是什么?
  • 刚刚添加到版本中
  • 您能否为您的活动发布onCreate(),因为这是发生错误的地方?
  • 为什么你的导航抽屉是一个片段?您可以在主要活动上使用布局。
  • 我建议使用android support design library 及其NavigationView。这样你就可以很容易地创建一个好看的抽屉。

标签: android android-listview navigation-drawer


【解决方案1】:

查看下面的链接,您将获得所需的一切。 一个非常简单的抽屉式导航示例。

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

【讨论】:

    【解决方案2】:

    Android Design Support Library 大大简化了这种模式

    文章说要像这样创建你的抽屉:

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer"
    />
    </android.support.v4.widget.DrawerLayout>
    

    headerLayout 是指位于列表上方的布局(您的帐户详细信息布局)。菜单是一个标准的菜单资源,一个例子是:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/nav_group_fragments"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home_one"
            android:checked="true"
            android:title="@string/home"
            android:icon="@drawable/ic_home"/>
    
        <item android:id="@+id/nav_offers"
            android:icon="@drawable/ic_my_offers"
            android:title="@string/offers"/>
    
        <item
            android:id="@+id/nav_aroundme"
            android:title="@string/aroundme"
            android:icon="@drawable/ic_around_me"/>
    </group>
    
    <group android:id="@+id/nav_group_activities">
    
        <item
            android:id="@+id/nav_help"
            android:title="@string/help"
            android:icon="@drawable/ic_help"/>
    
        <item android:id="@+id/nav_settings"
            android:title="@string/action_settings"
            android:icon="@drawable/ic_settings"/>
    
        <item android:id="@+id/nav_logout"
            android:icon="@drawable/ic_disconnect"
            android:title="@string/disconnect"/>
    
    </group>
    </menu>
    

    会自动为每个具有您创建的 id 的组插入分隔符

    【讨论】:

    • 不必要的工作和非回收视图......很高兴你的记忆。
    • 我在 headerLayout 和菜单上找不到资源标识符。它似乎无法识别那些 XML 属性
    • 您需要包含支持库才能使用这些东西:在您的应用的 build.gradle 中编译 'com.android.support:design:22.2.0'
    【解决方案3】:

    你试图给你的片段充气 2 次,在这里:

    <fragment android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="com.proyecto.cutcsa.cutcsa.Interfaz.ElementosUI.NavigationDrawerFragment"
            tools:layout="@layout/layout_menu"/><!--tools:layout="@layout/fragment_navigation_drawer" -->
    

    您可以将此布局(导航抽屉内容)添加到您的活动中:

    <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_layout"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <!-- USE VIEWPAGER INSTEAD -->
        <ViewFlipper
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/container">
        </ViewFlipper>
    
        <LinearLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="User logged in"/>
    
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:background="@color/fondo_menu"
                android:id="@+id/listaMenu"/>
    
        </LinearLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 以及所有嵌入显示/隐藏菜单的代码,是如何处理的?
    • android:layout_gravity="start" 会控制它
    • 请减少 ViewGroup 依赖。
    猜你喜欢
    • 2012-10-17
    • 2013-03-10
    • 2012-09-07
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多