国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ScrollView 嵌套 ListView or ExpandableListView顯示問題(一行屬性搞定)

2019-11-06 09:41:39
字體:
來源:轉載
供稿:網友

前言

項目中難免會用到滾動視圖嵌套列表的情況

ScrollView

若一個手機屏幕的大小都不能夠顯示整個頁面的話,最外層根視圖可以考慮使用ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"></ScrollView>

當然,使用ScrollView也會有一個局限性。 相信大家應該也都了解到 ScrollView 的子布局只允許有一個,否則XML的編譯也會報錯。

這里寫圖片描述

ScrollView嵌套列表出現的問題

這是一個根視圖為ScrollView 的XML布局,里面嵌套的是ListView。然后就會出現列表內容只顯示了一個Item。就算有多組數據,列表也只會顯示一個。

這里寫圖片描述

根據以往的經驗,可能會想到去動態的計算出每一個Item的寬高才可以完全顯示,這也算是一種辦法。但是沒辦法在PReview中查看ListView是否真的顯示完整,只能通過運行到模擬器或者真機上調試看效果。 動態計算每一個Item的代碼:

public static void setListViewHeightBasedOnChildren(ExpandableListView listView) { //獲取ListView對應的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回數據項的數目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); //計算子項View 的寬高 totalHeight += listItem.getMeasuredHeight(); //統計所有子項的總高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); //listView.getDividerHeight()獲取子項間分隔符占用的高度 //params.height最后得到整個ListView完整顯示需要的高度 listView.setLayoutParams(params);}

這并不是我們最終想要的效果。 其實只要在根視圖中加一行 android:fillViewport="true" 就可以了。 不管內部嵌套的是ListView 還是ExpandableListView 都能完美解決這個問題。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"/></ScrollView>

這里寫圖片描述


上一篇:runtime學習筆記

下一篇:進程與線程詳解

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南部县| 鄢陵县| 咸阳市| 澜沧| 周口市| 大兴区| 临城县| 柳州市| 潞城市| 囊谦县| 安达市| 汉沽区| 昆明市| 酉阳| 武城县| 枣阳市| 上栗县| 松桃| 长沙县| 巴林右旗| 麦盖提县| 竹溪县| 伽师县| 麻江县| 桑植县| 衡阳县| 澎湖县| 五华县| 元江| 汾西县| 安陆市| 金华市| 清丰县| 五家渠市| 任丘市| 仙游县| 呈贡县| 贵德县| 丰都县| 普洱| 南部县|