1

I have decided for remaking UI of my app by using ViewPagerIndicator and TitlePageIndicator, but I have following problems:

1) Fragments that are for each page are not displayed.

2) Swipe betwen two pages does not work.

I am using ActionBarSherlock as I did with tabs.

I have not changed anything in Fragmens since it has workrd fine when I was using tabs and I have written following code:

public class CustomFragmentAdapter extends FragmentPagerAdapter {


    private final String[] TITLES = new String[] { 
            "General",
            "Companies",
            "Discounts"


    };

    public final int NUM_TITLES = TITLES.length;

    public CustomFragmentAdapter(FragmentManager fm) {      
        super(fm);

    }

    @Override
    public Fragment getItem(int position) { 

        Log.v("POSITION", "position: "+position);
        switch (position) {
            case 0:
                return new General();
            case 1:
                return new Companies();
            case 2:
                return new Discounts();
        }

        return null;
    }

    @Override
    public int getCount() {
        return NUM_TITLES;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position % NUM_TITLES].toUpperCase();
    }
}

and

mViewPager = (ViewPager)findViewById(R.id.pager);

    mViewPager.setAdapter(new CustomFragmentAdapter(getSupportFragmentManager()));

    TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);

    indicator.setViewPager(mViewPager);
    indicator.setCurrentItem(0);

and my XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"

>

<com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/indicator"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

As I have seen most of the tutorials and examples are quiet similar to my code, but I still can't make it work. I have tried almost everything. It was also not working when I have put there some dummy fragments with just some TextView. I also have tried to remove ViewPagerIndicator and try only ViewPager, but it was not working for me.

I am probably missing something, but I can't find it. Thank you very much in advance for your advices.

ziky90
  • 2,627
  • 4
  • 33
  • 47
  • 2
    This question has nothing to do with ViewPagerIndicator. This is a ViewPager problem. – Jake Wharton Sep 11 '12 at 19:27
  • Can you see getItem() being evoked at least once (when the current Fragment is being instantiated)? I mean, the place where you use Log.v – Michał Klimczak Sep 11 '12 at 19:30
  • I can see it evoked three times. After I launch the app it is called twice for position 0 and 1 and then when I tap on item Companies or Discounts it is evoked again for position 2. – ziky90 Sep 11 '12 at 19:43
  • So your adapter probably works. Maybe the way you instantiate your fragments is wrong? Tried this?: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html – Michał Klimczak Sep 11 '12 at 19:45
  • I have tried to create instance in the static way as it is described in the article but it did not helped. – ziky90 Sep 11 '12 at 20:03
  • To be sure that they get instantiated put Log.v in their onCreate. If their onCreate is evoked than ViewPager works and the Fragments are corrupt, I guess – Michał Klimczak Sep 11 '12 at 20:17
  • I have put log in onCreate method and onCreate is evoked in the same way as getItem() method. But as I have said I have tried it also with dummy Fragment where was just one TextView. Last thing that is coming on my mind is that if the combination of ActionBarSherlock and ViewPager is not the problem? I think that fragments are not wrong because it has worked before I have switched from tabs. My previous TabAdapter that was working fine can be seen in this thread: http://stackoverflow.com/questions/11976397/android-getting-fragment-that-is-in-fragmentpageradapter and I was using it differently – ziky90 Sep 11 '12 at 21:13

2 Answers2

1

One difference I see from a similar Activity of mine is that you're not setting the current item of the pager:

mViewPager.setCurrentItem(0, false);
JeffB
  • 1,887
  • 15
  • 13
0

I have found the problem which was missing android:orientation="vertical" in activity layout xml

ziky90
  • 2,627
  • 4
  • 33
  • 47