viewpager is not showing any fragment - android

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");

Related

Fragment showing the wrong arguments

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;
}
}

tab fragment not calling OnCreateView switching from tab1 to tab0?

There is three tab fragment in my app when tab at index 0 onCreate called when app launch, tab ta index 1 onCreateView method not called. I need to update UI in tab 1 onCreate method. How do i resolve this kind of issue.
code:
private void setupViewPager(ViewPager viewPager) {
adapter.addFragment(new Users());
adapter.addFragment(new FavouriteUsers());
adapter.addFragment(new Recent());
viewPager.setAdapter(adapter);
}
page Adapter code:
public class UserPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public UserPagerAdapter(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) {
mFragmentList.add(fragment);
}
}
Try to Use getChildFragmentManager() to initilize your UserPagerAdapter
UserPagerAdapter adapter = new UserPagerAdapter(getChildFragmentManager())
if this doesn't work try changing FragmentStatePagerAdapter to FragmentPagerAdapter.
implement your getItem(int position) like this.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Users tab1 = new Users();
return tab1;
case 1:
FavouriteUsers tab2 = new FavouriteUsers();
return tab2;
case 2:
Recent tab3 = new Recent();
return tab3;
default:
return null;
}
}
and in your tab activity call adapter like this
final UserPagerAdapter adapter = new UserPagerAdapter
(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

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

java.lang.IllegalStateException: FragmentManager is already executing transactions

I am getting this error .Although I have gone through various link but nothing helped.
Already using getChildFragmentManager() as mentioned in various links.
This is the code:
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(this);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(2);
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0: return new Fragment1();
case 1: return new UserFragment();
case 2: return new Fragment2();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}

Preference fragment tab adapter

I want to build a preference tab for my app but since I have other tabs extending from a regular fragment, I have an incompatible types error, as a PreferenceFragment cannot be converted to Fragment.
Here is my Tab Fragment Adapter :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Discover tab1 = new Tab1Discover();
return tab1;
case 1:
Tab2Planning tab2 = new Tab2Planning();
return tab2;
case 2:
Tab3Favorites tab3 = new Tab3Favorites();
return tab3;
case 3:
Tab4Messages tab4 = new Tab4Messages();
return tab4;
case 4 :
/*Tab5Profile tab5 = new Tab5Profile();
return tab5;*/
return new PreferenceFragment() {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
};
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
There are incompatible types, the problem is - your fragments inherited from support.v4.app.Fragment, but PreferenceFragment inherited from android.app.Fragment.
There are two ways you could deal with it:
Inherit all your fragments from android.app.Fragment
Reimplement PreferenceFragment by taken it source code from here for example and inherit it from support.v4.app.Fragment

Categories

Resources