如何使用底部导航活动更改片段?

我创建了一个带有“底部导航活动”的新项目:

enter image description here

这是生成的代码:

package com.aaron.waller.mrpolitik;

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;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

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

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
            }
            return true;
        }

    };

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

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

}

如何更改为带有底部栏的新片段?例如,我有3个片段:Fragment1 Fragment2和Fracment3,我想用底部栏中的3个按钮更改为它们。另外,我希望我可以通过左右滑动手指来切换碎片,我该怎么做?


答案 1

我这样做的方法是,我首先添加三个与此类似的方法(每个方法用于单个片段。将布局名称和片段对象替换为要切换到的相应片段:

public void switchToFragment1() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.your_fragment_layout_name, new Fragment1()).commit();
}

因此,您的 switch 语句最终将如下所示:

        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                switchToFragment1();
                break;

            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);                    
                switchToFragment2();
                break;

            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);                     
                switchToFragment3();
                break;
        }

至于通过向侧面滑动来切换片段,我相信你需要一个ViewPager


答案 2

这很“简单”。

  1. 创建您的片段。假设我们想要3个片段;,并且成为我们在BottomNavigationView上想要的第一个Fragment。FragmentAFragmentBFragmentCFragmentA
  2. 在“活动”中,转到该方法并将内容更改为:onNavigationItemSelected

     private BottomNavigationView.OnNavigationItemSelectedListener  
       mOnNavigationItemSelectedListener
           = new BottomNavigationView.OnNavigationItemSelectedListener(){
    
       @Override
       public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    
        switch (item.getItemId()) {
            case R.id.frag_a:
                currentFragment = new FragmentA();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content, currentFragment);
                ft.commit();
                return true;
            case R.id.frag_b:
                currentFragment = new FragmentB();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content, currentFragment);
                ft.commit();
                return true;
            case R.id.frag_c:
                currentFragment = new FragmentC();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content, currentFragment);
                ft.commit();
                return true;
        }
    
        return false;
     }
    
    };
    
  3. 在您的方法中,执行以下操作:onCreate()

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_profile);
        ft = getSupportFragmentManager().beginTransaction();
        currentFragment = new FragmentA();
        ft.replace(R.id.content, currentFragment);
        ft.commit();
    
        BottomNavigationView navigation = (BottomNavigationView)  
        findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
      }
    

如果不添加 ,则在首次启动时,活动将为空。FragmentAonCreate()

如果您想知道所指的是什么,它是活动布局中框架布局的 ID。它最初包含一个 ,删除 TextView,使其如下所示:R.id.contentTextView

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

最后,并定义如下:ftcurrentFragment

Fragment currentFragment = null;
FragmentTransaction ft;

不确定优雅,但这有效。


推荐