Problem: I was modifying a small app where three tabs are there in fragment but all refers to text string. I wanted to use layout xml so that I can make good graphics in it. How can I do it in this code:
Information_tab refers to a string where I can put text, but I want a layout.
public class AdapterInformation extends FragmentPagerAdapter {
public AdapterInformation(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Bundle bundle = new Bundle();
int imgResId = 0;
int tab = 0;
int colorResId = 0;
switch (index) {
case 0:
tab = R.string.information_tab1;
break;
case 1:
tab = R.string.information_tab2;
break;
case 2:
tab = R.string.information_tab3;
break;
}
bundle.putInt("image", imgResId);
bundle.putInt("tab",tab);
bundle.putInt("color", colorResId);
SwipeTabFragment swipeTabFragment = new SwipeTabFragment();
swipeTabFragment.setArguments(bundle);
return swipeTabFragment;
}
#Override
public int getCount() {
return 3;
}}
you can add fragment layout like this.
private void setupViewPager(final ViewPager viewPager) {
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Pending Upload");
adapter.addFragment(new TwoFragment(), "Complete Upload");
viewPager.setAdapter(adapter);
}
and your fragmentPagerAdapter code is like this:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
and also add your setupViewPager method in onCreate()
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
setupViewPager(viewPager)
tabLayout.setupWithViewPager(viewPager);
Note: Do not use getItemPosition() method if its possible.. it
will produce redundancy of your listItems or ArrayList.. bcz every
time you swipe to other layout it will refresh page automatically..
its good if want this facility.. but you have write some code
regarding to handle this method.. and its also going to slow your
swipe animation if you have large amount of data.
For more information about tab layout and fragment: you can refer this one:
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
Related
I am designing a application in which i am facing problem,
I have a String date; in MainActivity.class and there are two fragments.
InboundDate & OutboundDate
I designed it by TabLayout & ViewPager, I am getting issue when i sending date to fragments & have to call WebServices by POST request on both the fragments.
Below is the date i have in MainActivity.class, How i send this date to both the fragments & call jsonRequest and set Recyclerview to both the fragments Or if i call the jsonRequest to MainActivity itself then how can i display to both the fragments.
date = getIntent().getStringExtra("DATE");
Log.e("DATE",""+date);
ViewPager & TabLayout Code:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new InBoundDate(),"INBOUND");
adapter.addFrag(new OutBoundDate(),"OUTBOUND");
viewPager.setAdapter(adapter);
}
//viewPager adapter ----- inbound & outbound
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I got the answer of this question by searching over the internet.
In this question i have to send string to MainActivity.java to fragments present on same Activity.
Note : We have to create bundle object & have to send string(any dataType) by bundle.
I have two fragments InBoundDate & OutBoundDate.
In MainActivity.java in setupViewpager ()
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//bundle
bundle =new Bundle();
bundle.putString("DATE","2017-10-19");
InBoundDate frag_in = new InBoundDate();
OutBoundDate frag_out = new OutBoundDate();
frag_in.setArguments(bundle);
frag_out.setArguments(bundle);
adapter.addFrag(frag_in,"INBOUND");
adapter.addFrag(frag_out,"OUTBOUND");
viewPager.setAdapter(adapter);
}
ViewPager Adapter :
//viewPager adapter ----- inbound & outbound
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
In your Fragments get String by this way :
String dateTxt;
private Bundle bundle;
//within OncreateView
bundle = this.getArguments();
dateTxt = bundle.getString("DATE");
Log.e("DATE_FRAG_INBOUND",""+dateTxt);
i have a viewPager that contains 3 tabs fragments.
the first and the second fragments works fine but when i sweep to the third tab
i do not see him.
the third tab has shown only when the activity with the viewPager has reload(move to another activity and return).
my code:
private void setupViewPager(ViewPager viewPager) {
adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), Constants.TAB_0);
adapter.addFragment(new Fragment2(), Constants.TAB_1);
adapter.addFragment(new Fragment3(), Constants.TAB_2);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
}
pager adapter:
public class PagerAdapter extends FragmentPagerAdapter {
/*
* Arraylist used to contain the fragments
* and contain fragments title's
*/
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
//Adds the fragment and it's title. Called in MainActivity
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
//Retrieves the title of the tab
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Here is my adapter, on getItem I set the fragment object to the Fragment activity.
public class SectionPagerAdapter extends FragmentPagerAdapter {
Fragment fragment;
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0){
fragment = new FragmentOne();
}
else if (position == 1){
fragment = new FragmentTwo();
}
else if (position == 2){
fragment = new FragmentThree();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "name one";
case 1:
return "name two";
case 2:
return "name three";
}
return null;
}
}
Then in my main activity I call it like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
SectionPagerAdapter sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(sectionPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Try replacing your code with above and let me know if it helps!
USE new PagerAdapter(getChildSupportFragmentManager());instead of getSupportFragmentManager()
Try this one !!
i'm using viewpager and FragmentPagerAdapter in my application . in the first activity , this is my code :
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setScrollPosition(2,0f,true);
viewPager.setCurrentItem(2);
private void setupViewPager(ViewPager viewPager) {
Bundle bundle = new Bundle();
bundle.putString("id",id);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I'm using setCurrentItem because my application is no in english and the default selected tab should be on the right .
When I use setCurrentItem , the Fragment_contacts is not loading and it's not showing any thing , but when I remove setCurrentItem , it loads perfectly.
what is the problem ? how can I solve this ?
Your ViewPager will take some time at initialization before you could use setCurrentItem. So set it after the intialization of your ViewPager by calling it in a thread, like this:
viewPager.post(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(2);
}
});
Instead of keeping reference to your fragment in your activity, try to do the same in your ViewPager
The view pager is probably already initialized when you add your item in the list and there is a kind of "synchronization" issue between the two.
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I have a FragmentActivity with two Fragments in it and a sliding tab layout:
String titles[] = new String[] {"Tab One", "Tab Two"};
int numTabs = titles.length;
EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
With the FragmentPagerAdapter:
private class EventAdapter extends FragmentPagerAdapter {
private List<String> titles;
private int numTabs;
public void addTab (String title) {
this.titles.add(title);
this.numTabs++;
this.notifyDataSetChanged();
}
public EventAdapter(FragmentManager fm, List<String> mTitles, int mNumTabs) {
super(fm);
this.titles = mTitles;
this.numTabs = mNumTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 2:
return new FragThree();
case 1:
return new FragTwo();
default:
return new FragOne();
}
}
#Override
public String getPageTitle(int position) {
return titles.get(position);
}
#Override
public int getCount() {
return numTabs;
}
}
Some time down the line, I would like to dynamically add the third tab to this layout. Is there a way to do this in code? As you can see, I already have the PagerAdapter set up to catch the third tab. I just need to load it in...
You need to add this method to your adapter EventAdapter :
public void addTab (String title) {
this.titles.add(title);
// this is the variable returned from getCount method
this.numbTabs++;
this.notifyDataSetChanged();
}
Main purpose is to add the tab and call notifydatasetchanged.
Now call addTab on this adapter's object and tab will be added.
after addTab do sliding_tab.setViewPager(this.pager);
Please accept if this solves the purpose.
I'm using FragmentPagerAdapter to load 4 fragments initially. Now i have another fragment which i want to show only when a click is performed (let say from a button inside other fragments) , and every time that fragment is bring to front, new data of should be in it. So now what should be my logic to do this?
My FragmentPagerAdapater
public class SmallViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> allFragments;
public SmallViewPagerAdapter(FragmentManager fm,
ArrayList<Fragment> allFragments) {
super(fm);
this.allFragments = allFragments;
}
#Override
public Fragment getItem(int position) {
return allFragments.get(position);
}
#Override
public int getCount() {
return allFragments.size();
} }
Defining my adapter and adding it to viewpager
private ArrayList<Fragment> frags = new ArrayList<Fragment>();
private SmallViewPagerAdapter vpAdapter;
viewPager.setPagingEnabled(false);
viewPager.setOffscreenPageLimit(3);
frags.add(new MapContainerFragment(this));
frags.add(new FavoriteContainerFragment(this));
frags.add(new AllOffersFragment(this));
frags.add(new RegisterFragment(this));
vpAdapter = new SmallViewPagerAdapter(getSupportFragmentManager(),
frags);
viewPager.setAdapter(vpAdapter);
Now for some reason i have to put setOffscreenPageLimit(3) because i dont want my other fragments to reload when ever they brought back.
Thanks.
you should change this method
#Override
public int getCount() {
return numberOfFragmentsToShow;
}
numberOfFragmentsToShow is an int variable you change when you want to show more/less fragments
keep a reference to an instance of SmallViewPagerAdapter and when you are chaining the number of fragments, make sure you call smallViewPagerAdapterInstance.notifyDataSetChanged(); for the changes to take effect.
Alternately you could be adding/removing items from the arraylist of fragments, but you must call smallViewPagerAdapterInstance.notifyDataSetChanged(); to see the changes.
Well the solution i have found for this is to add my 5 fragments on initialization of viewpager
private ArrayList<Fragment> frags = new ArrayList<Fragment>();
private SmallViewPagerAdapter vpAdapter;
viewPager.setPagingEnabled(false);
viewPager.setOffscreenPageLimit(4);
frags.add(new MapContainerFragment(this));
frags.add(new FavoriteContainerFragment(this));
frags.add(new AllOffersFragment(this));
frags.add(new RegisterFragment(this));
frags.add(new DetailFragment(this));
vpAdapter = new SmallViewPagerAdapter(getSupportFragmentManager(),
frags);
viewPager.setAdapter(vpAdapter);
and created a static function for updating the view on every click
public static void updateView(Context context, String offerId) {
offer = new DBHandler(context).getOffersById(offerId);
if (offer != null) {
offerName.setText(offer.getTitle());
offerSave.setText(offer.getListTitle());
ImageLoader.getInstance().displayImage(
Constants.URLHelper.getUrlForOfferImage(offer.getId(),
offer.getLogoId()), offerImage);
detailText.setText(Html.fromHtml(offer.getDetailDesc().replace(
"COLOR=\"#0B333C\"", " ")));
} else {
Toast.makeText(context, "No Offer detail found!!",
Toast.LENGTH_LONG).show();
}
}
and on the button click i just change the current selected page of the viewpager.
NOTE: I know its a bad approach but that's all I can come up for the solution to my problem. If someone have a better idea or thinks that this approach isn't better, please tell me.
Add or remove fragment in viewpager dynamically:
Call setupViewPager(viewPager) on activity start.
To load a different fragment, call setupViewPagerCustom(viewPager):
private void setupViewPager(ViewPager viewPager)
{
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new fragmnet1(), "HOME");
adapter.addFrag(new fragmnet2(), "SERVICES");
viewPager.setAdapter(adapter);
}
private void setupViewPagerCustom(ViewPager viewPager)
{
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new fragmnet3(), "Contact us");
adapter.addFrag(new fragmnet4(), "ABOUT US");
viewPager.setAdapter(adapter);
}
//Viewpageradapter, handles the views
static class ViewPagerAdapter extends FragmentStatePagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
public void addFrag(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position){
return mFragmentTitleList.get(position);
}
}