Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top