-1

I have a tablayout with viewpager and a recyclerview inside... I've override the onMeasure method... The first time I open the fragment, the fragment inside the viewpager not show, but if i change tab it appear and if i go back to the tab that doesn't show, it begin to show... Can some one help me please!!!

I Think i saw is the viewpager get visible on screen if it fit on the screen, if i need to scroll the screen to see the viewpager it begin hided until i go to another tab and back again to see the content.

I got diferent sizes of contents inside the viewpager, that's why i need to use a custom view pager and override the onMeasure...

The source code below...

THE VIEW PAGER

public class CustomPager extends ViewPager {

    private int mCurrentPagePosition = 0;

    public CustomPager(Context context) {
        super(context);
    }

    public CustomPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            View child = getChildAt(mCurrentPagePosition);
            if (child != null) {
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void reMeasureCurrentPage(int position) {
        mCurrentPagePosition = position;
        requestLayout();
    }
}

THE SCROLLVIEW

public class CustomScrollView extends ScrollView {

    private GestureDetector mGestureDetector;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev)
                && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            return (Math.abs(distanceY) > Math.abs(distanceX));
        }
    }
}

THE LAYOUT

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="br.com.ole.oleconsignado.ui.fragment.main.LastEntriesFragment">

    <View
        android:id="@+id/vw_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/dimen_mdpi"
        android:layout_marginLeft="@dimen/dimen_mdpi"
        android:layout_marginRight="@dimen/dimen_mdpi"
        android:layout_marginEnd="@dimen/dimen_mdpi"
        android:layout_marginStart="@dimen/dimen_mdpi"
        android:background="@color/colorGrey"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@color/colorLightBlue"
        app:tabSelectedTextColor="@color/colorLightBlue"
        android:layout_marginLeft="@dimen/dimen_mdpi"
        android:layout_marginRight="@dimen/dimen_mdpi">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_national"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_national" />

        <android.support.design.widget.TabItem
            android:id="@+id/tab_international"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_international" />

    </android.support.design.widget.TabLayout>

    <br.com.ole.oleconsignado.util.CustomScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <br.com.ole.oleconsignado.util.CustomPager
            android:id="@+id/vp_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_mdpi" />

    </br.com.ole.oleconsignado.util.CustomScrollView>
</LinearLayout>

I realy don't know why it happen... Please, help me!

Here some examples similar...

Set Viewpager height inside Scrollview in android

getchildat() method returns null for last position (3rd Fragment in my case) Fragment for WarpContent ViewPager Android

ViewPager wrap_content not working

Android - ViewPager tab not showing first time

Community
  • 1
  • 1
Felipe A.
  • 929
  • 3
  • 12
  • 28
  • Why do you need a custom `ViewPager` and don't simply use the default `ViewPager` ? – Shark May 16 '17 at 19:38
  • Because if i use the default viewpager, i'm needing to set height="xdp" to see the viewpager.. With that custom viewpager it's showing... I've edited my anwser – Felipe A. May 16 '17 at 19:40
  • why not `wrap_content` or `match_parent` ? – Shark May 16 '17 at 19:45
  • I didn't understand why i wrap_content not getting the size... :( – Felipe A. May 16 '17 at 19:46
  • one of the reasons would be because you overridden the `onMeasure()` method. – Shark May 16 '17 at 19:47
  • this line `int h = child.getMeasuredHeight();` is probably returning 0, and this line `heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);` followed by this line `super.onMeasure(widthMeasureSpec, heightMeasureSpec);` tells it you want it to be exactly 0 high. But then again, this is just me supposing - the debugger will tell you exactly what's going wrong and where. – Shark May 16 '17 at 19:49
  • 1
    I'll check the results when pass the first time to dont show nothing... and add on my question – Felipe A. May 16 '17 at 19:53
  • The first time is not finding child... it comming null that's the reason its not making the height dynamic... and when i change the tab, the child return something... – Felipe A. May 16 '17 at 19:56
  • and whats the value of `heightMeasureSpec` when the child is null, that is being passed into `super.onMeasure(...);` ? – Shark May 16 '17 at 19:57
  • Yes... the super.onMeasure(...) – Felipe A. May 16 '17 at 19:58
  • I need to know why the getChildAt(int) not finding my view... – Felipe A. May 16 '17 at 19:58
  • The `ViewPager` is instantiated, then it measures itself, then it instantiates it's children with the correct measure specs so they can measure themselves. That'd be my first guess, so the first time `ViewPager::onMeasure()` is called, perhaps it doesn't have any children yet? – Shark May 16 '17 at 20:00
  • You can always look at the ViewPager's sourcecode to find out the real reason though. – Shark May 16 '17 at 20:01
  • I found the error... inside my fragment that i inflate on view pager have a recyclerview... and its setting the layout manager after pass on the getChildAt(int)... But thx bro! – Felipe A. May 16 '17 at 20:41
  • Hey man... I didn't figured how i can call my recyclerview before call onMeasure from viewpager... The viewpager only have the size of my recyclerview after i call LayoutManager from recyclerview... Can you help me please? – Felipe A. May 17 '17 at 13:35
  • Help you with what? update the question with additional info and i'll try to see whether i can see something. – Shark May 17 '17 at 14:57

1 Answers1

1

Sorry I couldn't understan your use-case completely but I think I can help.

Are You Trying to get any sort Dimensional-Measurements from the activity?

If Yes Then unfortunately the activities and fragments are not completely generated until onPause()

but you can try to do this-

Create a variable of your parent layout in your activity ;

    RelativeLayout canvas;

            canvas = (RelativeLayout) findViewById(R.id.canvas);

 canvas.getViewTreeObserver().addOnGlobalLayoutListener(new 
 ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

 canvas.getViewTreeObserver().removeOnGlobalLayoutListener(this);

     /* Code to get dimensional measurements */


        }
    });

Hope It Helps :)