使用BottomNavigationView在片段之间移动

时间:2018-10-01 09:29:02

标签: android android-layout android-fragments bottomnavigationview

我正在尝试使用BottomNavigationView在3个片段之间移动,虽然它可以正常工作,但是存在一个小问题。

我有2个片段和1个活动,该活动的代码在各个片段之间移动。

当按下底部导航栏上的按钮时,我可以看到底部栏的图像发生变化。

在一个片段中,我做了一个按钮,按下该按钮将移至另一个片段,并且效果很好。

我面临的问题是,当我按下按钮移至另一个片段时,底部条形图不会改变。

这是我主要活动的代码:

public class MainActivity extends AppCompatActivity {


    public static Bundle bundle = new Bundle();


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

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
        }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_announcement:
                        selectedFragment = new AnnouncementFragment();
                        break;
                }

                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };
}

这是带有按钮的片段的代码:

public class HomeFragment extends Fragment {

    Calendar calendar = Calendar.getInstance();
    Button buttonTrack;
    int year, month, day;

    public HomeFragment() {
        //Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);

        this.year = calendar.get(Calendar.YEAR);
        this.month = calendar.get(Calendar.MONTH) + 1;
        this.day = calendar.get(Calendar.DAY_OF_MONTH);

        buttonTrack = view.findViewById(R.id.btn_track);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        final String currentTime = simpleDateFormat.format(new Date());


        buttonTrack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MainActivity.bundle.putString("time", currentTime);

                getFragmentManager().beginTransaction().replace(R.id.fragment_container, new AnnouncementFragment()).commit();

            }
        });

        return view;

    }
}

另一个片段:

public class AnnouncementFragment extends Fragment {

    String timeFromBundle;
    TextView announcementHere, timeView;


    public AnnouncementFragment() {
    // Required empty public constructor
        }

    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_announcement, container, false);

        timeFromBundle = MainActivity.bundle.getString("time");

        announcementHere = view.findViewById(R.id.announcement_here);
        timeView = view.findViewById(R.id.time_view);
        timeView.setText(timeFromBundle);

        return view;
    }

}

任何人都可以告诉我何时应该修复以使其更改底栏中的图像,即使我通过按片段内的按钮移动到另一个片段时也可以?预先感谢!

1 个答案:

答案 0 :(得分:1)

在按钮内的HomeFragment中,单击监听器,而不是此行

getFragmentManager().beginTransaction().replace(R.id.fragment_container, new AnnouncementFragment()).commit();

使用此

((BottomNavigationView)getActivity().findViewById(R.id.bottom_navigation)).setSelectedItemId(R.id.nav_announcement);

它将在您的活动中呼叫您的BottomNavigationView.OnNavigationItemSelectedListener