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

首頁 > 系統 > Android > 正文

Android中listview嵌套scrollveiw沖突的解決方法

2019-12-12 03:51:57
字體:
來源:轉載
供稿:網友

一.使用網上用的動態改變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);      }  } 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正宁县| 隆化县| 漳平市| 吉首市| 南岸区| 临邑县| 平凉市| 军事| 如东县| 赤水市| 龙岩市| 承德市| 凤城市| 保山市| 商丘市| 报价| 东丽区| 江门市| 万山特区| 佳木斯市| 卢龙县| 马公市| 岗巴县| 台安县| 岐山县| 肇东市| 杭锦旗| 达孜县| 威宁| 甘洛县| 收藏| 岐山县| 宜城市| 北票市| 汉中市| 勐海县| 龙川县| 五指山市| 额尔古纳市| 布拖县| 丘北县|