从导航器调用时片段无法显示

时间:2017-07-05 16:27:38

标签: android android-layout android-fragments

来自Fragment的来电时,NavigationDrawer无法显示。当我点击导航抽屉Fragment中的项目无法加载或显示代码完美无缺时,代码中没有错误。还有代码在Fragment

上执行
 <android.support.design.widget.CoordinatorLayout
    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.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            style="@style/MyAppTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#ffffff"
            app:tabIndicatorColor="@color/black" />

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




    <android.support.v4.view.ViewPager
        android:layout_marginBottom="?attr/actionBarSize"
        android:layout_marginTop="105dp"
        android:layout_below="@id/appBar"
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true">

    </android.support.v4.view.ViewPager>


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:elevation="16dp"
        android:id="@+id/design_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        app:behavior_hideable="true"
        app:behavior_peekHeight="50dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <fragment android:name="com.techweblearn.musicplayer.BottomPlayer"

            android:id="@+id/bottom_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


</RelativeLayout>



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

activity.java

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {


    int id = item.getItemId();
    if (id == R.id.library) {
        Toast.makeText(MainActivity.this,"Library",Toast.LENGTH_SHORT).show();




    } else if (id == R.id.equalizer) {

        FragmentManager fragmentManager=getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,new EqualizerFragment()).commit();
        Toast.makeText(MainActivity.this,"Equalizer",Toast.LENGTH_SHORT).show();
    } else if (id == R.id.sleep_timer) {
        Toast.makeText(MainActivity.this,"Sleep Timer",Toast.LENGTH_SHORT).show();

    } else if (id == R.id.setting) {
        Toast.makeText(MainActivity.this,"Setting",Toast.LENGTH_SHORT).show();

    } else if (id == R.id.nav_share) {
        Log.d("Navigation","1");

    } else if (id == R.id.nav_send) {
        Log.d("Navigation","1");

    }


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

1 个答案:

答案 0 :(得分:0)

在您的代码中添加setTranslationZ,以将content_frame保持在viewpager的顶部,如下所示:

else if (id == R.id.equalizer) {

View content = findViewbyId(R.id.content_frame)
ViewCompat.setTranslationZ(content , 50);
        FragmentManager fragmentManager=getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,new EqualizerFragment()).commit();
        Toast.makeText(MainActivity.this,"Equalizer",Toast.LENGTH_SHORT).show();