2015-04-26 1 views
0

У меня есть ящик для навигации, который использует RecyclerView макет. В то же время моя основная страница также использует макет RecyclerView. Как управлять им оптимально?Могу ли я использовать ту же структуру макета (как контейнер) для отображения DrawerLayout и RecyclerView?

Это мой activity_main.xml

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

<!--Toolbar--> 
<include 
    android:id="@+id/toolbar_actionbar" 
    layout="@layout/toolbar_default" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<!-- A RecyclerView with some commonly used attributes --> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/my_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="vertical" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:baselineAlignBottom="true" 
    android:layout_marginBottom="12dp" 
    android:src="@mipmap/ic_action_good" /> 

<!--Mulai layout drawer--> 
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar_actionbar"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true"> 
     </FrameLayout> 

    <!--Fragment Drawer--> 
    <fragment 
     android:id="@+id/fragment_drawer" 
     android:name="com.mundane.hortipedia.NavigationDrawerFragment" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

Я просто хочу, чтобы принести мои данные из базы данных SQLite и показать их R.id.my_recycler_view. Но это не сработает.

+0

http://prntscr.com/6y9wxk << мой макет схемы – eto

+1

я думаю андроид принципы говорит, что drawerlayout должен быть корневой контейнер вашего XML. см. Это https://developer.android.com/training/implementing-navigation/nav-drawer.html – Shivansh

+0

Хорошо. поэтому разрешено помещать Recyclerview (my_recycler_view) внутри FrameLayout (контейнер), или мне нужно создать новый фрагмент для my_recycler_view, а затем заменить контейнер. что лучше? – eto

ответ

1

Попробуйте это как основной макет, так как в соответствии с рекомендациями по android drawerlayout должен быть корневым контейнером, так как первый дочерний контейнер drawerlayout является главной страницей, а второй - ящиком.

<?xml version="1.0" encoding="utf-8"?> 
<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"> 

    <FrameLayout 
     android:id="@+id/fl_main_container" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <!-- A RecyclerView with some commonly used attributes --> 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/my_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:baselineAlignBottom="true" 
      android:layout_marginBottom="12dp" 
      android:src="@mipmap/ic_action_good" /> 

     <!--Toolbar--> 
     <include 
      android:id="@+id/toolbar_actionbar" 
      layout="@layout/toolbar_default" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </FrameLayout> 

    <!--Framelayout Drawer--> 
    <FrameLayout 
     android:id="@+id/fl_drawer_container" 
     android:layout_width="give the desired width here" 
     android:layout_height="fill_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/my_recycler_view2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 
    </FrameLayout> 

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

Я надеюсь, что это поможет

+0

Код только anwers обескуражен в SO. – Emmanuel

+0

скрыть другие элементы. Когда я заменил его на свой фрагмент, ** app: layout = "@ layout/fragment_navigation_drawer" /> ** не работает должным образом. У меня есть recyclerview, который вызван фрагментным элементом. – eto

+0

Прошу прощения за то, что вы слишком прямо ответили. я обновлю свой ответ – Shivansh

Смежные вопросы