我有一个带有viewpager的tablayout和一个recyclelerview里面...我已经覆盖了onMeasure方法......我第一次打开片段时,viewpager里面的片段没有显示,但如果我更改了它,它会出现如果我回到没有显示的标签,它会开始显示......有人可以帮助我!!!
我认为我看到如果屏幕上有适合屏幕的,则可以在屏幕上看到,如果我需要滚动屏幕以查看它开始隐藏的viewpager,直到我转到另一个标签并再次返回查看内容。 / p>
我在viewpager中有不同大小的内容,这就是为什么我需要使用自定义视图寻呼机并覆盖onMeasure ......
以下源代码......
THE PIEW 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();
}
}
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));
}
}
}
布局
<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>
我真的不知道为什么会这样......请帮助我!
这里有一些类似的例子......
Set Viewpager height inside Scrollview in android
答案 0 :(得分:1)
抱歉,我无法完全理解您的用例,但我认为我可以提供帮助。
您是否正在尝试从活动中获得任何排序尺寸测量?
如果是,那么很遗憾,直到onPause()
,活动和片段才会完全生成但你可以尝试这样做 -
在您的活动中创建父布局的变量;
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 */
}
});
希望它有助于:)