Android scrollable Tabbar - android

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 :)

Related

ClassCastException when new Fragment is added to SectionsPagerAdapter

I have an activity that holds my tabbed layout fragments. When I have 2 tabs everything works fine, but when I add a new tab I get
Caused by: java.lang.ClassCastException: rauhalamika.rcontrolble.HomeFragment cannot be cast to rauhalamika.rcontrolble.ManualFragment
Here is the SectionsPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
PresetsFragment presets = new PresetsFragment();
return presets;
case 1:
ManualFragment manual = new ManualFragment();
return manual;
case 2:
HomeFragment home = new HomeFragment();
return home;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Presets";
case 1:
return "Manual";
case 2:
return "Home";
}
return null;
}
}
And the problem occurs when I'm calling ManualFragment's method from the activity like this:
((ManualFragment)getSupportFragmentManager().findFragmentById(R.id.container)).updatePressure(values);
This method updates a bunch of TextViews in the ManualFragment.
Everything works as it should if I only have PresetsFragment a ManualFragment, but when I add HomeFragment the app crashes.
What am I doing wrong?
When using FragmentPagerAdapter you can not get fragment by id.
getSupportFragmentManager().findFragmentById(R.id.container)
change this to
getSupportFragmentManager().findFragmentByTag("f1")
For tagging fragment Read This thread.

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.

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)

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

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

How do you implement a FragmentActivity into your FragmentPagerAdapter?

I have a section my class which is for handling what fragment to load for the FragmentPagerAdapter. Right now it loads fragments just fine but now I want to implement a FragmentActivity and I am having a problem figuring how to create it as I cant use "newinstance" since its type of Activity, and on top of it, I dont know how to call that FragmentActivity as its different from loading just a Fragment via "newinstance" method. My second tab is the one that I would like to be extended to FragmentActivity.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
_context= getApplicationContext();
}
#Override
public Fragment getItem(int i) {
Fragment f = new Fragment();
switch(i){
case 0:
f=News.newInstance(_context);
break;
case 1:
f=Info.newInstance(_context);
break;
case 2:
f=Files.newInstance(_context);
break;
case 3:
f=Donate.newInstance(_context);
break;
}
/*
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
*/
return f;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
My guess is that I cant use getitem methods as that for only switching Fragments and that I would have to use something like:
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new BasicFragment());
ft.commit();
Can someone help point me in the right direction?
Can someone help point me in the right direction?
This is not supported. It certainly is not possible with FragmentPagerAdapter, which only works with fragments.

Categories

Resources