Fragmento de NavigationDrawer con ListView y encabezado personalizado
Estoy teniendo un problema y no puedo solucionarlo. En mi aplicación tengo un cajón de navegación definido como a continuación:
Activity_main.xml
- Barra de herramientas setNavigationOnClickListener breaks ActionbarDrawerToggle funcionalidad
- Calcular la velocidad de una aplicación de navegación sin el método getSpeed ()
- Cómo ocultar ActionBar y NavigationBar después de un cierto tiempo de retardo?
- Android - Fragmentos del cajón de navegación
- Android: Regresar al fragmento anterior en la parte posterior de la prensa
<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" > <!-- As the main content view, the view below consumes the entire space available using match_parent in both dimensions. --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 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" />
En la clase MainActivity, normalmente setContentView pasa este xml: MainActivity.java
setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
En mi NavigationDrawerFragment onCreateView normalmente hincho su xml con esto:
NavigationDrawerFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false); mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } });
Y esto es fragment_navigation_drawer.xml: fragment_navigation_drawer.xml
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <ImageView android:id="@+id/profile_picture" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/> <TextView android:id="@+id/user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/profile_picture" android:textStyle="bold" android:textSize="15sp" /> <TextView android:id="@+id/user_mail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/user_name" android:layout_alignLeft="@id/user_name" android:fontFamily="sans-serif-light" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@android:drawable/ic_menu_close_clear_cancel"/> </RelativeLayout> <ListView 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" android:choiceMode="singleChoice" android:id="@+id/drawer_list_view" android:dividerHeight="0dp" android:background="@color/navigation_drawer_background" />
Cuando ejecuto el código, recibo una excepción en el setContentView de MainActivity diciendo:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Creo que hay algo mal con el diseño de NavigationDrawer. ¿Qué estoy haciendo mal?
EDIT: He cambiado mi diseño a:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- The navigation drawer --> <FrameLayout android:id="@+id/left_drawer_frame" android:layout_width="@dimen/navigation_drawer_dimension" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white"/>
Así que con FrameLayout, en mi actividad uso:
mFragment = new Fragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, mFragment) .commit();
Y yo inflar el fragmento con esto:
View root = inflater.inflate(R.layout.fragment_main, container, false);
Todo parece funcionar
- ¿Es correcta la navegación en la aplicación iosched 13?
- Recycler View No se muestra
- 2 ViewPager no se desplaza en Android 2.3
- Cómo obtener el ID de submenú en la vista de navegación
- ¿Cómo agrego el cajón de navegación a mi código existente?
- Cómo reanudar la actividad en lugar de reiniciar al ir "arriba" de la barra de acción
- Android Diseño de materiales Navegación Vista -> Elemento
- Color de fondo del elemento del cajón de navegación para el elemento seleccionado
Mejor tarde que nunca.
Observando su código (nombres de variables, estructura de diseño, etc.) Supongo que está utilizando Android Studio y creó el proyecto utilizando la plantilla "Gaveta de navegación" proporcionada por el asistente de Android Studio "Crear proyecto".
Si la suposición anterior es verdadera, entonces su problema reside en el onCreateView
de NavigationDrawerFragment
. Debe devolver la vista inflada de onCreateView
y lo que debe devolver en este caso concreto es root
no mDrawerListView
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false); mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } }); return root; // This is wrong // return mDrawerListView; }
Es un error común que ocurre cuando editamos el código generado por IDE.
Espero que esto ayude.
En tu fragmento id: navigation drawer add
tools:layout="@layout/fragment_navigation_drawer"
- ¿Qué significa layout_height = "0dp"?
- Room: el directorio de exportación de esquemas no se proporciona al procesador de anotaciones, por lo que no podemos exportar el esquema