android基础开发之scrollview
scrollView 是android系统提供的一种 特殊的展示view。
其实我们很早就遇到过scrollview的东东,比如listview。
而google官方文档也提出,不要混合使用scrollview & listview,应为他们有一定的冲突性。
本文后面会分析和解决这个问题。
1.认识scrollview
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through. You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. 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. The 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. ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.
上面这段就是官方描述,大体讲了几个问题。
- scrollview本质上就是一个framelayout,所以scrollview内部建议只存放一个view,比如linerlayout。
- Textview & ListView 本生就有scroll的功能,所以不建议和scrollview一起使用。
- 水平方向的scrollview ,可以使用HorizontalScrollview
2.使用scrollview。
官方文档上,第一行属性就是android:fillViewport 可见这个属性对scrollview至关重要!
这是属性什么作用:
当你的scrollview的子view,比如说linearlayout,在屏幕够得情况下,想撑满整个屏幕,但是你发现无论你怎么设置layout_height = "match_parent".
它就是不会把整个屏幕撑满。只会按照wrap_content 的方式显示。这个就是scrollview特殊的地方。使用android:fillViewport就可以解决这个问题。
3.scrollview & listview
在某些特殊情况下scroollview 和 listview需要同时使用,以下是参考网上的例子写的一个demo:
布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/act_solution_1_sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0dfeef">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nListView上方数据\n" /> <com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView
android:id="@+id/act_solution_1_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nListView下方数据\n" />
</LinearLayout>
</ScrollView>
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView; /**
* @author deman.lu email luyuanchun032@****.com
* @version on 2016-03-10 11:12
*/
public class ScrollListView extends ListView {
public ScrollListView(Context context) {
super(context);
} public ScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
} public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
如此自定义listview以后,listview就可以全部展示在scrollview里面了。
android基础开发之scrollview的更多相关文章
- android基础开发之WebView
WebView 是android平台沟通 http & H5 页面的桥梁. 但是google对这块的表述不是很清晰,而且SDK里面基本看不到源码,只有一个接口而已. 传送:http://dev ...
- android基础开发之RecycleView(1)---基本使用方式
RecycleView是google为了优化listview,gridview 提供的一个新的控件. 1.android 导入recycleview 在app的gradle里面加入: dependen ...
- Android安全开发之WebView中的地雷
Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...
- android软件开发之webView.addJavascriptInterface循环渐进【一】
本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...
- Android NDK开发之C调用Java及原生代码断点调试(二)
上一篇中,我们主要学习了Java调用本地方法,并列举了两大特殊实例来例证我们的论据,还没学习的伙伴必须先去阅读下,本次的学习是直接在上一篇的基础上进行了.点击:Android NDK开发之从Java与 ...
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...
- Android混合开发之WebView与Javascript交互
前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...
- Android混合开发之WebView使用总结
前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...
- Android安全开发之ZIP文件目录遍历
1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...
随机推荐
- 数据可视化(6)--Google Charts实例
上头给的关于jqplot的问题暂时解决了,继续google charts. google charts的实例很多,就以区域图(Geochart)为例~~ 区域图(Geochart)是一个国家.大陆或者 ...
- Win7以上 32/64位系统隐藏托盘图标
前两天有朋友找我,让帮忙写个小工具,隐藏windows的托盘图标,想想最近在家也不想做太复杂的事,也好几年没写过windows上的小工具了,就答应了.想来挺简单的事,没想到还是有点小插曲的.特地来博客 ...
- 潮流设计:15个创意的 3D 字体版式作品欣赏
3D字体设计是真的很棒,它最适用于广告.使用3D文字和不同的惊人效果,例如灯光或纹理带来了很多东西.在版式设计中,最重要的是消息.如果它抓住了用户的注意力,设计工作是在正确的轨道上. 您可能感兴趣的相 ...
- mysql批量生成修改表和列注释语句
当我们在一个数据库修改完备注后,需要将其导致另一个服务器上的数据库中,需要生成批量执行语句,方便操作,注意用change 或modified 进行列的属性修改时,没有写全的话会造成其它属性的丢失,如 ...
- Jcrop简单实用
今天有一个项目的功能需求 “在上传照片的时候能进行裁剪”,网上找了下,发现有Jcrop这款插件,自己试了下,感觉很不错,蛮好用的.又能增加用户体验,测试了兼容性也很好,所以在这里分享下 首先,可以到官 ...
- linq order by charindex 排序 按给定字符串顺序排序
//list=list.OrderBy(ee => SqlFunctions.CharIndex("书记,主任,支部委员,村委委员,系统工作人员", ee.ZhiWu)).T ...
- FreeBSD的新一代包管理工具Pkg 1.3.0 发布
Pkg 是 FreeBSD 下一代的打包管理工具,用来替代原来的 pkg_info/pkg_create/pkg_add 工具,可以本地打包,也提供远程打包,主要是为了方便远程二进制包升级. Pkg ...
- 狂屌的Windows下的定时任务工具xStarter
xStarter是一款将某些常规计算机操作自动化进行为目的的程序. 它不能为你生成word文件,但是它可以周期性地为你备份文件以保持完整性. 程序的特点有:加强的任务计划工具;在系统事件上执行任务;用 ...
- 配置ssh免密码连接
建立ssh连接步骤: 1,在主机安装ssh-server,执行指令: apt-get install openssh-server 2,在主机上执行指令: netstat -atpn | grep 可 ...
- linux压缩和解压命令总结
一.tar.gz tar -xzvf 二.tar.bz2 tar.bz2 解压命令 bzip2 -d gcc-4.1.0.tar.bz2---上面解压完之后执行下面的命令.执行成功后,会解压生成一个 ...