【问题标题】:Fragment is not getting replace片段没有被替换
【发布时间】:2021-06-25 04:43:15
【问题描述】:

嗨,我正在制作一个应用程序,我正在使用底部导航来浏览片段,但我的问题是当我点击应用程序加载时,片段在框架布局中不可见,即使我点击底部导航,它也只是下面显示的空白页是我在谷歌上搜索此问题的主要活动的代码,但看起来我是唯一遇到此问题的人

我的主要活动是 dashboard,我想在其中实现底部导航,一切看起来都很完美,但片段没有显示

dashboard.xml

<?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"
    android:background="@color/white"
    android:gravity="center"
    tools:context=".DashboardActivity">


<!--    Framelayout: show fragment-->

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/navigation"
        android:layout_weight="1">

    </FrameLayout>

<!--    Bottom nav : show menu-->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/menu_nav">

    </com.google.android.material.bottomnavigation.BottomNavigationView>


</RelativeLayout>

仪表板.java

package com.example.trella;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class DashboardActivity extends AppCompatActivity {


    //firebase  auth
    FirebaseAuth firebaseAuth;

    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        
        //Action bar its title
        actionBar = getSupportActionBar();
        actionBar.setTitle("Profile");

        //init
        firebaseAuth = FirebaseAuth.getInstance();

        BottomNavigationView bnv= findViewById(R.id.bottom_navigation);
        bnv.setOnNavigationItemSelectedListener(selectedListener);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HomeFragment()).commit();

    }

    private  BottomNavigationView.OnNavigationItemSelectedListener selectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    Fragment selectedFragment = null;
                    switch (item.getItemId()){
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();
                            break;
                        case R.id.nav_profile:
                            selectedFragment = new ProfileFragment();
                            break;

                        case R.id.nav_users:
                            selectedFragment = new UsersFragment();
                            break;

                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
                    return true;
                }
            };

    private void  checkUserStatus(){
        //get current user
        FirebaseUser user = firebaseAuth.getCurrentUser();

        if(user !=null){
            //user is signed in stays here


        }
        else {
            //user is not signed in , login activity
            startActivity(new Intent(DashboardActivity.this,MainActivity.class));
            finish();
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onStart() {
        //check user status
        checkUserStatus();

        super.onStart();
    }

    //inflate options menu


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //inflating menu
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //handle menu click event

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //get item id
        int id =   item.getItemId();
        if(id == R.id.action_logout){
            firebaseAuth.signOut();
            checkUserStatus();
        }

        return super.onOptionsItemSelected(item);
    }
}

虽然设定的标题有效 actionBar.setTitle("Home");

【问题讨论】:

  • 在您使用 R.id.fragment_container 作为容器时,您的布局 id 是 R.id.content
  • 您应该为您的底部导航视图使用导航组件。参考本教程 - youtu.be/Chso6xrJ6aU

标签: java android fragment


【解决方案1】:

在您的代码中更新以下行,id 应该是 content 而不是 fragment_container。

 getSupportFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多