ScrollView

  • 1.概述

    • 1)ScrollView:垂直滚动,ScrollView继承FrameLayout,是一种特殊类型的FrameLayout,因为它可以使用户滚动显示一个内容占据的空间大于物理设备显示的空间的视图列表,即:当拥有很多内容,屏幕显示不完时,需要通过滚动来显示更多内容的视图。
    • 2)HorizontalScrollView:水平滚动
    • 3)滚动的内容是它其中包含的View
    • 4)ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的LinearLayout
    • 5)值得注意的是,ScrollView不能和ListView一起使用,因为ListView已经对垂直方向的滚动做了处理,它会迫使如果ListView的内容大于物理视图的内容的时候,强制垂直滚动的效果,所以这里使用ScrollView和ListView混合使用是没有意义的,
    • 6)ScrollView还需要注意EditText自带的多行输入的滚动效果,也是不可以混合使用的,
    • 7)如果在ScrollView中包含了多行的EditText,那EditText中自带的滚动效果将失效。其中心思想就是ScrollView是一个滚动视图的容器,对于一些自带了滚动效果的控件,是无法和它一起被混合使用的。
    • 8)ScrollView其实就是一个布局,所以基本上没有什么太多的自己的方法或者属性需要特别讲解。
    • 9)把其中包裹的LinearLayout的android:orientation属性设置为vertical即可实现垂直滚动的效果
    • 10)构造函数介绍:
      http://blog.csdn.net/ustory/article/details/42454853
  • 2.使用说明
    STICKY

    • 1)scrollview的onScrollChanged方法是只能通过继承scrollview来获取的,那么我们就通过接口的回调把我们需要的值给取出来。
      步骤一:定义一个内部interface接口类,回调CallBack接口类,声明其中的方法
      步骤二:在外部定义一个回调接口类的引用,并定义一个set方法
      步骤三:客户端实现这个内部接口类,调用其setCallbacks(this);
    • 2)Layout container for a view hierarchy that can be scrolled by the user,allowing it to be larger than the physical display
    • 3)A ScrollView is a {@link FrameLayout}, meaning you should place one child in it containing the entire contents to scroll;
    • 4)this child may itself be a layout manager with a complex hierarchy of objects.
    • 5)A child that is often used is a {@link LinearLayout} in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
      **不然就会报错:**java.lang.IllegalStateException: ScrollView can host only one direct child
      **解决方法:**Wrap the Views inside of a LinearLayout so the ScrollView has only the LinearLayout as a direct child
<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
      <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
           // all the views currently in your ScrollView
     </LinearLayout></ScrollView>
  • 6)You should never use a ScrollView with a {@link ListView}, because ListView takes care of its own vertical scrolling. 冲突点,官方都不建议一起使用

    • 7)Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView. 与listview冲突很大,listview会一次性把其items全部显示出来,显然是不友好的
    • 8)The {@link TextView} class also takes care of its own scrolling, so does not require a ScrollView,, but using the two together is possible to achieve the effect of a text view,within a larger container. Textview还是可以一起使用的
    • 9)ScrollView only supports vertical scrolling. For horizontal scrolling,use {@link HorizontalScrollView}.

    • 3.使用建议:

    • 1)其本质就是一个framelayout,容器布局

    • 2)I have an XML layout file, but the content of the layout is more than fits into the screen size. Just make the top-level layout a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:layout_height="match_parent/wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">
   <!-- everything you already have -->
</ScrollView>

注意:其本身自带的属性android:fillViewport =true必须设置,his makes the subviews occupy the whole space of the ScrollView,You can make your entire layout to be scrollable or only the part to be scrollable.
- 3)对于文本框内容超出屏幕范围其实是不需要使用ScrollView,设置

android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"

代码中添加:yourTextView.setMovementMethod(new ScrollingMovementMethod());
4)推荐写法:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">

        <TextView
        android:id="@+id/TEXT_STATUS_ID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"/>

    </ScrollView>
</LinearLayout>

建议:

详解:
http://blog.csdn.net/u014163726/article/details/41801209
使用技巧:
http://stackoverflow.com/questions/6674341/how-to-use-scrollview-in-android
官网:
http://developer.android.com/reference/android/widget/ScrollView.html
Android开发之ScrollView中嵌套ListView的解决方案
http://blog.csdn.net/minimicall/article/details/40983331
http://www.aplesson.com/?p=35
http://blog.csdn.net/zhaokaiqiang1992/article/details/38585547
自定义View超出屏幕大小,如何滑屏
http://www.debugease.com/android/234587.html

  • 3.注意事项
    • 3.1.Change height of scroll view from match_parent to wrap_content. 把高度改为 包裹内容,Because scroll view only enable scrolling if its total height is more than the height of parent view 因为 当滑动控件总高度大于父控件高度时,滑动控件才会滑动
    • 3.2第二个注意事项:the ScrollView is not working when used as the root element of an XML layout. It has to be wrapped inside a LinearLayout. 滑动控件scrollView不能作为布局的根视图,它必须在外面包一层线性布局
    • 3.3.The child view of a ScrollView should be set to wrap_content. If you set it to match_parent, it will fill the area of the ScrollView and never scroll, because it won’t be larger than the ScrollView.Try changing the child LinearLayout layout_height to either wrap_content or a specific size (in dp) instead of match_parent. 滑动控件的子视图需要把高度设置为包裹内容,永远不比scrollView 大。
    • 3.4. 最终标准如下:
      xml
      <com.android.haobanyi.view.MyScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="vertical"
      android:fillViewport="true">

      如果没有超过物理尺寸是不会滑动的,只有超过了屏幕尺寸才会滑动
      参考文档:
      http://stackoverflow.com/questions/30282580/scrollview-not-scrolling-android
      http://stackoverflow.com/questions/14811032/scrollview-not-scrolling-at-all
      http://stackoverflow.com/questions/27739055/android-scrollview-is-not-scrolling
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值