0

我用默认的底部导航视图制作了一个简单的应用程序。现在代码是正确的并且应用程序正在构建但是当我启动它时,无论我点击什么菜单项,我都有一个空白片段。

MainActivity:包com.ali.mydesign;

 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;
 import android.widget.TextView;
 import android.support.v4.app.Fragment;

public class MainActivity extends AppCompatActivity {

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


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

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

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

    Fragment navFragment = null;
        switch (item.getItemId()) {
            case R.id.nav_1:
                navFragment = new HomeFragment();
                return true;
            case R.id.nav_2:
                navFragment = new SecondFragment();
                return true;
            case R.id.nav_3:
                navFragment = new ThirdFragment();
                return true;
        }
        return false;
     }
   };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

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

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation"/>

</LinearLayout>

首页片段:

public class HomeFragment extends Fragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_1, container, false);
    return v;
}
}

片段_1.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"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginTop="?attr/actionBarSize">

 <ScrollView
    android:id="@+id/svLog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="6dp"
            app:cardBackgroundColor="#FFF"
            app:cardCornerRadius="0dp"
            app:cardElevation="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFF"
                    android:orientation="vertical"
                    android:padding="32dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Text"
                        android:textColor="#FFF"
                        android:textStyle="bold"
                        android:textSize="18dp"
                        android:layout_marginBottom="32dp"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tips"
                        android:textColor="#FFF"
                        android:textSize="16dp"
                        />

                </LinearLayout>

            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>

 </ScrollView>

 </RelativeLayout>

如何正确实现片段以显示内容?

已解决:感谢 Gabrielle 的回答,我通过用 break 替换 return true 来解决它,然后添加 getSupport.. 像这样:

片段 navFragment = null;

        switch (item.getItemId()) {
            case R.id.nav_1:
                navFragment = new HomeFragment();
                break;
            case R.id.nav_2:
                navFragment = new SecondFragment();
                break;
            case R.id.nav_3:
                navFragment = new ThirdFragment();
                break;
        }

        getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit();
        return true;
     }

   };

要设置我使用的默认片段

导航.setSelectedItemId(R.id.nav_1);

在 setContentView() 之后

4

2 回答 2

1

在您的方法中onNavigationItemSelected(),您忘记了以下内容:

if (navFragment != null){
  getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit();
}
于 2019-08-25T07:36:27.117 回答
0

您可以像这样添加片段

public void loadFragment (Fragment fragment){
FragmentManager fm = getFragmentManager(); 
FragmentTransaction ft =fm.beginTransaction();
 ft.replace(R.id.fragmentContainer, fragment); ft.commit();
}

点击菜单项

loadFragment(new HomeFragment());
于 2019-08-25T07:33:32.053 回答