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