ScrollView not scrolling at all

时间秒杀一切 提交于 2020-01-09 03:43:17

问题


I can't make a ScrollView properly scrolling. It always cut off the content on the bottom, as if it were a normal LinearLayout.

My code

<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" >

Of course, I already tried to add / remove the "fillViewport" and "isScrollContainer" properties, and it did not change anything at all.

Thanks in advance.


回答1:


Answer: the ScrollView is not working when used as the root element of an XML layout. It has to be wrapped inside a LinearLayout.

Solution then :

<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" >

        </LinearLayout>
    </ScrollView>
</LinearLayout>



回答2:


The selected answer IS NOT CORRECT!

You CAN use ScrollView as the root view, it doesnt work for you because you are missing the padding.

Add something like:

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"



回答3:


remove the android:isScrollContainer in LinearLayout. As per the documentation android:isScrollContainer is used to set the view scrollable. I hope it helps you. Refer this link for definition.




回答4:


Android Studio adds a NestedScrollView to the activity file of some of its templates (e.g. Master-Detail). Having a ScrollView in the fragment file and another one in that fragment’s activity file prevents the scroll view from working. Removing the ScrollView in my fragment file and leaving the one in the activity file solved the issue for me.




回答5:


Scroll View as Parent has no issues. I faced such issue when we add padding/margin to the direct child of scrollview. Keep the child of scrollview with just height and width properties, den it will work fine.

    <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>



回答6:


Try this,

<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" >



回答7:


Try this solution, just delay your scroll using post delayed method.

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>

TestFragment.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);

This worked for me, hope this helps.




回答8:


This fixed my problem: I added android:isScrollContainer="true" to a LinearLayout and Removed the ScrollView Code Before:

    <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>

Code After

    <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>



回答9:


Is there a way to close this question? it is old and currently the answer marked as valid doesn't have sense. The only valid thing regarding ScrollView right now is this:

A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout. Never add a RecyclerView or ListView to a scroll view. Doing so results in poor user interface performance and a poor user experience.

Taken from official documentation: https://developer.android.com/reference/android/widget/ScrollView.html




回答10:


I finally figured it out took me literally 5 hours building everything step by step and testing it every step at a time ugh....

Anyway if you have a problem with this i found out that setOntouch listener is messing up your code - at least in my case it was...

i have to figure a way around it, but try to delete your ontouch listener and test your code - it has to work



来源:https://stackoverflow.com/questions/14811032/scrollview-not-scrolling-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!