【发布时间】:2017-05-16 18:17:03
【问题描述】:
我有一个带有 viewpager 和 recyclerview 的 tablayout...我已经覆盖了 onMeasure 方法...第一次打开片段时,viewpager 中的片段不显示,但是如果我更改选项卡,它会出现并且如果我回到不显示的选项卡,它会开始显示...请有人帮助我!!!
我想我看到的是如果 viewpager 适合屏幕,它会在屏幕上可见,如果我需要滚动屏幕以查看 viewpager,它开始隐藏,直到我转到另一个选项卡并再次返回查看内容。
我在 viewpager 中有不同大小的内容,这就是为什么我需要使用自定义 view pager 并覆盖 onMeasure...
源码如下...
查看寻呼机
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>
我真的不知道为什么会这样……请帮帮我!
这里有一些类似的例子...
Set Viewpager height inside Scrollview in android
【问题讨论】:
-
为什么需要自定义
ViewPager而不是简单地使用默认ViewPager? -
因为如果我使用默认的 viewpager,我需要设置 height="xdp" 才能看到 viewpager。使用自定义的 viewpager 它正在显示......我已经编辑了我的 anwser跨度>
-
为什么不
wrap_content或match_parent? -
我不明白为什么我 wrap_content 没有得到大小... :(
-
我会在第一次通过时检查结果以不显示任何内容...并添加我的问题
标签: android android-viewpager android-tablayout