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

首頁 > 系統 > Android > 正文

android中listview嵌套scrollveiw沖突解決方法

2019-10-23 19:49:56
字體:
來源:轉載
供稿:網友

一.使用網上用的動態改變listview高度的方法,該方法只適用于item布局是LinearLayout布局的情況,不能是其他的,因為其他的Layout(如RelativeLayout)沒有重寫onMeasure(),所以會在onMeasure()時拋出異常。所以使用限制較大。

public class Utility { public static void setListViewHeightBasedOnChildren(ListView 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); } } 

二.網上有帖子說在ScrollView中添加一屬性 android:fillViewport="true" ,這樣就可以讓ListView全屏顯示了。在我機器上測試失敗了。

三:重寫ListView、gridView(推薦)

重寫ListView:

public class MyListView extends ListView {    public MyListView(Context context) {     // TODO Auto-generated method stub     super(context);   }    public MyListView(Context context, AttributeSet attrs) {     // TODO Auto-generated method stub     super(context, attrs);   }    public MyListView(Context context, AttributeSet attrs, int defStyle) {     // TODO Auto-generated method stub     super(context, attrs, defStyle);   }    @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     // TODO Auto-generated method stub     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,         MeasureSpec.AT_MOST);     super.onMeasure(widthMeasureSpec, expandSpec);   } } 

重寫GridView:

/**  *自定義gridview,解決ScrollView中嵌套gridview顯示不正常的問題(1行)  */ public class MyGridView extends GridView{    public MyGridView(Context context, AttributeSet attrs) {        super(context, attrs);      }          public MyGridView(Context context) {        super(context);      }          public MyGridView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);      }          @Override      public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,            MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);      }  } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石河子市| 辉南县| 广丰县| 义马市| 宁阳县| 皋兰县| 汕头市| 荆门市| 陇西县| 香格里拉县| 广汉市| 焉耆| 临潭县| 巢湖市| 凤阳县| 巴中市| 壶关县| 安新县| 二手房| 津南区| 台安县| 顺昌县| 水富县| 综艺| 霍林郭勒市| 庄浪县| 乐山市| 宜春市| 郴州市| 凤阳县| 永顺县| 水城县| 海安县| 板桥市| 衡阳市| 石棉县| 长丰县| 刚察县| 马关县| 屏东市| 大安市|