已创建四个片段。所有4个片段都显示在导航栏上,但是单击这些片段不会发生任何事情。单击后,应该带您到片段页面。
我已经尝试删除导航栏的片段和代码。这仍然不能解决问题。
我的NavigationMenu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/nav_home" />
<item
android:id="@+id/nav_symptoms"
android:icon="@drawable/ic_contact_black_24dp"
android:title="@string/nav_symptoms" />
<item
android:id="@+id/nav_treatment"
android:icon="@drawable/ic_contact_black_24dp"
android:title="@string/nav_treatment" />
<item
android:id="@+id/nav_contact"
android:icon="@drawable/ic_contact_black_24dp"
android:title="@string/nav_contact" />
</menu>
我的Layout
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
app:menu="@menu/nav_items">
</android.support.design.widget.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_nav"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</FrameLayout>
我的Activity
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth firebaseAuth;
private TextView textViewUserEmail;
private Button buttonLogout;
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private HomeFragment homeFragment;
private SymptomsFragment symptomsFragment;
private TreatmentFragment treatmentFragment;
private Contactragment contactragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this, LoginActivity.class));
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
symptomsFragment = new SymptomsFragment();
treatmentFragment = new TreatmentFragment();
contactragment = new Contactragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home :
mMainNav.setItemBackgroundResource(R.color.colorPrimary);
setFragment(homeFragment);
return true;
case R.id.nav_symptoms:
mMainNav.setItemBackgroundResource(R.color.colorAccent);
setFragment(symptomsFragment);
return true;
case R.id.nav_treatment:
mMainNav.setItemBackgroundResource(R.color.colorPrimary);
setFragment(treatmentFragment);
return true;
case R.id.nav_contact:
mMainNav.setItemBackgroundResource(R.color.colorAccent);
setFragment(contactragment);
return true;
default:
return false;
}
}
});
}
FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
textViewUserEmail.setText("Welcome"+user.getEmail());
buttonLogout = (Button) findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(this);
//listener has been added
}
private void setFragment (Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
@Override
public void onClick(View view) {
if(view == buttonLogout){
firebaseAuth.signOut();
finish();
startActivity(new Intent(this, LoginActivity.class));
}
}
}
在此页面上单击主页后,将带您进入主页片段。
答案 0 :(得分:-1)
我认为您可能正在寻找。 这是一个快速入门的片段。 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="@+id/viewlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/colorAccent"
android:layout_gravity="bottom"
tools:elevation="2dp"
app:itemTextColor="#fff"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_menu" />
</RelativeLayout>
在res / menu / bottom_menu.xml中创建菜单资源文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_home"
android:enabled="true"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/home"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_msg"
android:enabled="true"
android:icon="@drawable/ic_message_black_24dp"
android:title="@string/msg"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_video"
android:enabled="true"
android:icon="@drawable/ic_ondemand_video_black_24dp"
android:title="@string/video"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_info"
android:enabled="true"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/info"
app:showAsAction="ifRoom" />
</menu>
为每个视图创建片段:
创建四个fragments,分别命名为Home,Message,Video和Notification。
实现BottomNavigation视图的OnItemSelectedListener: MainActivity.java:
package net.technxt;
import android.support.annotation.NonNull;
import android.support.design.bottomnavigation.LabelVisibilityMode;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
FrameLayout viewlayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewlayout = (FrameLayout)findViewById(R.id.viewlayout);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
final FragmentManager fragmentManager = getSupportFragmentManager();
// Declare fragments here
final Home home = new Home();
final Message msg = new Message();
final Video video = new Video();
final Notification noti = new Notification();
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.action_home:
fragment = home;
setTitle(item.getTitle());
break;
case R.id.action_msg:
fragment = msg;
setTitle(item.getTitle());
break;
case R.id.action_video:
fragment = video;
setTitle(item.getTitle());
break;
case R.id.action_info:
default:
fragment = noti;
setTitle(item.getTitle());
break;
}
fragmentManager.beginTransaction().replace(R.id.viewlayout, fragment).commit();
return true;
}
});
// Set default selection
bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
bottomNavigationView.setSelectedItemId(R.id.action_home);
}
}