2013-02-11 80 views
15

我無法正確滾動ScrollView。它總是切斷底部的內容,就好像它是一個正常的LinearLayout。ScrollView根本不滾動

我的代碼

<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」屬性,它並沒有在改變任何東西。

在此先感謝。

+0

能否請您粘貼完整的XML,也許是你的LinearLayout相關的東西。 – PaNaVTEC 2013-02-11 11:14:07

+0

嘗試將'android:layout_height =「match_parent」'更改爲'android:layout_height =「0dp」'並在'ScrollView'上添加'android:layout_weight =「1」'。 – 2013-02-11 11:14:53

+0

剛剛嘗試過,沒有運氣。 – thomaus 2013-02-11 12:56:02

回答

19

回答:當用作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" > 
+47

這對我不起作用... – Ciprian 2013-08-12 08:28:43

+0

Are you sure?!! – 2013-11-24 13:29:15

+1

+1確實爲我工作,謝謝。 – 2014-04-23 12:04:06

2

刪除的LinearLayout。根據文檔android:isScrollContainer用於設置視圖可滾動。我希望它能幫助你。請參閱此link的定義。

2

Android Studio中增加了一個NestedScrollView到它的一些模板的活動文件(例如主從)。在片段文件中有一個ScrollView,而在該片段的活動文件中有一個ScrollView可以防止滾動視圖正常工作。刪除我的片段文件中的ScrollView並將其保留在活動文件中解決了我的問題。

0

試試這個解決方案,只是延遲您的滾動使用後期延遲方法。

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

這爲我工作,希望這有助於。

0

這個固定我的問題:我添加android:isScrollContainer="true"到的LinearLayout並刪除了滾動 代碼:之前

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

之後有沒有辦法關閉這個題?它是舊的,目前標記爲有效的答案沒有意義。關於ScrollView現在唯一有效的事情是:

一個視圖組,允許在其中放置視圖層次結構進行滾動。 滾動視圖可能只有一個直接放置在其中的兒童。要在滾動視圖中添加多個視圖,請創建添加視圖組的直接子視圖(例如LinearLayout),並在該LinearLayout中放置其他視圖。 切勿將RecyclerView或ListView添加到滾動視圖。這樣做會導致較差的用戶界面性能和較差的用戶體驗。

從官方資料爲準:https://developer.android.com/reference/android/widget/ScrollView.html

0

選擇的答案是不正確的!

你可以使用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"