我正在尝试实现导航抽屉,我有一个问题,页面加载和汉堡图标显示。然而,当我点击图标时,它会像往常一样变成箭头,但我的抽屉却没有显示出来。当我试图强制默认显示其中一个片段时,我得到一个错误,它会导致类片段错误。
下面是我的页面布局,其中包含抽屉
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".app.index">
<include layout="@layout/toolbar">
</include>
<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 -->
<fragment
android:id="@+id/nav_drwr_fragment"
class="com.mobile.map.mapversion20.helpers.NavDrawerManager"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/nav_drawer_layout"
tools:layout="@layout/nav_drawer_layout"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
下面是我的抽屉布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="@color/colorPrimary">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_profile"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
</LinearLayout>
这里是我的片段管理员
public class NavDrawerManager extends Fragment
{
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private static boolean isLaunch = true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.nav_drawer_layout, container, false);
setUpRecyclerView(view);
return view;
}
private void setUpRecyclerView(View view) {
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.drawerList);
NavDrawerAdapter adapter = new NavDrawerAdapter(getActivity(), drawerItem.getData());
NavDrawerManager manager = new NavDrawerManager();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
if(isLaunch)
{
isLaunch = false;
manager.selectItem("DashBoard");
}
}
public void setUpDrawer(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
// Do something of Slide of Drawer
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public void selectItem(String title)
{
FragmentManager fragmentManager = getChildFragmentManager();
switch (title)
{
case "Dashboard":
Fragment dashFragment = new dashboard();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,dashFragment)
.commit();
break;
case "Profile":
Fragment profileFragment = new profile();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,profileFragment)
.commit();
break;
case "Notifications":
Fragment notificationsFragment = new notifications();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,notificationsFragment)
.commit();
break;
case "Contact":
Fragment contactFragment = new contact();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,contactFragment)
.commit();
break;
default:
Fragment defaultFrag = new dashboard();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,defaultFrag)
.commit();
break;
}
}
}
答案 0 :(得分:0)
问题是您需要将DrawerLayout作为布局文件的父级,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<fragment
android:id="@+id/nav_drwr_fragment"
class="com.mobile.map.mapversion20.helpers.NavDrawerManager"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/nav_drawer_layout"
tools:layout="@layout/nav_drawer_layout"/>
</android.support.v4.widget.DrawerLayout>