Fragment showing the wrong arguments - android

I have an Activity that is using viewpager and fragments with tabs. I pass data from the activity to the Fragments but it is always showing the wrong data for the specific tabs. I have one fragment which is supposed to query data depending on the arguments passed through the bundle.
private Fragment createFragment(String title){
HomeFragment fragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putString("category",title);
fragment.setArguments(bundle);
return fragment;
}
Then I pass this function on the viewpager like so:
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(createFragment("Item 1"),"Tab 1");
adapter.addFragment(createFragment("Item 2"),"Tab 2");
adapter.addFragment(createFragment("Item 3"),"Tab 3");
adapter.addFragment(createFragment("Item 4"),"Tab 4");
adapter.addFragment(createFragment("Item 5"),"Tab 5");
Lastly I get the argument passed like so:
String argument = this.getArguments().getString("category");
the problem is when I'm on Tab 1, It returns Item 2. But when I visit all tabs, it starts to return the correct item i.e. Item 1 when on Tab 1.
Where is the problem?
EDIT:
the ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragList = new ArrayList<>();
private final List<String> titleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragList.get(position);;
}
public void addFragment(Fragment fragment, String title) {
fragList.add(fragment);
titleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
#Override
public int getCount() {
return fragList.size();
}
}

Maybe you could create the adapter class and make sure you're passing the right fragment on the right position. It might be a problem with the adapter adding the fragments on the wrong position the way you are instantiating them.
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 1: return createFragment("Item 1"),"Tab 1");
case 2: return createFragment("Item 2"),"Tab 2");
case 3: return createFragment("Item 3"),"Tab 3");
case 4: return createFragment("Item 4"),"Tab 4");
case 5: return createFragment("Item 5"),"Tab 5");
}
}
#Override
public int getCount() {
return NUM_PAGES;
}
}

Related

viewpager is not showing any fragment

I have created two fragments in my app. I want to use these fragments in viewpager. This is my CustomAdapter for Viewpager:
public class CustomSwipeAdapter extends FragmentStatePagerAdapter {
public CustomSwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new fragmentComment();
case 1:
return new fragment_Rating();
default:
return new fragmentComment();
}
}
#Override
public int getCount() {
return 2;
}
}
And this is my Viewpager:
pager=(ViewPager)findViewById(R.id.view_pager);
FragmentActivity fm=new FragmentActivity();
CustomSwipeAdapter adapter=new CustomSwipeAdapter(fm.getSupportFragmentManager());
pager.setAdapter(adapter);
But when i run my app,nothing is shown in viewpager.
Can Anyone help?
You forgot
adapter.addFragment(new fragmentComment(), "FragmentComment");
adapter.addFragment(new fragment_Rating(), "FragmentRating");

Android - ViewPager third tab has not shown

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 !!

One PagerAdapter for multiple fragments

I have a basic PagerAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public PagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Main tab1 = new Main();
return tab1;
case 1:
Secondary tab2 = new Secondary();
return tab2;
case 2:
Third tab3 = new Third();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}
It works perfectly for one tablayout and 3 fragments which are displayed in the viewpager. I have multiple of the same fragment design (same tabs) across a few fragments. Is it possible to use this one PagerAdapter with multiple options (fragments), so all it has to change is the classes that are instantiated.
Main tab1 = new Main(); to SomeOtherClass tab1 = new SomeOtherClass();. I tried doing this with parameters and variables but no success.
In the public Fragment getItem(int position); method, you should return the fragment in the mFragmentListfor the given position. Something like this:
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
By this way you can use this adapter for any Fragment.
Also, the method getCount() should be as follow:
#Override
public int getCount() {
return mFragmentList!=null ? mFragmentList.size() : 0;
}
In order to avoid hardcoded values.

opening a layout (xml) inside a fragment instead of string text input

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/

Android FragmentPagerAdapter with same fragment class

I'm making a score keeping app using tabs, tablayout, fragments, and a FragmentPagerAdapter. The app runs fine, but I can't figure out how I can access the fragments. After doing some work in the fragment, I want to access the fragment to update its score. How can I access the current fragment?
Edit - my main problem is finding the position. I can use tabLayout.getSelectedTabPosition() to get the correct position if I clicked on the tab, but if I swipe between tabs, viewpager handles it differently and I don't know how to find the position from that.
public class PagerAdapter extends FragmentPagerAdapter {
private int numTabs;
public PagerAdapter(FragmentManager fm, int tabs) {
super(fm);
numTabs = tabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return makeScoreFrag(1, "Team 1");
case 1:
return makeScoreFrag(2, "Team 2");
case 2:
return makeScoreFrag(3, "Team 3");
case 3:
return makeScoreFrag(4, "Team 4");
default:
return null;
}
}
#Override
public int getCount() {
return numTabs;
}
private ScoreFragment makeScoreFrag(int num, String title){
Bundle bundle = new Bundle();
bundle.putInt("score", 0);
bundle.putInt("number", num);
bundle.putString("title", title);
ScoreFragment frag = new ScoreFragment();
frag.setArguments(bundle);
return frag;
}
}
You can Override instantiateItem method in FragmentPagerAdapter
ScoreFragment[] fragments = new ScoreFragment[numTabs];
#Override
public Object instantiateItem(ViewGroup container, final int position) {
fragments[position] = (ScoreFragment) super.instantiateItem(container, position);
return fragments[position];
}
Then current fragment is:
ScoreFragment curFragment = fragments[curTab];

Categories

Resources