[英]How can I make work ViewPager with RecyclerView inside NestedScrollView
I am using NestedScrollView with ViewPager. 我在ViewPager中使用NestedScrollView。 NestedScrollView has a LinearLayout inside with some TextViews, TabLayout and ViewPager at the end.
NestedScrollView里面有一个LinearLayout,最后有一些TextViews,TabLayout和ViewPager。 TextViews occupy most of the space and for ViewPager left a little space.
TextViews占据了大部分空间,并为ViewPager留下了一点空间。 ViewPager using two fragments, in one of them there is a few TextViews and ImageViews and in another fragment there is a RecyclerView.
ViewPager使用两个片段,其中一个有几个TextViews和ImageViews,另一个片段中有一个RecyclerView。
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的高度设置为
WRAP_CONTENT
它只占用空间,我无法滚动查看第一个片段的其余部分,以及第二个片段scoll里面的小ViewPager。
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. 例如,当我将ViewPager的高度设置为1000dp时,我可以向下滚动第一个片段,但第二个片段仍在小型ViewPager中滚动。 And after I scroll in fragment with RecyclerView scoll in the first Fragment not working anymore.
在我用第一个Fragment中的RecyclerView scoll滚动片段后不再工作了。
How can I fix scrolling problem and make ViewPager work with WRAP_CONTENT
? 如何修复滚动问题并使ViewPager与
WRAP_CONTENT
一起WRAP_CONTENT
?
Here is my code: 这是我的代码:
<?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>
I think you should override ViewPager like this: 我认为你应该像这样覆盖ViewPager:
@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);
}
maybe you can try : 也许你可以尝试:
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview);
scrollView.setFillViewport (true);
such as: https://stackoverflow.com/a/33385207/4035627 例如: https : //stackoverflow.com/a/33385207/4035627
@StevenZhang's solution almost worked for me but there was a problem when two tabs have different heights, the smaller tab takes the height of the bigger tab. @ StevenZhang的解决方案几乎对我有用,但是当两个标签具有不同的高度时出现问题,较小的标签采用较大标签的高度。
Based on this answer about how to make a dynamic height ViewPager
I made a solution in Kotlin 在此基础上回答关于如何使一个动态的高度
ViewPager
我取得了科特林解决方案
Custom View Pager 自定义视图寻呼机
class CustomViewPager: ViewPager {
constructor(context: Context) : super(context)
constructor(context : Context, attrs : AttributeSet) : super(context, attrs)
private var currentView : View? = null
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
currentView?.let {
it.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
val h = it.measuredHeight
val height = if (h > 0) h else 0
val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
return
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
fun measureCurrentView(currentView: View?) {
this.currentView = currentView
requestLayout()
}
}
In your custom PagerAdapter
override setPrimaryItem
在您的自定义
PagerAdapter
覆盖setPrimaryItem
private var currentPosition = -1
override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) {
super.setPrimaryItem(container, position, `object`)
if (position != currentPosition) {
val fragment = `object` as Fragment
val pager = container as CustomViewPager
if (fragment.view != null) {
currentPosition = position
pager.measureCurrentView(fragment.view)
}
}
}
You may also need to set isNestedScrollingEnabled
of your RecyclerView
to false
您可能还需要将
RecyclerView
isNestedScrollingEnabled
设置为false
recyclerView.isNestedScrollingEnabled = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.