我无法正确滚动ScrollView。它总是切断底部的内容,就像它是正常的LinearLayout一样。
我的代码
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="true"
android:orientation="vertical" >
当然,我已经尝试添加/删除“fillViewport”和“isScrollContainer”属性,它根本没有改变任何东西。
提前致谢。
答案:当用作XML布局的根元素时,ScrollView无法正常工作。它必须包含在LinearLayout中。
解决方案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
我终于弄明白了,我花了5个小时一步一步地建造所有东西,每次都在测试它......
无论如何,如果你有这个问题,我发现setOntouch监听器搞乱你的代码 - 至少在我的情况下它是...
我必须找到解决方法,但尝试删除你的ontouch监听器并测试你的代码 - 它必须工作
所选答案不正确!
您可以使用ScrollView作为根视图,它不适合您,因为您缺少填充。
添加如下内容:
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
删除android:isScrollContainer
中的LinearLayout
。根据文档android:isScrollContainer
用于设置视图可滚动。我希望它对你有所帮助。请参阅此link的定义。
Android Studio将NestedScrollView添加到其某些模板的活动文件中(例如Master-Detail)。在片段文件中有一个ScrollView,在该片段的活动文件中有一个ScrollView阻止了滚动视图的工作。删除我的片段文件中的ScrollView并将其保留在活动文件中解决了我的问题。
滚动视图作为父级没有问题。当我们向scrollview的直接子项添加填充/边距时,我遇到了这样的问题。保持scrollview的子项只有高度和宽度属性,它将工作正常。
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
试试这个,
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
尝试此解决方案,只需使用后延迟方法延迟滚动。
fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
</ScrollView>
</RelativeLayout>
test fragment.Java
...
private ScrollView mScrollView;
...
mScrollView = (ScrollView) mView.findViewById(R.id.scrollView);
mScrollView.setSmoothScrollingEnabled(true);
...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(isAdded()) {
// Scroll 1000px down
mScrollView.smoothScrollTo(0, 1000);
}
}
}, 150);
这对我有用,希望这会有所帮助。
这解决了我的问题:我将android:isScrollContainer="true"
添加到LinearLayout并删除了ScrollView代码之前:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollViewRecords"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/sheepList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
代码之后
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true">
<LinearLayout
android:id="@+id/sheepList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
有没有办法解决这个问题?它是旧的,目前标记为有效的答案没有意义。关于ScrollView现在唯一有效的事情是:
一个视图组,允许滚动放置在其中的视图层次结构。滚动视图可能只有一个直接子项放在其中。要在滚动视图中添加多个视图,请将直接子项添加到视图组(例如LinearLayout),并在该LinearLayout中放置其他视图。切勿将RecyclerView或ListView添加到滚动视图。这样做会导致用户界面性能下降和用户体验不佳。
摘自官方文件:https://developer.android.com/reference/android/widget/ScrollView.html