我如何在NestedScrollView中使用RecyclerView进行ViewPager工作 [英] How can I make work ViewPager with RecyclerView inside NestedScrollView
问题描述
我正在将NestedScrollView与ViewPager一起使用. NestedScrollView内部具有LinearLayout,最后还有一些TextView,TabLayout和ViewPager. TextViews占据了大部分空间,而ViewPager则保留了一些空间. ViewPager使用两个片段,其中一个片段中有一些TextViews和ImageViews,而另一个片段中有一个RecyclerView.
I am using NestedScrollView with ViewPager. NestedScrollView has a LinearLayout inside with some TextViews, TabLayout and ViewPager at the end. TextViews occupy most of the space and for ViewPager left a little space. ViewPager using two fragments, in one of them there is a few TextViews and ImageViews and in another fragment there is a RecyclerView.
当我将ViewPager的高度设置为WRAP_CONTENT
时,它只剩下剩余的空间,我无法滚动查看第一个片段的其余部分,而第二个片段在小的ViewPager内弯曲.
When I set ViewPager's height to WRAP_CONTENT
it takes only space that left and I can't scroll to see the rest of the first fragment, and the second fragment scoll inside little ViewPager.
例如,当我将ViewPager的高度设置为1000dp时,我可以向下滚动第一个片段,但是第二个片段仍在小ViewPager内滚动.在我使用RecyclerView scoll滚动片段后,第一个片段不再起作用.
When I set ViewPager's height to 1000dp for example, I am able to scroll down on first fragment, but the second fragment still scrolling inside little ViewPager. And after I scroll in fragment with RecyclerView scoll in the first Fragment not working anymore.
如何解决滚动问题并使ViewPager与WRAP_CONTENT
一起使用?
How can I fix scrolling problem and make ViewPager work with WRAP_CONTENT
?
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
推荐答案
我认为您应该像这样重写ViewPager:
I think you should override ViewPager like this:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
这篇关于我如何在NestedScrollView中使用RecyclerView进行ViewPager工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!