Android-ViewPager选项卡未第一次显示
我有一个带有viewpager和recyclerview的表格布局在里面。。。我已经覆盖了onMeasure方法。。。第一次打开片段时,viewpager中的片段不会显示,但如果我更改选项卡,它会出现,如果我返回到未显示的选项卡,它会开始显示。。。有人能帮我吗 我想我看到的是viewpager在屏幕上可见,如果它适合屏幕,如果我需要滚动屏幕以查看viewpager,它将开始隐藏,直到我转到另一个选项卡并再次返回以查看内容 我有不同大小的内容内的viewpager,这就是为什么我需要使用一个自定义的查看页面和覆盖的onMeasure 下面的源代码 查看寻呼机Android-ViewPager选项卡未第一次显示,android,android-viewpager,android-tablayout,Android,Android Viewpager,Android Tablayout,我有一个带有viewpager和recyclerview的表格布局在里面。。。我已经覆盖了onMeasure方法。。。第一次打开片段时,viewpager中的片段不会显示,但如果我更改选项卡,它会出现,如果我返回到未显示的选项卡,它会开始显示。。。有人能帮我吗 我想我看到的是viewpager在屏幕上可见,如果它适合屏幕,如果我需要滚动屏幕以查看viewpager,它将开始隐藏,直到我转到另一个选项卡并再次返回以查看内容 我有不同大小的内容内的viewpager,这就是为什么我需要使用一个自定义
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();
}
}
滚动视图
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>
很抱歉,我不能完全理解您的用例,但我想我可以帮您
您是否试图从活动中获取任何类型的尺寸测量值
如果是,那么不幸的是活动和片段直到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 */
}
});
希望有帮助:)为什么您需要自定义的ViewPager
,而不是简单地使用默认的ViewPager
?因为如果我使用默认的ViewPager,我需要设置height=“xdp”来查看ViewPager。。有了自定义的viewpager,它显示了。。。我编辑了我的Anwser为什么不wrap\u content
或match\u parent
?我不明白为什么我的wrap\u content没有得到大小…:(我会在第一次通过时检查结果,不显示任何内容…并添加我的问题。)