未在主页图标上显示fragment.xml内的DrawerLayout单击

时间:2018-10-21 00:50:13

标签: android drawerlayout

背景

我正在尝试在我的片段中实现一个抽屉导航。我宁愿将其放置在那里,而不是将其放置在主要活动中,因为我正尝试处理每个片段的工具栏,并且将Drawerlayout耦合起来将使设置更容易。

问题

当我单击“汉堡”主页按钮时,没有显示抽屉布局。如何解决此问题,同时将抽屉布局保留在fragment.xml文件中而不是将其移到活动中?

我尝试过的事情

fragment.xml

<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_article_topics"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/toolbar" />

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/articles_swipe_to_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/articles"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

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

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

    <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:menu="@menu/navigation_article_topic" />

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

activity.xml

<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"
    android:orientation="vertical"
    tools:context=".ui.main.MainActivity">

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

Fragment.java

@BindView(R.id.drawer_article_topics)
DrawerLayout articleTopicDrawerLayout;

// Other code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_articles_refresh:
            articleListingViewModel.startRefresh();
            refresh();
            return true;
        case android.R.id.home:
            articleTopicDrawerLayout.openDrawer(GravityCompat.START);
            return true;

    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
    setSupportActionBar(toolbar);
    setDrawerIcon();
    setupRecyclerView();
    setupSwipeRefreshLayout();
}

private void setDrawerIcon() {
    if (isAdded()) {
        ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
    }
}

1 个答案:

答案 0 :(得分:0)

问题似乎出在DrawerLayout.openDrawer()和Android Material Components库上。因为我继承了AppTheme中的Theme.MaterialComponents.Light.NoActionBar,所以导航抽屉未打开。在他们添加修复程序之前,解决方法是将其添加到您的应用主题中:

<item name="navigationViewStyle">@style/Widget.Design.NavigationView</item>

更多信息在这里: https://github.com/material-components/material-components-android/issues/133