Navigation Drawer and ViewPager conflict - android

I'm currently developing an android application where I'm using a navigation drawer as well as the swipe gesture using viewPager to traverse through fragments.
Basically, I want the user to be able to select pages (fragments) using the navigation drawer and be able to swipe to the next page (fragment).
My problem right now is that they overlap. The swipe gesture works as expected but when I select another page on the nav drawer they underlying page doesn't disappear. I need someway to "refresh" each fragment.
Here is my code in the MainActivity.java page
package com.example.home.cloud;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private static final int NUM_PAGES = 10;
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter
{
public MyFragmentStatePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position) {
final Fragment result;
switch (position) {
case 0:
result= new HomeFragment();
break;
case 1:
result= new OverviewFragment();
break;
case 2:
result= new BenFragment();
break;
case 3:
result= new TechFragment();
break;
case 4:
result= new ArcFragment();
break;
case 5:
result= new DmodFragment();
break;
case 6:
result= new SmodFragment();
break;
case 7:
result= new VirtFragment();
break;
case 8:
result= new StorageFragment();
break;
case 9:
result= new SecurityFragment();
break;
default: result=null;
break;
}
/* if (result != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, result);
fragmentTransaction.commit();
}*/
return result;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new OverviewFragment();
title = getString(R.string.title_overview);
break;
case 2:
fragment=new BenFragment();
title="Benefits and Risks";
break;
case 3:
fragment=new TechFragment();
title="Technologies behind Cloud Computing";
break;
case 4:
fragment = new ArcFragment();
title="Architecture";
break;
case 5:
fragment= new DmodFragment();
title="Deployment Models";
break;
case 6:
fragment = new SmodFragment();
title="Service Models";
break;
case 7:
fragment = new VirtFragment();
title="Virtualization";
break;
case 8:
fragment = new StorageFragment();
title="Data Storage";
break;
case 9:
fragment = new SecurityFragment();
title="Security";
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}

The trouble is that you're displaying your fragments in two different ways. Your displayView() method is creating new fragments and displaying them, rather than showing the ones which are already loaded in the ViewPager. To fix this, you need displayView() to instead set your ViewPager position.
private void displayView(int position) {
mPager.setCurrentItem(position);
getSupportActionBar().setTitle(mPagerAdapter.getPageTitle(position));
}

Related

how to go back to previous fragment when hardware back button is pressed

i am begineer in android development , i would like to configure hardware backbutton to go back to previous fragment and the fragment titles should also change according to the current fragment ,
here is my Main activity source :
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new MicrosoftDskFragment();
title = "Microsoft Dsk";
break;
case 2:
fragment = new GoogleDskFragment();
title = "Google Dsk";
break;
case 3:
fragment = new AppleDskFragment();
title = "Apple Dsk";
break;
case 4:
fragment = new OthersDskFragment();
title = "Others Dsk";
break;
case 5:
fragment = new AboutUSFragment();
title = "About US";
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null).commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}
plz hel me in fixing this .
You are adding the transaction to the backstack, so all you should need in order to get back to the previous one should be:
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackstack(null, 0);
You may also consider adding an id for each fragment and passing it when calling
fragmentTransaction.addToBackStack(null).commit();
instead of the null. This way, you will be able to do this:
fragmentTransaction.addToBackStack(myFragmentId).commit();
//Later on...
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackstack(myFragmentId, 0);
To go back to a specific fragment, not just the last one :-)
every time when you add a fragment just add addToBackStack() method before commit and then in your activity on back pressed try this
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 1) {
goToExitApplication(
getResources().getString(R.string.close_app), true);
} else {
String title = getSupportFragmentManager().getBackStackEntryAt(count - 2).getName();
getSupportFragmentManager().popBackStack();
getSupportActionBar().setTitle(title);
}
}
I have used this in my code:
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0 ){
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
To push the Fragment I used:
MyFragment_Items myf = new MyFragment_Items();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_layout, myf);
transaction.addToBackStack(null);
transaction.commit();
I pushed the fragment here and popped it back onBackPressed.
It will Surely work for you If there is any Fragment on the back stack of current fragment , popBackStackImmediate() works..
#Override
public void onBackPressed() {
if(getFragmentManager().getBackStackEntryCount() > 0){
getFragmentManager().popBackStackImmediate();
}
else{
super.onBackPressed();
}
}

how change Action Bar title from fragment to fragment?

