2016-06-24 2 views
1

Во-первых, у меня была панель инструментов, после чего я добавил ресайлер, но теперь панель инструментов исчезла. Я попробовал все, чтобы отобразить панель инструментов, но я всегда терпел неудачу. Можете ли вы рассказать мне, как я могу вернуть панель инструментов? Заранее спасибо. Вот мой Java код:Панель инструментов не отображается на Android

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{ 

    private Toolbar mToolbar; 
    private FragmentDrawer drawerFragment; 

    private RecyclerView recyclerView; 
    private IsAdapter isAdapter; 
    private List<Is> isList; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mToolbar = (Toolbar) findViewById(R.id.toolbar); 

     setSupportActionBar(mToolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     isList = new ArrayList<>(); 
     isAdapter = new IsAdapter(this, isList); 


     RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(10), true)); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(isAdapter); 

     prepareIsler(); 

     drawerFragment = (FragmentDrawer) 
       getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
     drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
     drawerFragment.setDrawerListener(this); 



     getSupportActionBar().setTitle("TTT"); 
     // display the first navigation drawer view on app launch 
     displayView(0); 


    } 

Вот код макета:

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


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:local="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:minHeight="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       /> 

     <!-- <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" /> --> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.example.neozeka1.dts2.FragmentDrawer" 
     android:layout_width="@dimen/nav_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/white" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="info.androidhive.cardview.MainActivity" 
     tools:showIn="@layout/activity_main"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipToPadding="false" 
      android:scrollbars="vertical" /> 

    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 
+0

Вы хотите получить только один контент 'View' в' DrawerLayout'. Итак, однако вы хотите организовать вещи, все, что не является ящиком, должно быть в одной 'ViewGroup' над вашим ящиком' '. –

+0

@MikeM вы можете показать пример? Большое спасибо – jason

+1

Не совсем, потому что я не могу сказать, что вы пытаетесь сделать. Если «RecyclerView» предназначен для основного контента, просто переместите его внутри самого внешнего «LinearLayout», под 'container_toolbar', и избавьтесь от« RelativeLayout »и« FrameLayout ». Вы можете вырезать 'container_toolbar'' LinearLayout', если ничего не происходит внутри него. –

ответ

1

Сделайте эту иерархию.

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

<!-- Main Layout --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/container_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/container_body" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

<!-- Left Drawer --> 
<fragment 
    android:id="@+id/fragment_navigation_drawer" 
    android:layout_width="@dimen/nav_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:layout="@layout/fragment_navigation_drawer" 
    android:name="com.findthewayapp.fragments.NavigationDrawerFragment" 
    tools:layout="@layout/fragment_navigation_drawer" /> 
</android.support.v4.widget.DrawerLayout> 
+0

Где находится 'RecyclerView'? – jason

+0

@jason Я просто даю иерархию 'RecycleView', и все остальные вещи входят в' FrameLayout', или вы можете использовать его 'RelativeLayout'. Я просто даю вам «Pattern», как «Майк М.». – Ironman

+0

Итак, следует ли добавить RecycleView внутри 'FrameLayout'? – jason

2

Поместите панель инструментов виджет в AppBar, как это -

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/appbar" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

А потом - андроида : ниже = "@ + id/appbar в вашем recyclerview"

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

Надеюсь, это поможет!

+0

Должен ли я использовать «RelativeLayout» в корне? Благодарю. – jason

+0

Да ... Нет проблем –

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