0
Я реализовал this как фрагмент по умолчанию, когда выдвигается навигационный ящик, и он работает, но ActionBarDrawerToggle не появится.ActionBarDrawerToggle не отображается во фрагменте
Основная деятельность:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private ActionBarDrawerToggle toggle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ServiceFragment serviceFragment = new ServiceFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.content_fragment, serviceFragment)
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//Initializing NavigationView
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//This method triggers when an item is clicked in navigation drawer menu
navigationView.setNavigationItemSelectedListener(this);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/content_fragment"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
По умолчанию фрагмент, ServiceFragment раздувает fragment_service.xml Отчет на
<FrameLayout 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:id="@+id/service_fragment">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--android:scaleType="centerCrop"-->
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<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.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/recyclerview"/>
</android.support.design.widget.CoordinatorLayout>
Это потому, что ваш 'Toolbar' пока не существует, когда вы создаете' ActionBarDrawerToggle'. Фактически, 'toolbar' будет пустым в' onCreate() ', но' ActionBarDrawerToggle' не будет жаловаться на это, поэтому исключение Exception не будет выбрано. Вы действительно должны хранить «DrawerLayout», «Toolbar» и «ActionBarDrawerToggle» все в одном месте - либо «Активность», либо «Фрагмент», но если вы действительно хотите сохранить текущую настройку, вызывается 'manager.executePendingTransactions() 'сразу после того, как вызов' commit() 'скорее всего, устранит вашу проблему. –
Добавление 'DrawerLayout',' Toolbar' и 'ActionBarDrawerToggle' в Fragment делает [work] (http://imgur.com/a/MEhe1), но панель действий не появляется, когда я прокручиваю страницу вниз - рушится панель инструментов - как показано на рисунке выше. Нет, вызывая 'manager.executePendingTransactions()'. Есть идеи? – xaldin
О, извините, я забыл упомянуть, что вам придется переместить инициализацию 'toolbar' после этого вызова' executePendingTransactions() '. В основном, выполните «FragmentTransaction» сразу после 'setContentView()', а затем сделайте все остальное. –