我有一个 xml 布局,它具有以下 View Scrollview->RelativeLayout->Some Views + Tablayout + ViewPager->Recylerview(In Fragment of ViewPager)。 ViewPager 有一些固定的高度(保持它 "Wrap_Content" 根本不显示它)。现在的问题是 Recylerview 从不滚动。我已经尝试了几种已经发布的解决方案,例如在 “嵌套 ScrollView ” 中包装 viewpager 或什至制作 LinearLayout 的子项。但没有任何效果。
下面是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_startdraw"
android:fillViewport="true"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/_5sdp">
<TextView
android:id="@+id/invites_accepted"
style="@style/bodyStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/committee_slots"
android:text="@string/invites_accepted" />
<!-- A lot of other Textfields for data display-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pendingcommittee"
style="@style/TabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/draw_mode_val"
app:tabMode="fixed"></android.support.design.widget.TabLayout>b
<com.apponative.committeeapp.ui.custom.CustomViewPager
android:id="@+id/pending_member_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_below="@+id/tabs_pendingcommittee"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Some other views on bottom-->
</RelativeLayout>
Viewpager 显示的 fragment 具有以下布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
style="@style/bodyStyleBold1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No People..." />
<android.support.v7.widget.RecyclerView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:background="@color/white"></android.support.v7.widget.RecyclerView>
</FrameLayout>
我已经尝试在 Recyclerview 上使用 nestedScrollingEnabled 和 setFixedHeight 属性。但是没有任何效果。
最佳答案
我刚刚发现问题不在于 ViewPager 或 ScrollView 中的 RecyclerView,而是在于 ViewPager 在 ScrollView 中。当我将 ViewPager 放入 ScrollView 时,它的 Wrap_Content 属性不起作用,因此固定高度限制 RecyclerView 内容完全显示。所以我通过自定义 ViewPager 的 onMeasure 方法解决了这个问题并且它运行顺利。无需包装在 NestedScroll View 或其他给定的解决方案中。
下面是我的 CustomView Pager 的代码,它在作为子项添加到 ScrollView 时包装内容:
public class CustomViewPager extends ViewPager {
boolean canSwipe = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(getCurrentItem());
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void canSwipe(boolean canSwipe) {
this.canSwipe = canSwipe;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
关于android - ScrollView 内的 ViewPager 内的 Recyclerview 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45440171/