My Activity Layout like this,
Activity contains ViewPager1 that contains two fragments (Fragment1 and Fragment2) , Fragment1 contains ViewPager2 ,ViewPager2 contains 3 Fragments
the problems is these 3 Fragments Layout not shown even they exist on ViewPager.
I'm sure they added to ViewPager Adapter because when I debug it I can see them on ViewPager's Adapter
public class ViewPager1Adapter extends FragmentPagerAdapter {
private Context context;
private ArrayList<Fragment> fragments;
private String[] titles;
public ViewPager1Adapter(Context context, FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.context = context;
this.fragments = fragments;
titles = new String[2];
titles[0] = context.getString(R.string.fragment1);
titles[1] = context.getString(R.string.fragment2);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
ViewPager2 Adapter is same like ViewPager1
fragments.add(fragment21);
fragments.add(fragment22);
fragments.add(fragment23);
viewPager2Adapter = new ViewPager2Adapter(getActivity(),
getActivity().getSupportFragmentManager(), fragments);
vp2.setAdapter(viewPager2Adapter);
the problem is Fragment21 , Fragment22 and Fragment23 Layout is not shown?
You have to update below line:
ViewPager1Adapter viewPager1Adapter = new viewPager1Adapter(getActivity(), getActivity().getSupportFragmentManager(), fragments);
Reason: You are calling wrong ViewPagerAdapter.
Also you should update your constructor of adapter, because you are having 2 string for title so you should add third string for third fragment.
public ViewPager1Adapter(Context context, FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.context = context;
this.fragments = fragments;
titles = new String[3];
titles[0] = context.getString(R.string.fragment1);
titles[1] = context.getString(R.string.fragment2);
titles[2] = context.getString(R.string.fragment3);
}
Hope this will help you.
Related
I have viewpager that has 4 fragments.
I have set the offscreenPageLimit to 4 and did not override getItemPosition of the adapter because I don't want them to be re-created every time when I change tabs.
Now, there will be a scenario when I want a particular fragment to be re-created (it means to call onCreateView).
That is before I select or when I select that fragment again.
How to do this?
Please try this adapter. This recover to re-create fragment again.
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private Fragment[] fragments;
private String[] titleList;
private int index;
public ViewPagerAdapter(FragmentManager fm, String[] titleList){ // titleList is list of title of fragments
super(fm);
fragments = new Fragment[titleList.length];
this.titleList = titleList;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
if (fragments[position] == null){
YourFragment yourFragment = new YourFragment();
fragment = yourFragment;
fragments[position] = fragment;
return fragment;
}else{
return fragments[position];
}
}
#Override
public int getCount() {
return titleList.length;
}
#Override
public CharSequence getPageTitle(int position) {
return titleList[position].toUpperCase();
}
}
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.
In my mainactivity , I have a gridview, when I am selecting an image of the grid, I am displaying the selected image on another activity.
in the another activity I have Listfragment so I can use view pager , its working fine for List < fragment> but I am getting from the other activity Listitem which is parcelable
I am recveing from mainactivity-gridview listiem which is parceable
ArrayList<Listitem> personArrayList = Bundle.getParcelableArraylist("person_List);
However in the another activity I am using list fragment
List<Fragment> flist = new ArrayList<Fragment>();
My question how to add the parcable data into the list fragment so i can use the viewpager ?
public void onCreate(Bundle SavedInstanceState);
{
super.onCreate(SavedInstanceState);
setContentView(R.layout.activity);
List<Fragment> fragments=getFragments();
Bundle bundle =getIntent().getextras();
//I want the below arraylist to be added to fragment.. how to do that?
ArrayList<Listitem> personArrayList = Bundle.getParcelableArraylist("person_List");
for (listitem person : personArrayList ){
Log.i("Activity",String.valueof(person.url));
}
pageAdapter = new PageAdapter (getSupportFragmentManager(), fragments);
ViewPager pager= (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageadapter);
}
// here I am adding the content for the fragments, how to add instead the content of the parcalble ?
private List<Fragment> getFragments(){
List<Fragment> flist = new ArrayList<Fragment>();
flist = new ArrayList<Fragment>();
flist.add(MyFragment.newInstance("frag1");
flist.add(MyFragment.newInstance("frag2");
flist.add(MyFragment.newInstance("frag3");
return flist;
}
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
In my project I have a tab layout and a fragments viewpager. When I load the fragments and tabs the tab index and the displayed fragment is out of sync, and also when I swipe back and forth the fragments are not in sync with the tabs. I checked the getItem(int position) and the getTitle(int position) methods both have the same position number and the corresponding entries in the ArrayList<Fragments> and the ArrayList<String> (for tab headings) are correct but some how the tabs display is out of sync with the view pager.
Here is my fragmentPagerAdapter.java
public class DetailsFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
private ArrayList<String> titles;
public DetailsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<>();
titles = new ArrayList<>();
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return titles.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
public void setFragment(Fragment fragment, String title) {
fragments.add(fragment);
titles.add(title);
notifyDataSetChanged();
}
}
this is activity on create.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
tabLayout = (TabLayout) findViewById(R.id.detailsTabs);
viewPager = (ViewPager) findViewById(R.id.viewPager);
new GetCategoriesAsync2().execute();
}
the async task's doInBackground is calling is preparing arraylist
and the onPostExecute
#Override
protected void onPostExecute(Void aVoid) {
int size = lists.size();
ArrayList<Fragment> fragments = new ArrayList<>(size);
ArrayList<String> titles = new ArrayList<>(size);
for (int i = 0; i < lists.size(); i++) {
ArrayList<String> x = lists.get(i);
titles.add(x.remove(0));
fragments.add(DetailsListFragment.newInstance(x));
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentPagerAdapter = new DetailsFragmentPagerAdapter(fragmentManager);
fragmentPagerAdapter.setFragments(fragments,titles);
viewPager.setAdapter(fragmentPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
progressDialog.dismiss();
}
I am using design support library 23.0.1 and the build target is 23.
how can I fix this tab layout out of sync?
if you use a TabLayout, you need to connect it to the adapter
If you're using a ViewPager together with this layout, you can use
setTabsFromPagerAdapter(PagerAdapter) which will populate the tabs
using the given PagerAdapter's page titles. You should also use a
TabLayout.TabLayoutOnPageChangeListener to forward the scroll and
selection changes to this layout like so:
ViewPager viewPager = ...;
TabLayout tabLayout = ...;
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
Try this, it worked for me
public class DetailsFragmentPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments= new ArrayList<Fragment>();
private final List<String> titles= new ArrayList<String>();
public DetailsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
public void setFragment(Fragment fragment, String title) {
fragments.add(fragment);
titles.add(title);
}
}
And Change
fragmentPagerAdapter.setFragments(fragments,titles); To
for (int i=0;i<fragments.size();i++)
{
adapter.setFragments(fragments.get(i),titles.get(i));
}
Check this out
it's a demo app that uses a TabLayout with a ViewPager
https://github.com/chrisbanes/cheesesquare
and if you don't know the number of the fragments that you will have in the ViewPager
your pageadapter should extends FragmentStatePagerAdapter
https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
I have an activity that extends FragmentActivity, I use a custom FragmentStatePagerAdapter that extends FragmentStatePagerAdapter, a SlidingTabLayout and a pagerView.
In some cases I want my fragmentActivity to start on a specific (third, if it matters..) tab.
I've tried lots of suggestion I found on SO but non worked. When I tries to use "pager.setCurrentItem(3)" the problem was that onCreateView for that fragment was never called and therefore all objects and values are not assigned properly.
thanks for the help!
EDIT: Adding more info
this is how I init my adapter:
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, NewsTab.class.getName()));
fragments.add(Fragment.instantiate(this, PhotosTab.class.getName()));
fragments.add(Fragment.instantiate(this, AboutTab.class.getName()));
fragments.add(Fragment.instantiate(this, NextTab.class.getName()));
adapter = new ProtestViewPagerAdapter(getSupportFragmentManager(),Titles, NumOfTabs, fragments);
this is my view pager adapter:
public class ProtestViewPagerAdapter extends FragmentStatePagerAdapter {
public final String TAG = "PRTST_V_PGR_ADPTR";
private CharSequence Titles[];
private int NumbOfTabs;
private List<Fragment> fragments;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ProtestViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabs,
List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabs;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
return fragments.get(position);
// switch (position){
// case 0:
// NewsTab news = new NewsTab();
// return news;
// case 1:
// PhotosTab photos = new PhotosTab();
// return photos;
// case 2:
// AboutTab about = new AboutTab();
// return about;
// case 3:
// NextTab next = new NextTab();
// return next;
// default:
// return null;
// }
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return this.Titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return this.NumbOfTabs;
}
}
each of the tabs implements FragmentInterface that only have
void fragmentBecameVisible();
and the "tabs" also extends "MyBaseTab" that extends android.support.v4.app.Fragment