在scrollView中加插ListView是一個大難題。其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE1FF"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadingEdgeLength="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>

大家可以看到ListView中的文字行列只能顯示一行,界面什差,如何解決高度問題,讓我們重新設計一下。

public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] adapterData = new String[] { "Afghanistan", "Albania",… … "Bosnia"};
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));
setListViewHeightBasedOnChildren(listView);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
// 獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
} int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回數據項的數目
View listItem = listAdapter.getView(i, null, listView);
// 計算子項View 的寛高
listItem.measure(0, 0);
// 統計所有子項的總高度
totalHeight += listItem.getMeasuredHeight();
} ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()獲取子項間分隔符占用的高度
// params.height最后得到整个ListView完整顯示需要的高度
listView.setLayoutParams(params); }}

安卓中級教程(4):ScrollView與ListView之間的高度問題的更多相关文章

  1. 安卓中級教程(3):ScrollView

    以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...

  2. 安卓中級教程(5):ScrollView與refreshable之間的設置

    設置向下拉動更新. package com.mycompany.Scroll_test; import android.app.*; import android.os.*; import andro ...

  3. 安卓中級教程(10):@InjectView

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  4. 安卓中級教程(9):pathbutton中的animation.java研究(2)

    src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...

  5. 安卓中級教程(8):pathbutton中的animation.java研究(1)

    src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...

  6. 安卓中級教程(6):annotation的基本用法

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  7. 安卓中級教程(1):@InjectView

    package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...

  8. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  9. 安卓中級教程(7):annotation中的 public @interface的用法

    package com.example.ele_me.util; import java.lang.annotation.Retention; import java.lang.annotation. ...

随机推荐

  1. 【重点】Shell入门教程:流程控制(2)条件判断的写法

    第三节:条件判断的写法 if条件判断中,if的语法结构中的“条件判断”可以有多种形式.测试结果是真是假,就看其传回的值是否为0. 条件测试的写法,有以下10种: 1.执行某个命令的结果 这里的命令,可 ...

  2. winfrom 隐藏任务栏(win7)

    1:新建winfrom 窗体应用程序 2:拖入contextMenuStrip.notifyIcon 2个控件 3:如图 4:code: 注意:复制控件事件要注册 using System; usin ...

  3. .NET LINQ查询语法与方法语法

    LINQ 查询语法与方法语法      通过使用 C# 3.0 中引入的声明性查询语法,介绍性 LINQ 文档中的多数查询都被编写为查询表达式. 但是,.NET 公共语言运行时 (CLR) 本身并不具 ...

  4. 实现tableview的下拉刷新

    实现tableview的下拉刷新 推荐第三方下拉刷新代码http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6 ...

  5. 踩个猴尾不容易啊 Canvas画个猴子

    踩个猴尾不容易啊  Canvas画个猴子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  6. 解析 Linux 内核可装载模块的版本检查机制

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/ 为保持 Linux 内核的稳定与可持续发展,内核在发展过程中引进了可 ...

  7. 无废话ExtJs 入门教程十九[API的使用]

    无废话ExtJs 入门教程十九[API的使用] extjs技术交流,欢迎加群(201926085) 首先解释什么是 API 来自百度百科的官方解释:API(Application Programmin ...

  8. 铁区MES部分页面展示

    激活码: 76231722-2e7554593-b750-07e2f4844531 TIP: 若您激活不成功,可能是因为您所安装的软件版本较低,请尝试以下激活码 激活码: RXWY-A25421-K5 ...

  9. Using Script and Style Bundles【翻译】

    遇到个MVC4中的bundles的问题,问了别人,由于不熟悉MVC4,始终问不到点子上,所以自己就翻译了下资料,搞明白了这个VS显示正常IIS显示异常的小问题,申明我翻译的很烂,不过共享出来或许会帮到 ...

  10. java基础(二)

    1.equals()方法是可以重写的,因此说equals方法是判断两个对象的值是否相等是不完全正确的,当重写方法时可以定义自己的相等规则. 2.Java中的类可以包含六中元素;属性.方法.代码块.内部 ...