【问题标题】:Implementing fragments with the default BottomNavigationView使用默认的 BottomNavigationView 实现片段
【发布时间】:2019-08-25 07:29:38
【问题描述】:

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

主要活动: 包 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>

HomeFragment:

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;
}
}

fragment_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;
     }

   };

设置我使用的默认片段

navigation.setSelectedItemId(R.id.nav_1);

在 setContentView() 之后

【问题讨论】:

    标签: android android-fragments bottomnavigationview android-bottomnav


    【解决方案1】:

    在您的方法onNavigationItemSelected() 中您忘记了类似的内容:

    if (navFragment != null){
      getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit();
    }
    

    【讨论】:

    • 我得到了 .FragmentManager 的未知方法“替换”。
    • @AliEid getSupportFragmentManager().beginTransaction().replace(..)
    • 更新答案后,代码正确但同样​​的问题:没有片段。
    • @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Fragment navFragment = null; if (navFragment != null){ getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit(); } switch (item.getItemId()) { case R.id.nav_1: navFragment = new HomeFragment();返回真;
    • 我得到了它的工作取决于你的有用答案。我只是使用了break而不是return,然后在没有if语句的情况下添加了getSupport方法。我会接受你的回答,因为它完全引导了我。
    【解决方案2】:

    你可以像这样添加片段

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

    点击菜单项

    loadFragment(new HomeFragment());
    

    【讨论】:

    • 启动后的2个问题:1.底部导航视图现在位于屏幕顶部2.应用显示一个片段,其中没有任何内容
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多