gpt4 book ai didi

java - NavigationDrawerActivity 和 BottomNavigationActivity

转载 作者:行者123 更新时间:2023-11-30 04:58:21 25 4
gpt4 key购买 nike

我正在尝试将 NavigationDrawerActivity 与 BottomNavigationActivity 一起使用。我首先实现了 NavigationDrawerActivity(Separate) 并将 BottomNavigationActivity(Separate) 保存在 HomeActivity 中,但是在运行应用程序时黑屏出现,没有错误!

我的家庭 Activity

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private DrawerLayout drawerLayout;


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

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.home);
}
}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SearchFragment()).commit();
break;
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;

}
drawerLayout.closeDrawer(GravityCompat.END);
return true;
}
}

包含 BottomNavigationView 的 My HomeFragment 代码

public class HomeFragment extends Fragment {

View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
BottomNavigationView bottomNav = view.findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
return view;
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;

switch (item.getItemId()) {
case R.id.tours:
selectedFragment = new ToursFragment();
break;
case R.id.hotel:
selectedFragment = new HotelFragment();
break;
case R.id.maps:
selectedFragment = new SearchFragment();
break;
}
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, selectedFragment);
transaction.commit();

return true;
}
};

}

最佳答案

我想在此代码中使用带有 BottomNavigationActivity 的 Navigation Drawer Activity,并希望仅在一个 fragment 中显示 BottomNavigationActivity。我解决了这个问题,但首先我们忘记了 HomeFragment,我删除了除了 View 之外的几乎所有内容。我把 BottomNavigationActivity 和 Navigation Drawer Activity 放在一个 Activity 中这是我来自 HomeActivity 的 xml(即它是 MainActivity)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
>


<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>
<FrameLayout
android:id="@+id/fragment_container_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_alignParentBottom="true"
app:itemHorizontalTranslationEnabled="false"
app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="@layout/nav_helder"
app:menu="@menu/drawer_menu" />


</androidx.drawerlayout.widget.DrawerLayout>
</RelativeLayout>



And submitted to HomeActivity to BottomNavigationActivity
setVisibility (View.GONE);





public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{

private DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
BottomNavigationView bottomNavigationView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewCompat.setLayoutDirection(toolbar, ViewCompat.LAYOUT_DIRECTION_RTL);
toolbar.setTitle("Paris Tours ");

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ToursFragment()).commit();
navigationView.setCheckedItem(R.id.nav_view);
}

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_bottom,
new ToursFragment()).commit();
}

}


private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;

switch (item.getItemId()) {
case R.id.tours:
selectedFragment = new ToursFragment();
break;
case R.id.hotel:
selectedFragment = new HotelFragment();
break;
case R.id.maps:
selectedFragment = new MapsFragment();
break;
case R.id.taxi:
selectedFragment = new TaxiFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_bottom,
selectedFragment).commit();

return true;
}
};

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
if(drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SearchFragment()).commit();
bottomNavigationView.setVisibility(View.GONE);
break;
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FavouriteFragment()).commit();
bottomNavigationView.setVisibility(View.VISIBLE);
break;
case R.id.tours:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SettingsFragment()).commit();
bottomNavigationView.setVisibility(View.GONE);
break;
}
drawerLayout.closeDrawer(GravityCompat.END);
return true;
}

关于java - NavigationDrawerActivity 和 BottomNavigationActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58736824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com