1

我用底栏活动创建了一个新项目。这是生成的代码:

package com.aaron.waller.mrpolitik;

import android.content.Intent;
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 com.aaron.waller.mrpolitik.tabs.KommentareFragment;

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);
                    Intent fragen = new Intent(MainActivity.this, KommentareFragment.class);
                    startActivity(fragen);
                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);
    }

}

我想在导航栏上设置这三个片段: FirstFragment SecondFragment ThirdFragment

我也想在片段之间滑动,我该怎么做?

4

2 回答 2

0

您不能通过 Intent 加载 Fragments。要通过 BottomNavigationBar 加载片段,您可以执行以下操作:

首先添加FrameLayout到我们的activity_main.xml

  <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

然后用 Fragments 替换这个容器onNavigationItemSelected

 @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fm = getFragmentManager();
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    fm.beginTransaction().replace(R.id.container, new FirstFragment()).commit();                      
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    fm.beginTransaction().replace(R.id.container, new SecondFragment()).commit();  
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    fm.beginTransaction().replace(R.id.container, new ThirdFragment()).commit();  
            }
            return true;
        }

要在可以使用ViewPager的片段之间创建滑动效果。是一个很好的教程,如何使用 ViewPager 在片段之间滑动。希望这可以帮助。

于 2017-03-29T19:24:53.333 回答
0
NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);
                navController.navigate(R.id.navigation_settings);



//nav_host_fragment  = your  BottomNavigationView name

// navigation_settings   =  destination  fragment 
于 2021-08-20T10:15:41.277 回答