Trouble With Viewpager Displaying Fragments
I am trying to get a fragment with three tabs displayed using ViewPager. Initially I used to instantiate the fragment from the Activity using FragmentMgr, this worked fine. When I
Solution 1:
The question description is perfectly suitable to the problem I just had. I know the question is old but may this help other who face same. @Geoplex have not shown code of LaunchPageAdapter class. So I'm not sure if this solves his problem or not. But for me that worked.
In my PagerAdapter, the
public boolean isViewFromObject(View view, Object object) was overridden. I removed that method and allowed superClass to handle it. That started giving my expected result.
Solution 2:
This is useful for you,
publicclassViewPagerAdapterextendsFragmentStatePagerAdapter {
privateList<Fragment> fragments=null;
privateFragmentManager fragmentManager=null;
publicViewPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragments) {
super(fragmentManager);
this.fragments=fragments;
this.fragmentManager=fragmentManager;
}
@OverridepublicFragmentgetItem(int position) {
fragmentManager.beginTransaction().commitAllowingStateLoss();
return fragments.get(position);
}
@Overridepublic int getCount() {
return fragments.size();
}
@OverridepublicvoidsetPrimaryItem(ViewGroup container, int position, Objectobject)
{
super.setPrimaryItem(container,0,fragments.get(0));
}
@OverridepublicvoidnotifyDataSetChanged()
{
super.notifyDataSetChanged();
}
@OverridepublicvoiddestroyItem(ViewGroup collection, int position, Object view) {
fragmentManager.executePendingTransactions();
fragmentManager.saveFragmentInstanceState(fragments.get(position));
}
publicvoidreplaceItem(int position,Fragment fragment)
{
fragments.set(position, fragment);
this.notifyDataSetChanged();
}
}
MainActivity.java
publicclassMainActivityextendsFragmentActivityimplementsOnPageChangeListener,TabListener
{
private android.support.v4.view.ViewPager mViewPager=null;
private ViewPagerAdapter mPagerAdapter=null;
private ActionBar action=null;
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initilizeViewPager();
action=getActionBar();
action.setDisplayShowHomeEnabled(false);
action.setDisplayShowTitleEnabled(false);
action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
publicvoidinitilizeViewPager()
{
mViewPager=(android.support.v4.view.ViewPager)findViewById(R.id.viewPager);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(LaunchListFragment.newInstance(select));
fragments.add(AddTeamFragment.newInstance());
fragments.add(thirdFragment.newInstance());
viewPagerAdapter=new ViewPagerAdapter();
mViewPager.setAdapter(viewPagerAdapter(getSupportFragmentManager(),fragments));
new setAdapterTask().execute();
mViewPager.setOnPageChangeListener(this);
}
privateclasssetAdapterTaskextendsAsyncTask<Void,Void,Void>
{
protected Void doInBackground(Void... params)
{
returnnull;
}
@Override
protectedvoidonPostExecute(Void result)
{
mViewPager.setAdapter(mPagerAdapter);
Tab first=action.newTab().setTag("first").setText("First").setTabListener(MainActivity.this);
Tab second=action.newTab().setTag("second").setText("Second").setTabListener(MainActivity.this);
Tab third=action.newTab().setTag("third").setText("Third").setTabListener(MainActivity.this);
action.addTab(first);
action.addTab(second);
action.addTab(third);
}
}
}
publicvoidonPageScrollStateChanged(int arg0)
{}
publicvoidonPageScrolled(int arg0, float arg1, int arg2)
{}
publicvoidonPageSelected(int position)
{
getActionBar().setSelectedNavigationItem(position);
}
publicvoidonTabReselected(Tab tab, FragmentTransaction ft) {}
publicvoidonTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
publicvoidonTabUnselected(Tab tab, FragmentTransaction ft) {}
}
main_layout.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.ViewPagerandroid:id="@+id/viewpager"android:layout_width="fill_parent"android:layout_below="@android:id/tabs"android:layout_height="fill_parent"/></RelativeLayout>
Post a Comment for "Trouble With Viewpager Displaying Fragments"