f7dc3c357a0d41df6244c1dd10cdf10264cfaef6.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.example.myapplication.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import androidx.annotation.NonNull;
  7. import androidx.appcompat.widget.Toolbar;
  8. import androidx.drawerlayout.widget.DrawerLayout;
  9. import androidx.fragment.app.FragmentManager;
  10. import androidx.fragment.app.FragmentTransaction;
  11. import androidx.navigation.NavController;
  12. import androidx.navigation.Navigation;
  13. import androidx.navigation.ui.AppBarConfiguration;
  14. import androidx.navigation.ui.NavigationUI;
  15. import com.example.myapplication.R;
  16. import com.example.myapplication.ui.list.PullToRefreshFragment;
  17. import com.example.myapplication.ui.slideshow.SlideshowFragment;
  18. import com.google.android.material.navigation.NavigationView;
  19. public class MainActivity extends BaseActivity {
  20. private AppBarConfiguration mAppBarConfiguration;
  21. FragmentManager fragmentManager;
  22. FragmentTransaction fragmentTransaction;
  23. PullToRefreshFragment pullToRefreshFragment;
  24. SlideshowFragment slideshowFragment;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. Toolbar toolbar = findViewById(R.id.toolbar);
  30. setSupportActionBar(toolbar);
  31. /*FloatingActionButton fab = findViewById(R.id.fab);
  32. fab.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View view) {
  35. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  36. .setAction("Action", null).show();
  37. }
  38. });*/
  39. DrawerLayout drawer = findViewById(R.id.drawer_layout);
  40. NavigationView navigationView = findViewById(R.id.nav_view);
  41. // Passing each menu ID as a set of Ids because each
  42. // menu should be considered as top level destinations.
  43. mAppBarConfiguration = new AppBarConfiguration.Builder(
  44. R.id.nav_pullToRefresh, R.id.nav_slideshow, R.id.nav_slideshow,
  45. R.id.nav_tools, R.id.nav_jzvd, R.id.nav_send)
  46. .setDrawerLayout(drawer)
  47. .build();
  48. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  49. NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  50. NavigationUI.setupWithNavController(navigationView, navController);
  51. fragmentManager = getSupportFragmentManager();
  52. pullToRefreshFragment = new PullToRefreshFragment();
  53. slideshowFragment = new SlideshowFragment();
  54. navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  55. @Override
  56. public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  57. switch (menuItem.getItemId()) {
  58. case R.id.nav_pullToRefresh:
  59. fragmentTransaction = fragmentManager.beginTransaction();
  60. fragmentTransaction.replace(R.id.nav_host_fragment, pullToRefreshFragment).commit();
  61. break;
  62. case R.id.nav_LRecyclerView:
  63. startActivity(new Intent(MainActivity.this, com.lzx.demo.MainActivity.class));
  64. break;
  65. case R.id.nav_slideshow:
  66. fragmentTransaction = fragmentManager.beginTransaction();
  67. fragmentTransaction.replace(R.id.nav_host_fragment, slideshowFragment).commit();
  68. break;
  69. }
  70. return false;
  71. }
  72. });
  73. }
  74. @Override
  75. public boolean onCreateOptionsMenu(Menu menu) {
  76. // Inflate the menu; this adds items to the action bar if it is present.
  77. getMenuInflater().inflate(R.menu.main, menu);
  78. return true;
  79. }
  80. @Override
  81. public boolean onSupportNavigateUp() {
  82. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  83. return NavigationUI.navigateUp(navController, mAppBarConfiguration)
  84. || super.onSupportNavigateUp();
  85. }
  86. }