在BottomNavigationView中启动片段 [英] Start fragment in BottomNavigationView

查看:79
本文介绍了在BottomNavigationView中启动片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用底部导航视图的简单应用程序。
我有3个文本片段,我想在Botton Navigation中选择一个项目时启动它们,但我不知道在MainActivity.java中写什么;
所有片段都有.xml布局和.java。
我搜索了一些代码,我写了它们,我搜索了视频,但我没有成功。

I'm working with a simple app with Bottom Navigation View. I have 3 fragment with text, and i want to start their when i select a item in Botton Navigation, but i don't know what to write in the MainActivity.java; All the fragments have a .xml layout and .java. I searched in some codes, i wrote them, i search videos, but i have no sucess.

我正在学习片段和UI动态,所以我在Android Studio中创建了一个带有底部导航活动的新项目。
因此,在我的activity_main中,我在底层导航中有3个itens,在Bottom Navigation上面有一个framelayout,占用了所有父级。这个想法是:当我在底部导航中选择一个项目时,它将在framelayout中显示另一个布局。所以我在布局文件夹中创建了3个xml布局(也使用了java类),在framelayout中创建了一个片段。现在,当我选择一个项目时,我正试图在我的framelayout(有一个片段)中显示这些布局。但我不知道该怎么做。

I'm learning about Fragments and UI Dynamic, so i created a new project in Android Studio with Bottom Navigation Activity. So, in my activity_main i have 3 itens in Bottom Navigation, and one framelayout above Bottom Navigation, taking all the parent. The idea is: when i select a item in Bottom Navigation, it will show another layout in the framelayout. So i created 3 xml layouts (with java class too) in layout folder and one fragment in framelayout. Now, i'm trying to show these layouts in my framelayout(that have a fragment) when i select a item. But i don't know how to do that.

主要活动`

private TextView mTextMessage;

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_home:
                selectedFragment = HomeFragment.newInstance();
                break;
            case R.id.navigation_dashboard:
                selectedFragment = DashboardFragment.newInstance();
                break;
            case R.id.navigation_notifications:
                selectedFragment = NotificationsFragment.newInstance();
                break;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }

};

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


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

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.content, HomeFragment.newInstance());
    transaction.commit();
}

activity_main xml

activity_main xml

<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="bandeira.thalisson.barradenavegacaoembaixo.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"/>

navigation xml

navigation xml

<item
    android:id="@+id/Fragment_one"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home"/>

<item
    android:id="@+id/Fragment_two"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard"/>

<item
    android:id="@+id/Fragment_three"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_notifications"/>

Fragment.java示例

Fragment.java example

public class HomeFragment extends Fragment {

public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();
    return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.navigation_home, container, false);
    return inflater.inflate(R.layout.navigation_home, container, false);
}


推荐答案

首先,在您的activity_main中。 xml我们不需要3个不同的片段,因为我们可以在1个FrameLayout中替换或添加我们任何选定的片段。
其次,当用户从Bottom NavigationView中选择任何一个时,只需获取相关Fragment类的实例,并将其替换为您的activity_main的FrameLayout。

Firstly, in your activity_main.xml we don't require 3 different fragments as we can replace or add our any selected Fragment in 1 FrameLayout only. Secondly, when ever the user select any one from the Bottom NavigationView just get an instance of the related Fragment class and replace it with your activity_main's FrameLayout .

Fragment selectedFragment = null;
         switch (item.getItemId()) {
               case R.id.navigation_home:
                   selectedFragment = FunFragment.newInstance();
                   break;

获取所选片段的实例后,将其替换为您在activity_main的FrameLayout上显示在屏幕上。

after getting the instance of the selected Fragment replace it with you activity_main's FrameLayout as to show on the screen.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.content, selectedFragment);
            transaction.commit();

EDIT TAG
第1步。您的activity_main.xml应该如下所示

EDIT TAG Step 1. your activity_main.xml should look like this

<?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="com.exampl.MainActivity">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <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>

第2步。您的fragment_home.xml布局应该是这样的

Step 2. Your fragment_home.xml layout should be like this

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="android"/>
    </LinearLayout>

为您的三种不同选项制作3种不同的片段布局

Make 3 different fragment layout for your three different options

第3步。你的MainActivity.java类将是这样的

Step 3. your MainActivity.java class will be like this

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        selectedFragment = FunFragment.newInstance();
                       break;
                    case R.id.navigation_dashboard:
                        selectedFragment = TheoryFragment.newInstance();
                       break;
                    case R.id.navigation_notifications:
                        selectedFragment = AndroidFragment.newInstance();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content, selectedFragment);
                transaction.commit();
                return true;
            }
        };

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

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, FunFragment.newInstance());
        transaction.commit();
    }

在创建时替换启动应用程序后要显示的片段导航监听器将负责您将选择的选项

In on create replace with the fragment which you want to show after launching the application and navigation listener will take care of what ever option you will select

步骤4。您将拥有3个不同的Fragment类,看起来像这样

Step 4. Your will have 3 different Fragment class and look like this

public class TheoryFragment extends Fragment {

     public static TheoryFragment newInstance() {
            TheoryFragment fragment = new TheoryFragment();
            return fragment;
        }
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_theory, container, false);
            unbinder = ButterKnife.bind(this, rootView);
            return rootView;
        }
    }

希望它会对你有所帮助,如果有的话,请告诉我问题。

Hope it will help you, let me know if any problem.

这篇关于在BottomNavigationView中启动片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