片段未在发布时显示

时间:2018-02-03 19:34:42

标签: java android android-fragments

我的应用程序中有一个带有两个项目的底部导航栏,当我选择其中一个时,会显示一个片段,当我选择另一个片段时,会显示不同的片段。该应用程序工作很大,但唯一的问题是,当我启动应用程序时,没有显示任何片段。那么我需要添加什么才能在发布时显示另一个片段?提前谢谢。

MainActivity.java:

package com.fields.curiumx.fields;



import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;


import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {








private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {


        switch (item.getItemId()) {
            case R.id.navigation_explore:
                ExploreFragment exploreFragment = new ExploreFragment();
                FragmentManager manager = getFragmentManager();
                manager.beginTransaction().replace(R.id.layoutContainer, exploreFragment).commit();
                return true;


            case R.id.navigation_profile:
                ProfileFragment profileFragment = new ProfileFragment();
                FragmentManager manager1 = getFragmentManager();
                manager1.beginTransaction().replace(R.id.layoutContainer, profileFragment).commit();
                return true;



        }return true;



    }
};

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






        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

activity_main.xml中:

<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fields.curiumx.fields.MainActivity">


<FrameLayout


    android:id="@+id/layoutContainer"
    android:layout_width="match_parent"
    android:layout_height="495dp"
    app:layout_constraintBottom_toTopOf="@id/navigation"
    tools:layout_editor_absoluteX="8dp">


</FrameLayout>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

  </android.support.constraint.ConstraintLayout>

我在主活动中的onNavigationItemSelected方法中可以看到替换FrameLayout的两个片段。我希望在启动应用程序时显示ExploreFragment。目前,该应用在发布时显示空的FrameLayout。

1 个答案:

答案 0 :(得分:-1)

用于在启动应用程序时显示片段,就像您要在启动应用程序时在onCreate上创建片段代码时启动片段ExploreFragment。因此,当应用程序启动时,它将显示ExploreFragment。

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


ExploreFragment exploreFragment = new ExploreFragment();
                FragmentManager manager = getFragmentManager();
                manager.beginTransaction().replace(R.id.layoutContainer, exploreFragment).commit();



        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }