在BottomNavigationView中启动片段

时间:2017-06-02 23:46:43

标签: java android android-fragments

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

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

主要活动

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

<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"/>

导航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示例

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

4 个答案:

答案 0 :(得分:3)

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

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

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

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

编辑标签 第1步。您的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="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布局应该是这样的

 <?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种不同的片段布局

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

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

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

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

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

希望它能帮助你,让我知道是否有任何问题。

答案 1 :(得分:3)

获取HomeFragment,DashboardFragment和NotificationFragment等三个片段以及这三个片段的三个布局。

<强> HomeFragment

public class HomeFragment extends Fragment {
    public HomeFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

<强> fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home Fragment"
        android:textStyle="bold"
        android:layout_gravity="center"/>
</FrameLayout>

** DashboardFragment **

public class DashboardFragment extends Fragment {
    public DashboardFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dashboard, container, false);
    }
}

<强> fragment_dashboard.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dashboard Fragment"
        android:textStyle="bold"
        android:layout_gravity="center"/>
</FrameLayout>

** NotificationFragment **

public class NotificationFragment extends Fragment {
    public NotificationFragment () {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_notification, container, false);
    }
}

<强> fragment_notification.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification Fragment"
        android:textStyle="bold"
        android:layout_gravity="center"/>
</FrameLayout>

这是你的活动。

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id){
                    case R.id.Fragment_one:
                        fragment = new HomeFragment();
                        break;
                    case R.id.Fragment_two:
                        fragment = new DashboardFragment();
                        break;
                    case R.id.Fragment_three:
                        fragment = new NotificationFragment();
                        break;
                }
                final FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container, fragment).commit();
                return true;
            }
        });

答案 2 :(得分:1)

我想补充一点,如果您想使用BottomNavigationView.OnNavigationItemSelectedListener()选择开始片段,可以使用setSelectedItemId(),例如:

替换&#39; R.id.navigation_home &#39;用你自己的开始片段。

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

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

答案 3 :(得分:1)

单独创建此方法

protected boolean openFragment(Fragment fragment){
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content, fragment)
            .commit();
    return true;
}

要在开关中进行编辑

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home: return openFragment(OneFragment.newInstance("",""));
            case R.id.navigation_beehive: return openFragment(twoFragment.newInstance("",""));
            case R.id.navigation_notifications: return openFragment(otherFragment.newInstance("",""));
        }
        return false;
    }

要进行最终确定,您可以使用中的片段进行初始化

 protected void onCreate(Bundle savedInstanceState) {
    ....
    openFragment(OneFragment.newInstance("",""));
}