我正在使用底部导航栏的项目中工作,但是当我运行该应用程序时,只有点击底部导航栏菜单项中的图标,片段活动才会自动出现。
屏幕截图:
当我点击任何片段活动时,空白活动首先出现,然后出现。
这是我的代码:
BottomNavigation.java
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class BottomNavigation extends AppCompatActivity {
private BottomNavigationView mMianNav;
private FrameLayout mMainFrame;
private AllResturant allResturant;
private UserProfile userprofile;
private ActivityUser activityUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);
allResturant = new AllResturant();
userprofile = new UserProfile();
activityUser = new ActivityUser();
//Listener for handling selection events on bottom navigation items
mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.toprofile:
setFragment(userprofile);
return true;
case R.id.tohome:
setFragment(allResturant);
return true;
case R.id.toactivity:
setFragment(activityUser);
return true;
case R.id.tologout:
logout();
return true;
default:
return false;
}
}
private void logout() {
Intent intent = new Intent(BottomNavigation.this, Registration.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
}
BottomNavigation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".BottomNavigation">
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
app:itemIconTint="@color/nav_items_color"
app:itemTextColor="@color/nav_items_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/nav_items" />
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_above="@+id/main_nav"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:layout_constraintBottom_toTopOf="@+id/main_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
它将如何自动显示?
答案 0 :(得分:0)
设置UI后,请自行设置第一个片段。在onCreate()
方法的最后一行添加下面的行。
setFragment(userprofile);
因为您的活动开始时未调用onNavigationItemSelected
,所以在用户切换屏幕时会调用它。
答案 1 :(得分:0)
按以下说明修改创建的内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);
allResturant = new AllResturant();
userprofile = new UserProfile();
activityUser = new ActivityUser();
setFragment(userprofile);
//Listener for handling selection events on bottom navigation items
mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.toprofile:
setFragment(userprofile);
return true;
case R.id.tohome:
setFragment(allResturant);
return true;
case R.id.toactivity:
setFragment(activityUser);
return true;
case R.id.tologout:
logout();
return true;
default:
return false;
}
}
private void logout() {
Intent intent = new Intent(BottomNavigation.this, Registration.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
}
答案 2 :(得分:0)
您的代码几乎是完美的。您唯一要做的就是在创建Fragment
时给Activity
充气。
创建Activity
后,您需要使用此行向您想要的Fragment
充气一次:
setFragment(userprofile);
实例化片段后,可以将其放入onCreate
方法中。之后,您的OnNavigationItemSelectedListener
将负责填充片段。