I want to change the Action Bar title from fragment to fragment..
example loginfragment to registerfragment..
i have button at loginfragment that will redirect to register fragment..
case R.id.textSignup:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, RegisterFragment.newInstance())
.commit();
break;
and i add this code in my registerfragment
public static RegisterFragment newInstance() {
RegisterFragment fragment = new RegisterFragment();
return fragment;
}
public RegisterFragment () {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(3);
}
then here is the MainActivity
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_home);
break;
case 2:
mTitle = getString(R.string.title_login);
break;
case 3:
mTitle = getString(R.string.title_register);
break;
}
}
#SuppressWarnings("deprecation")
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
the problem is the register title is not displaying even i add onSectionAttached(3) in the registerfragment..
here is the MainActivity Code
package com.example.administrator.mosbeau;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
Boolean InternetAvailable = false;
Seocnd detectconnection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detectconnection = new Seocnd(this);
InternetAvailable = detectconnection.InternetConnecting();
if (InternetAvailable) {
} else {
NointernetFragment fragment = new NointernetFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
// NEW STUFF
if(position == 0){
fragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment.newInstance())
.commit();
} else if (position == 1){
fragmentManager.beginTransaction()
.replace(R.id.container, LoginFragment.newInstance())
.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_home);
break;
case 2:
mTitle = getString(R.string.title_login);
break;
case 3:
mTitle = getString(R.string.title_register);
break;
}
}
#SuppressWarnings("deprecation")
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
thanks
joe
i.e bcoz you not calling restoreActionBar() function anywhere in your code.
you can change actionbar Title as you navigate from fragment to fragment by like this aswell in onCreateView()of each Fragment.
ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle("Your title");
The simplest way is to call
getActivity().setTitle(your_title)
in the onCreate() method in all of your fragments

Controlling which fragment is shown from navDrawer

I am working on this application that has a viewPager and a few swipeable fragments in its MainActivity. Today I implemented navigation drawer but I do not know how to control which fragment is shown from drawer`s DrawerItemClickListener class. Is there a solution or I was just moving in a wrong direction and have to change the way navigation drawer is implemented?
Tutorial suggests to use the getFragmentManager method which does not work for me.
Thanks.
Here is DrawerItemClickListener class:
package com.freestylers.druskischool;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView;
public class DrawerItemClickListener implements ListView.OnItemClickListener
{
public int FragmentNumber;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
selectItem(position);
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new StartFragment();
FragmentNumber=0;
break;
case 1:
fragment = new MesFragment();
FragmentNumber=1;
break;
case 2:
fragment = new NaujienosFragment();
FragmentNumber=2;
break;
case 3:
fragment = new InstruktoriaiFragment();
FragmentNumber=3;
break;
case 4:
fragment = new MetodikaFragment();
FragmentNumber=4;
break;
case 5:
fragment = new GalerijaFragment();
FragmentNumber=5;
break;
case 6:
fragment = new InstruktoriaiFragment();
FragmentNumber=6;
break;
case 7:
fragment = new AtsiliepimaiFragment();
FragmentNumber=7;
break;
case 8:
fragment = new DrufunparkFragment();
FragmentNumber=8;
break;
case 9:
FragmentNumber=9;
fragment = new KontaktaiFragment();
break;
default:
break;
}
if (fragment != null) {
//tutorial suggests this code:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
And here is my MainActivity.java:
package com.freestylers.druskischool;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
public class MainActivity extends ActionBarActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
ViewPager pager;
FragmentStatePagerAdapter adapter2;
/* Just some random URLs
*
* Each page of our pager will display one URL from this array
* Swiping, to the right will take you to the next page having
* the next URL.
*/
String[] fragments={
"start",
"mes",
"naujienos",
"instruktoriai",
"metodika",
"galerija",
"kainos",
"atsiliepimai",
"drufunpark",
"kontaktai"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[10];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Pradžia");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Mes");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Naujienos");
drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Instruktoriai");
drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Metodika");
drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Galerija");
drawerItem[6] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Kainos");
drawerItem[7] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Atsiliepimai");
drawerItem[8] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Drufunpark");
drawerItem[9] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Kontaktai");
mNavigationDrawerItemTitles=
getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this,
R.layout.listview_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
pager=(ViewPager)findViewById(R.id.my_pager);
adapter2=new FragmentStatePagerAdapter(
getSupportFragmentManager()
){
#Override
public int getCount() {
// This makes sure getItem doesn't use a position
// that is out of bounds of our array of fragments
return fragments.length;
}
#Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
//(fragments[position]);
if(position==0){
return StartFragment.newInstance();
}
else if(position==1){
return MesFragment.newInstance();
}
else if(position==2){
return NaujienosFragment.newInstance();
}
else if(position==3){
return InstruktoriaiFragment.newInstance();
}
else if(position==4){
return MetodikaFragment.newInstance();
}
else if(position==5){
return GalerijaFragment.newInstance();
}
else if(position==6){
return KainosFragment.newInstance();
}
else if(position==7){
return AtsiliepimaiFragment.newInstance();
}
else if(position==8){
return DrufunparkFragment.newInstance();
}
else if(position==9){
return KontaktaiFragment.newInstance();
}
else{return KontaktaiFragment.newInstance();}
}
};
//Let the pager know which adapter it is supposed to use
pager.setAdapter(adapter2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/*#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
itu f (id == R.id.action_start) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle other action bar items...
/* switch (item.getItemId()) {
case R.id.action_settings:
return true;
}*/
return super.onOptionsItemSelected(item);
}
public static void ChooseFragment(Fragment fragment) {
}
}
I figured out my mistake, it all has to be inside MainActivity.java, DrawerItemClickListener is an inner class.
Still have not found a way to navigate between swipeable fragments from the navDrawer, probably it is impossible.

Play Store Arrow Animation in Navigation Drawer

Last week I started developing on android. I'm making an app for the gym where I train. I've made the main functions so far but i'm stuck trying to add the animation when the navigation drawer is opened or closed. Right now the navigation drawer is fully functional but the arrow doesn't animate as you can see from the picture:
Image
This is the link to the project made with Android Studio:
App
This is the Main Activity. java of the app:
package com.dev.lollos.argus1910;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainMenu extends android.support.v7.app.ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
public void OpenSettings(View view) {
// Do something in response to button
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment;
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position) {
default:
case 0:
fragment = new Home_Fragment();
break;
case 1:
fragment = new Atleti_Fragment();
break;
case 2:
fragment = new Societa_fragment();
break;
case 3:
fragment = new Manifestazioni_Fragment();
break;
case 4:
fragment = new Risultati_Fragment();
break;
case 5:
fragment = new Storia_Fragment();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
case 6:
mTitle = getString(R.string.title_section6);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main_menu, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void OpenSettings(MenuItem item) {
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainMenu) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
I hope you can help me. Thanks in advance
You need to sync the drawer:
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(<context>, drawerLayout, 0, 0, 0);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();

how to call an intent with my swipe buttons

Here I have a code where you can swipe the tab menus, but I want that after a swipe another activity is opened, for example Simmering.class.
I think this should go with an intent, but how or where should I put it.
Here is the code:
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressWarnings("unused")
public class MainActivity extends FragmentActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Try adding below code in onCreateView method of your fragment class. check for the position after which you want to start new activity. And in it call the startActivity method.
Intent intent = new Intent(getApplicationContext(), Simmering.class);
getApplicationContext().startActivity(intent);
Do you know, that starting new activity after swipe hide page you're swiping to? If your answer is yes, you can use ViewPager's onPageChangeListener. It's methods are invoked when page is changed/updated etc. Usage is following:
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int index) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
})
i already found an option to achieve what i wanted, here is the code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, R.string.refresh_clicked, Toast.LENGTH_SHORT).show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
switch(i){
case 0: fragment = new Home(); break;
case 1: fragment = new Simmering(); break;
case 2: fragment = new LugnerCity(); break;
case 3: fragment = new Gmunden(); break;
case 4: fragment = new Salzburg(); break;
case 5: fragment = new Linz(); break;
case 6: fragment = new Saalbach(); break;
case 7: fragment = new Innsbruck(); break;
case 8: fragment = new Reutte(); break;
case 9: fragment = new Bregenz(); break;
case 10: fragment = new Kufstein(); break;
case 11: fragment = new Bratislava(); break;
}
return fragment;
}
#Override
public int getCount() {
return 12;
}
#Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0: return "Home";
case 1: return "Simmering";
case 2: return "Lugner City";
case 3: return "Gmunden";
case 4: return "Salzburg";
case 5: return "Linz";
case 6: return "Saalbach";
case 7: return "Innsbruck";
case 8: return "Reutte";
case 9: return "Bregenz";
case 10: return "Kufstein";
case 11: return "Bratislava";
}
return null;
}
}

Categories

Resources