BottomNavigation视图后退按钮处于按下状态

时间:2018-07-18 19:26:14

标签: android android-fragments

因此,让我尝试解释一下:我有一个带有三个按钮的底部导航视图,每个按钮按下时都会加载一个片段。但是,当按下后退按钮时,我已将后退栈编程为返回到先前的片段。但是,更改后退按钮后,导航按钮的颜色不会改变。我知道这与状态检查有关,但是我不知道如何在我的代码上实现它。这是代码

这是菜单页面,它仅设置main_frame片段所在的底部导航视图:

public class menuPage extends AppCompatActivity {
BottomNavigationView mainNav;
FrameLayout mainFrame;

private MoviesFragment moviesFragment;
private HomeFragment homeFragment;
private ProfileFragment profileFragment;

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


    mainFrame= (FrameLayout) findViewById(R.id.main_frame);
    mainNav = (BottomNavigationView) findViewById(R.id.main_nav);

    moviesFragment= new MoviesFragment ();
    homeFragment = new HomeFragment();
    profileFragment = new ProfileFragment ();

    removeFragment(homeFragment);

    mainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.nav_movies :
                    setFragment(moviesFragment);
                    return true;

                case R.id.nav_home :
                    setFragment(homeFragment);
                    return true;

                case R.id.nav_profile:
                    setFragment(profileFragment);
                    return true;

                    default:
                        return false;
            }
        }
    });
}
private void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, fragment);
    fragmentTransaction.addToBackStack("detail");
    fragmentTransaction.commit();
}
private void removeFragment(Fragment fragment){
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.main_frame, fragment);
    fragmentTransaction.disallowAddToBackStack();
    fragmentTransaction.commit();
}

}

这是“家庭片段”,“电影片段”和“个人资料片段”的代码。您可以忽略cos内编写的代码,因为我知道启动该代码后它可以正确执行另一项活动,而与该问题无关

家庭片段

public class HomeFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_agenda, container, false);

    return view;
}

}

电影片段

public class MoviesFragment extends Fragment {
ListView listofmovies;
ArrayList<String> genres;
ArrayAdapter<String> listview;


NowShowing nowShowing;
ComingSoon comingsoon;

@Override//inflate this
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_movies, container, false);

    listofmovies = (ListView) view.findViewById(R.id.movielist);
    genres = new ArrayList<String>();
    listview = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, genres);
    listofmovies.setAdapter(listview);

    //The types of options
    genres.add("Now Showing");
    genres.add("Coming Soon");
    genres.add("July");
    genres.add("June");


    nowShowing = new NowShowing();
    comingsoon = new ComingSoon();

    listofmovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0) {
                Intent nextPage = new Intent(view.getContext(), NowShowing.class);
                startActivityForResult(nextPage,0);
            }
            if (position == 1){
                Intent nextPage = new Intent(view.getContext(),ComingSoon.class);
                startActivityForResult(nextPage,1);
            }
        }
    });
    return view;
}

}

个人资料片段

public class ProfileFragment extends Fragment {
ListView management;
ArrayList<String> mis;
ArrayAdapter<String> Adapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile,container, false );
    management= (ListView) view.findViewById(R.id.management);
    mis= new ArrayList<String>();
    Adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, mis);
    management.setAdapter(Adapter);
    //add management settings
    mis.add("Settings");
    mis.add("Favourites");
    mis.add("Log Out");

    userprofilefragment userprofilefragment = new userprofilefragment();
    FragmentManager manager = getFragmentManager();
    manager.beginTransaction()
            .replace(R.id.profilelayout, userprofilefragment, userprofilefragment.getTag())
            .commit();
    return view;
}

非常感谢您的帮助。上下文是,我只是为学校作业制作一个简单的电影应用程序,而我仍然是Java的初学者。 谢谢

0 个答案:

没有答案