12

I am learning to use ViewPager and PagerTabStrip to implement navigation bar. I have implemented it, my problem is: every time I open the app fresh, the titles don't show, but after I swipe it once, the titles all appear again, and then everything is normal. code shown below:

Customised Adapter

public class MyPagerAdapter extends PagerAdapter {
    private List<View> viewList;
    private List<String> titleList;

    public MyPagerAdapter(List<View> viewList, List<String> titleList){
        this.viewList = viewList;
        this.titleList = titleList;
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

.xml File:

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        />

</android.support.v4.view.ViewPager>

This is the screenshot of "Just clicked the app icon": enter image description here

And this is after I swiped to the second page: enter image description here

I'm really frustrated. Thanks!!

TPWang
  • 1,322
  • 4
  • 20
  • 39
  • you have your PagerTabStrip inside your viewpager, try placing it outside, by the way. you are passing the views as parameters in your constructor; that is not good for memory. Try using fragments, here is an example of how to do it http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html – Raykud Sep 01 '15 at 05:09
  • Nope, did not solve the problem, also I believe I am supposed to put it inside the ViewPager. – TPWang Sep 01 '15 at 05:11
  • Are you setting title on onPageSelected function? – Jignesh Jain Sep 01 '15 at 05:12
  • I don't think I have a onPageSelected function... – TPWang Sep 01 '15 at 05:13
  • Try extending your adapter as FragmentStatePagerAdapter instead of PagerAdapter, if you are using fragment. – Jignesh Jain Sep 01 '15 at 05:16
  • But I'm not using any Fragments. I wouldn't be able to implement any of the methods. – TPWang Sep 01 '15 at 05:19
  • you can always use whatever you need inside a Fragment – Raykud Sep 01 '15 at 05:26
  • Sure, but what I need now, specifically for this question is to find out why this irregularity happens. As a matter of fact, I tried with a new project, the problem persists. But the same code works fine in the tutorial I'm watching. I want to know why that happens. – TPWang Sep 01 '15 at 05:33
  • here is solution http://stackoverflow.com/questions/32379050/how-can-we-work-around-the-blank-title-in-pagertitlestrip-and-pagertabstrip – toxa_xa Oct 19 '15 at 07:44

4 Answers4

13

It is an issue appeared in com.android.support:appcompat-v7:23.0.0. You can refer here https://code.google.com/p/android/issues/detail?id=183127

In that link google support team have mention that defect would be fixed in future releases. So for now solution is build the project using com.android.support:appcompat-v7:22.2.1

Update : If feasible for you then you can go ahead with another solution provided by @nidheeshdas. I have tried on simple project; it work Modified solution of @nidheeshdas inside onResume() of Activity

viewPager.setCurrentItem(1);
    viewPager.postDelayed(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(0);
        }
    },100);

New Update: As mentioned in the above google issue tracker link and comments from JP Ventura. I have tried with new version of library and issue seems to be fixed.

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
  • 1
    Updating dependency to "com.android.support:appcompat-v7:23.1.1" and build tools version to "23.0.2" also works. – JP Ventura Dec 07 '15 at 16:22
3

Instead of using android.support.v4.view.PagerTabStrip , use android.support.design.widget.TabLayout for displaying tabs for viewPager. It is included in Google Design Support Library.

See this link for more information http://android-developers.blogspot.in/2015/05/android-design-support-library.html

Just few lines:

viewPager=(ViewPager)v.findViewById(R.id.viewPager);

ViewPagerAdapter adapter=new ViewPagerAdapter(this.getChildFragmentManager(),doctor);
adapter.setViewPagerFragmentListener(this);
viewPager.setAdapter(adapter);

tabLayout.setupWithViewPager(viewPager);  //Sync Tabs with viewPager
tabLayout.setTabsFromPagerAdapter(adapter);  //Setup tabs titles 

And to change the titles use the following code in ViewPagerAdapter

@Override
public CharSequence getPageTitle(int position) {
    switch (position){
        case 0:
            return "Title 1";
        case 1:
            return "Title 2";
        case 2:
            return "Title 3";
    }
    return super.getPageTitle(position);
}
Jibin Mathews
  • 606
  • 4
  • 13
1

I also recently started to have this problem, and after a little bit of testing I think I found a bug in Android's latest support package update.

The problem appears in to be in com.android.support:appcompat-v7:23.0.0.

Try changing the dependency back to com.android.support:appcompat-v7:22.2.1 (second latest update) and see if that works.

Unfortunately, I have yet to find any solution to get it to work with the latest support package update.

Henrik Wassdahl
  • 930
  • 1
  • 10
  • 18
  • I don't even get a response when I click on add dependency menu and library dependency, it just doesn't respond. I am going to uninstall Android Studio first and then reinstall it. – TPWang Sep 01 '15 at 13:02
1

Try this. Its seems to be working for me.

@Override
protected void onResume() {
    super.onResume();
    pager.setCurrentItem(1);
    Task.delay(500).continueWith(new Continuation<Void, Object>() {
        @Override
        public Object then(Task<Void> task) throws Exception {
            pager.setCurrentItem(0);
            return null;
        }
    }, Task.UI_THREAD_EXECUTOR);
}

onResume set the pager to 1 and then back to 0. This makes the title appear the page loads the first time.

nidheeshdas
  • 1,097
  • 1
  • 11
  • 20