0

I'm having a strange issue using TabLayout with ViewPager. When I swipe tabs, only a fraction of a text is shown for a short period of time. If I select tabs there is no such problem. What could be wrong?

issue

Edit: I have overriden the onMeasure method of my ViewPager:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if(null != getAdapter()) {

        int height = 0;
        View child = ((FragmentPagerAdapter) getAdapter()).getItem(getCurrentItem()).getView();
        if (child != null) {
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = child.getMeasuredHeight();

            if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.JELLY_BEAN && height < getMinimumHeight()) {
                height = getMinimumHeight();
            }
        }

        getLayoutParams().height = height;
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

This sets height on every ViewPager fragment dynamically. Maybe this has something to do with the problem?

B. Hurray
  • 382
  • 4
  • 16
  • are you doing something in `setOnUserVisibleHint`? – Veneet Reddy May 08 '17 at 11:19
  • @VeneetReddy no. – B. Hurray May 08 '17 at 11:20
  • Stop right here. Why would you re-calculate the height of the page in "onMeasure"? Are you aware of the performance consequences? This looks wrong big time... – Mike May 08 '17 at 12:09
  • @Mike How to set dynamic height on viewpager then? If the text in the tab is very long it gets cut and I can't scroll. I was following [this](https://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content/) question. – B. Hurray May 08 '17 at 12:14
  • Have the fragment match_parent and the TextView's height inside your fragment's layout wrap_content. That's it. – Mike May 08 '17 at 12:15
  • @Mike I already tried: https://stackoverflow.com/questions/43834846/nestedscrollview-not-scrolling-fully-in-coordinatorlayout – B. Hurray May 08 '17 at 12:18
  • Do you need the CoordinatorLayout? Because based on the current designs, it doesn't seem so. If the problem is specifically with that, remove it. Other than that, you should follow the official docs rather than wrong StackOverflow suggestions. Just my 2 cents :) https://developer.android.com/training/implementing-navigation/lateral.html – Mike May 08 '17 at 12:22
  • @Mike Yes, I have collapsible toolbar in it. Thanks, I'll try to follow that. – B. Hurray May 08 '17 at 12:28
  • 1
    @Mike your provided docs says nothing about ViewPager's dynamic height. So why would he follow that if it has nothing to do with the needed feature? – Godfryd May 08 '17 at 12:52

1 Answers1

0

Please set your off Screen Page limit to number of tab you have in your fragment.

Example :
In OnCreate method :

 final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager_dairy);
       viewPager.setAdapter(new TabsDairyAdapter(getSupportFragmentManager()));

       viewPager.setOffscreenPageLimit(5 );  //  5 is number of tab you have

worked for me :)

Amit Sharma
  • 645
  • 5
  • 13