我使用NestedScrollView与ViewPenger。NestedScrollView内部有一个LinearLayout,末尾有一些TextViews、TabLayout和ViewPenger。TextViews占据了大部分空间,为ViewPenger留了一点空间。ViewPenger使用两个片段,其中一个片段中有一些TextViews和ImageViews,另一个片段中有一个RecyClaView。
当我将 ViewPager 的高度设置为 WRAP_CONTENT
时,它只占用剩下的空间,我无法滚动查看第一个片段的其余部分,以及小 ViewPager 中的第二个片段。
例如,当我将ViewPager的高度设置为1000dp时,我可以向下滚动第一个片段,但第二个片段仍然在小ViewPager中滚动。当我用recycle在片段中滚动后,查看第一个片段中的scoll不再工作。
如何修复滚动问题并使ViewPenger与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>
我认为你应该像这样覆盖ViewPenger:
@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);
}
@StevenZhang的解决方案几乎对我有效,但当两个标签具有不同的高度时,存在一个问题,较小的标签占据较大标签的高度。
基于这个关于如何生成动态高度的答案,我在Kotlin中提出了一个解决方案
自定义视图寻呼机
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()
}
}
在您的自定义Pager适配器
覆盖setPrimary项目
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)
}
}
}
您可能还需要将回收器视图
的 isNestedScrollingEnabled
设置为 false
recyclerView.isNestedScrollingEnabled = false
也许你可以试试:
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview);
scrollView.setFillViewport (true);
如:https://stack overflow . com/a/33385207/4035627