load multiple tab contents in view pager without getting it again - android

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.

Related

Fragments display getting reset

I have setup a list of fragments using TabLayout and ViewPager on MainActivity as such
public class MainActivity extends AppCompatActivity implements Fragment1.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener, Fragment3.OnFragmentInteractionListener, Fragment4.OnFragmentInteractionListener
, Fragment5.OnFragmentInteractionListener{
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapater(getSupportFragmentManager(), getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#51ffff"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#ced21f"));
break;
case 1:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#51ffff"));
break;
case 2:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
#Override
public void onFragmentInteraction(Uri uri) {
}
private class CustomAdapater extends FragmentPagerAdapter {
private String fragments[] = {"Control", "Connection", "Log", "Others", "Others2"};
public CustomAdapater(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
case 1:
return new Fragment3();
case 2:
return new Fragment2();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
Somehow when I transit from Fragment1 to Fragment3 and back to Fragment1, the views on Fragment1 gets reset to original state(Like it was just created).
This only happens if I transit 2 fragment away from the current Fragment. For example 2->4 or 3->1.
Everything is fine if the transition is next to each other, for example 2->3 or 1->2.
This is my first android app so do pardon me.
It seems like it is creating a new fragment instead loading of existing ones
Your fragment get destroyed due to the fact that by default viewpager retain only one fragment at a time while moving around so you can simply use
viewPager.setOffscreenPageLimit(no_of_fragment_to_retain);
or
viewPager.setOffscreenPageLimit(3); // in your case
so when you come back from 4->1 or 3->1 you will get the previous stored state of your fragment. link to docs
First :
viewPager.setAdapter(new CustomAdapater(getSupportFragmentManager(), getBaseContext()));
Now see : in get Item method of View pager evrytime when you go to new tab a new instance of fragment is created So the previous view is lost . You should retain state in your fragment class.
or a simple solution is use FragmentstatepagerAdapter instead of FragmentPagerAdapter in your Adapter class.
Let me know if it helps

Set tab indicator position programatically in TabLayout support library

In my application i am using TabLayout from support library with view pager.I have 3 fragments in it.Suppose i am in fragA which has a button which on clicked takes me to fragB.I am sucessfully going to fragB but only problem is that the tab indicator remains at fragA.
Code
private void setupTablayout() {
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1, true);
tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
tabLayout.getTabAt(1).setIcon(R.drawable.status_1).setText("");
tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");
tvHeader.setText("STATUS");
tabLayout.setOnTabSelectedListener(this);
//tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
tab.setIcon(R.drawable.archive_1).setText("");
tvHeader.setText("ARCHIVES");
tvDate.setVisibility(View.GONE);
ivRefresh.setVisibility(View.VISIBLE);
break;
case 1:
tab.setIcon(R.drawable.status_1).setText("");
tvHeader.setText("STATUS");
tvDate.setVisibility(View.VISIBLE);
ivRefresh.setVisibility(View.VISIBLE);
break;
case 2:
tab.setIcon(R.drawable.settings_1).setText("");
tvHeader.setText("SETTINGS");
tvDate.setVisibility(View.GONE);
ivRefresh.setVisibility(View.GONE);
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
tab.setIcon(R.drawable.archive).setText("");
break;
case 1:
tab.setIcon(R.drawable.status).setText("");
break;
case 2:
tab.setIcon(R.drawable.settings).setText("");
break;
}
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
tab.setIcon(R.drawable.archive).setText("");
break;
case 1:
tab.setIcon(R.drawable.status).setText("");
break;
case 2:
tab.setIcon(R.drawable.settings).setText("");
break;
}
}
class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
// tabLayout.getTabAt(0).setIcon(R.drawable.archive_1).setText("");
// tabLayout.getTabAt(1).setIcon(R.drawable.status).setText("");
// tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");
fragment = new ArchivesFrag();
break;
case 1:
// tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
// tabLayout.getTabAt(1).setIcon(R.drawable.status_1).setText("");
// tabLayout.getTabAt(2).setIcon(R.drawable.settings).setText("");
fragment = StatusFrag.newInstance(listPosition);
break;
case 2:
// tabLayout.getTabAt(0).setIcon(R.drawable.archive).setText("");
// tabLayout.getTabAt(1).setIcon(R.drawable.status).setText("");
// tabLayout.getTabAt(2).setIcon(R.drawable.settings_1).setText("");
fragment = new SettingFrag();
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
FragA
{
((Home) getActivity()).getArchiveList();
}
Instead of
viewPager.setCurrentItem(tab.getPosition());
You can use,
tab.select();
This worked for me, after trial and error (SDK 23):
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
selectedTab = savedInstanceState.getInt("seltab");
allTabs.getTabAt(selectedTab).select(); // TabLayout
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("seltab",selectedTab);
super.onSaveInstanceState(outState);
}
Note: Set selectedTab when the fragment is loaded...
Have you Created the .Xml file for each Tab.
Main Activity
public class MainActivity extends TabActivity
{
TabHost tabHost;
TabSpec tab1,tab2,tab3,tab4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the TabHost that will contain the Tabs
tabHost = (TabHost)findViewById(android.R.id.tabhost);
// tabHost.setOnTabChangedListener(this);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third tab");
TabSpec tab4 = tabHost.newTabSpec("Fourth Tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("",getResources().getDrawable(R.drawable.video));
tab1.setContent(new Intent(this,VideoActivity.class));
//tab1.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));
tab2.setIndicator("",getResources().getDrawable(R.drawable.images));
tab2.setContent(new Intent(this,ImagesActivity.class));
//tab2.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));
tab3.setIndicator("",getResources().getDrawable(R.drawable.audio));
tab3.setContent(new Intent(this,AudioActivity.class));
//tab3.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));
tab4.setIndicator("",getResources().getDrawable(R.drawable.favourites));
tab4.setContent(new Intent(this,Favourites.class));
//tab4.setIndicator("", getResources().getDrawable(setBackgroundColor(Color.RED)));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
}
Make Xml For Each Tab/Class And Declare the classes in Manifest

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.

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

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