繁体   English   中英

当我同时添加“抽屉导航”和“底部导航”但单击“导航”时不显示片段吗?

[英]When i add Drawer Navigation and Bottom Navigation Simultaneously but Fragments not Show when Click on Navigation?

当我单击底部导航图标时,片段不会仅显示可见的抽屉导航和底部导航。 我被困在这个问题几天。 请指导我为解决此问题而进行的更改

抽屉活动xml

<androidx.coordinatorlayout.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"
tools:context=".activities.DrawerActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.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">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_gravity="center"
            android:textColor="@color/colorTitle"
            android:textSize="20sp"
            android:textStyle="bold"
            android:id="@+id/toolbar_title" />

    </androidx.appcompat.widget.Toolbar>

//将文件home活​​动包含在抽屉活动中

活动主xml

<androidx.coordinatorlayout.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"
tools:context=".activities.HomeActivity">

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

</LinearLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemIconTint="@color/bottom_nav_item"
    app:itemTextColor="@color/bottom_nav_item"
    android:background="@color/colorPrimary"
    app:menu="@menu/nav_menu"
    tools:ignore="MissingConstraints" />

家庭活动Java

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

BottomNavigationView bottomNavigationView;

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


    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(this);

    displayFragment(new HomeFragment());

}


private void displayFragment(Fragment fragment ) {
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_area, fragment)
            .commit();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment;
    switch (item.getItemId()) {
        case R.id.nav_home:
            fragment = new HomeFragment();
            break;
        case R.id.nav_fav:
            fragment = new FavouritesFragment();
            break;
        case R.id.nav_set:
            fragment = new SettingsFragment();
            break;
        default:
            fragment = new HomeFragment();
            break;
    }
    displayFragment(fragment);
    return true;
 }
}

抽屉活动Java

public class DrawerActivity extends AppCompatActivity {

Toolbar mToolbar;

private RecyclerView recyclerView;
private SwipeRefreshLayout recyclerLayout;
Drawer resultDrawer;
AccountHeader headerResult;
ArrayList<Object> filesList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTheme( Constant.theme);
    setContentView(R.layout.drawer_activity);


    //Toolbar
    mToolbar = (Toolbar) findViewById( R.id.toolbar);
    mToolbar.setBackgroundColor( Constant.color);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

}

主要活动Java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );


    Intent intent = new Intent( MainActivity.this, HomeActivity.class );
    startActivity( intent );
    finish();

   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM