How do I tap to move to the next ViewPager page fragment? - android

I have a ViewPager setup that's been working fine for my app but it only lets me swipe the screen to move to the next slide. What I want is to also be able to move to the next slide by tapping the screen. Is this possible?
I've been trying to figure this out for a week but I couldn't find the right code to make it work.
Here's what I've done so far:
public class JoyfulHappyfuntime extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_joyful_happyfuntime);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new BoopTheName();
case 1: return new Creed();
case 2: return new Waffles();
case 3: return new Smilea();
case 4: return new Smileb();
case 5: return new Smilec();
case 6: return new PancakesOne();
case 7: return new FirstJoyfulHappyfuntime();
case 8: return new Waffles();
case 9: return new Smilea();
case 10: return new Smileb();
case 11: return new Smilec();
//etc
default: return new Continue();
}
}
#Override
public int getCount() {
return 74;
}
}
}

You can try one of those :
Adding a transparent view ontop where you handle the onClick to change
the position to the next.
using a ViewPagerIndicator on top to let the user click on the section he wants
View Pager Tutorial

Related

load multiple tab contents in view pager without getting it again

i have a view pager in tabLayout that getting it's content from a JSON
each page has differ contents
all i want to do is to get each page content once
is there a way to do this
thanks in advance
Yes it's possible. My answer is not from an official source but it's from my previous experience.
Create your ViewPager as a member of the Activity:
private ViewPager pager;
Then inside onCreate() method, initialize your ViewPager:
pager=(ViewPager)findViewById(R.id.pager);
Initialize your tablayout and and a tab selected listener:
tabLayout.addOnTabSelectedListener(
new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition())
{
case 0:
pager.setCurrentItem(0,true);
break;
case 1:
pager.setCurrentItem(1,true);
break;
case 2:
pager.setCurrentItem(2,true);
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
switch (tab.getPosition())
{
case 0:
pager.setCurrentItem(0,true);
break;
case 1:
pager.setCurrentItem(1,true);
break;
case 2:
pager.setCurrentItem(2,true);
break;
}
}
}
);
Set the an adapter to your ViewPager:
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
Create a private inner class MyPagerAdapter that extends FragmentPagerAdapter:
private class MyPagerAdapter extends FragmentPagerAdapter
{
private Tab1 tab1;private Tab2 tab2;private Tab3 tab3;
//Tab1,Tab2,Tab3 representing your respective fragments
public MyPagerAdapter(FragmentManager fm) {
super(fm);
//initialize all your fragments at once inside the constructor
tab1=new Tab1();
tab2=new Tab2();
tab3=new Tab3();
}
#Override
public int getCount() {
return 3;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
//Return each fragment according to a position
switch (position)
{
case 0:
return tab1;
case 1:
return tab2;
case 2:
return tab3;
}
return null;
}
}
That's how I handled my case. Good coding.

Android how to use a activity in default case in switch

I'm using a view pager library in my application, where I use switch case with view pager. Here my code:
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return Slider_One_Fragment.newInstance();
case 1:
return Slider_Two_Fragment
.newInstance("Slider_Two_Fragment, Instance 1");
case 2:
return Slider_Three_Fragment
.newInstance("Slider_Three_Fragment, Instance 1");
default:
return new MainActivity();
}
}
#Override
public int getCount() {
return 3;
}
}
here the return type is of type "Fragment". But in default case I need to have a activity "MainActivity". Can anybody please help me to solving this issue?
the way the the ViewPager is setup it has to be a Fragment and not An Activity. One suggestion that you could do is move all the logic out of the MainActivity and into a MainFragment. then have your MainActivity contain the MainFragment.

Change fragment on click

I have to implement an application in which the user can click through a bottom_bar between 5 pages (although google does not recommend it in recent documents, the customer is always right).
So I created a swipe activity with automatic tool android study.
Question: how do I start the transation, after the click on the button, and change fragment??
Here is the code (at the time the activity change with the swipe)
public class Home extends ActionBarActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ImageView home, ospitalita,multimedia,territoro, prodotti;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
home = (ImageView) findViewById(R.id.ibHome);
ospitalita = (ImageView) findViewById(R.id.ibOspitalita);
multimedia = (ImageView) findViewById(R.id.ibMultimedia);
territoro = (ImageView) findViewById(R.id.ibTerritorio);
prodotti = (ImageView) findViewById(R.id.ibProdotti);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
View.OnClickListener gestore = new View.OnClickListener() {
#Override
public void onClick(View v) {
//this is the bottom bar, on click, change icon color
switch (v.getId()){
case R.id.ibHome:
changeBottomIcon("home");
mSectionsPagerAdapter.getItem(0); //do nothing
break;
case R.id.ibOspitalita:
changeBottomIcon("ospitalita");
mSectionsPagerAdapter.getItem(1);
break;
case R.id.ibMultimedia:
changeBottomIcon("multimedia");
mSectionsPagerAdapter.getItem(2);
break;
case R.id.ibTerritorio:
changeBottomIcon("territorio");
mSectionsPagerAdapter.getItem(3);
break;
case R.id.ibProdotti:
changeBottomIcon("prodotti");
mSectionsPagerAdapter.getItem(4);
break;
}
}
};
home.setOnClickListener(gestore);
ospitalita.setOnClickListener(gestore);
multimedia.setOnClickListener(gestore);
territoro.setOnClickListener(gestore);
prodotti.setOnClickListener(gestore);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* 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.
switch (position) {
// change bottom doesn't work here
case 0:
// changeBottomIcon("home");
return new fragment_home();
case 1:
//changeBottomIcon("territorio");
return new fragment_territorio();
case 2:
//changeBottomIcon("ospitalita");
return new fragment_ospitalita();
case 3:
//changeBottomIcon("multimedia");
return new fragment_multimedia();
case 4:
//changeBottomIcon("prodotti");
return new fragment_prodotti();
}
return null;
}
#Override
public int getCount() {
//num pagine
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
//Not visible, no nav bar
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.home);
case 1:
return getString(R.string.territorio);
case 2:
return getString(R.string.ospitalita);
case 3:
return getString(R.string.multimedia);
case 4:
return getString(R.string.prodotti);
}
return null;
}
}
//simple method to change icon color
public void changeBottomIcon(String tipo){
.
.
.
//lo ometto perchè inutile
}
}
You can achieve this by calling:
mViewPager.setCurrentItem(index);
(see the android dev docs for further information)

Send network request as FragmentPagerAdapter shows the Fragment

I am using the FragmentPagerAdapter which shows three fragments by using ViewPager. I have setOffscreenPageLimit to 3 so all the three fragments send the network request simultaneously as they are visible(I have send the network request on Fragment.onActivityCreated()). But I want network request is send only when Fragment is visible(means user is scrolled to that fragment).
Note: I am using the setOffscreenPageLimit to 3 because every time the fragment is created no new network request is created and show the catches fragment
Making network requests in oncreateview () of every fragment should do what you want to.
example :Something that ive been using .
Parent activity with fragment pager adapter.
public class HomeActivity extends SherlockFragmentActivity{
private static final String[] CONTENT = new String[] { "Fragment 1", "Fragment 2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homexml);
Bundle extra = getIntent().getExtras();
int i = Integer.parseInt(extra.getString("id"));
FragmentPagerAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
switch (i)
{
case 2:
pager.setCurrentItem(0);
break;
case 3:
pager.setCurrentItem(1);
// break;
// case 4:
// pager.setCurrentItem(3);
// break;
// case 5:
// pager.setCurrentItem(4);
// break;
// case 6:
// pager.setCurrentItem(5);
// break;
// case 7:
// pager.setCurrentItem(6);
// break;
// default:
// pager.setCurrentItem(0);
// break;
}
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setFooterColor(Color.parseColor("#e7c450"));
}
class FragmentAdapter extends FragmentPagerAdapter {
SherlockFragment frg;
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public SherlockFragment getItem(int position) {
switch (position)
{
case 0:
frg = new fragment1();
break;
case 1:
frg = new fragment2();
break;
}
return frg;
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position];
}
// #Override public int getIconResId(int index) {
// return ICONS[index];
// }
#Override
public int getCount() {
return CONTENT.length;
}
}
Pass specify the fragments in the adapter. make network requests in onCreateView() of every fragment. that way network requests will be only made when fragment is visible.

Android scrollable Tabbar

is it somehow possible to have horizontal scrollable tabbar if there are more than e.g. 10 tabs in it?
Have anybody implemented something like this?
Mur
Ps.
It was not nice, what I did: I've deleted almost the same topic, I'd started yesterday. A big SORRY for man, who answered it already, even if it wasn't really the answer I'm looking for.
There actually is a way to implement this and it is called swipeable tabs layout. I have managed to use it in one of the apps I developed and published on Google Play. Here is the code to implement it:
SectionPagerAdapter class:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new HomeFragment();
case 1:
return fragment = new EventFragment();
case 2:
return fragment = new CoreTeamFragment();
case 3:
return fragment = new MapsFragment();
case 4:
return fragment = new FacebookFragment();
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 5 total pages.
return 5;
}
#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);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
}
return null;
}
}
Main class
public class CentruActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_centru);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// getActionBar();
}
public ActionBar getActionBar() {
return null;
}
}
Hope this helps :)

Categories

Resources