public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FragmentCallback{
navFragment1 navFrag1;
navFragment2 navFrag2;
navFragment3 navFrag3;
ViewPager viewPager;
Toolbar toolbar;
MovieFragment fragment4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navFrag1 = new navFragment1();
navFrag2 = new navFragment2();
navFrag3 = new navFragment3();
viewPager = new ViewPager();
fragment4 = new MovieFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, viewPager).commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} } else {
super.onBackPressed();
}
}
@Override
public void onFragmentChanged(int index) {
Fragment curFragment = null;
if (index == 0) {
curFragment = navFrag1;
toolbar.setTitle ("first navigation");
} } else if (index == 1) {
curFragment = navFrag2;
toolbar.setTitle ("Second Nav");
} } else if (index == 2) {
curFragment = navFrag3;
toolbar.setTitle ("third navigation");
} } else if (index == 3) {
curFragment = fragment4;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container, curFragment).addToBackStack(null).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_0) {
onFragmentChanged(0);
} } else if (id == R.id.nav_1) {
onFragmentChanged(1);
} } else if (id == R.id.nav_2) {
onFragmentChanged(2);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
=================================================================================
public class ViewPager extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.viewpager1, container, false);
Button button = rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rootView;
}
}
Click Button in Viewpager fragment to change to MovieFragment How should I approach this? Also, pressing the back button on navFragment will shut down the app....
fragment android
Well, it's a little strange that once addToBackStack() is applied, but if you press backkey with the navFragment, the app will not return to ViewPager fragment; I think you can handle the change to MovieFragment as below.
Create a method to replace with MovieFragment in Activity
public void replaceMoveFragment(){
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment4).addToBackStack(null).commit();
}
OnClick() in ViewPager fragment
Button.setOnClickListener(newView).OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).replaceMovieFragment();
}
});
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.