zoukankan      html  css  js  c++  java
  • android ScrollView 与 ListView 冲突汇总

    大部分内容来源于网络 在此总结下。

    方法一:

    public  void setListViewHeightBasedOnChildren(ListView listView) {  
    	        ListAdapter listAdapter = listView.getAdapter();   
    	        if (listAdapter == null) {  
    	            // pre-condition  
    	            return;  
    	        }  
    	  
    	        int totalHeight = 0;  
    	        for (int i = 0; i < listAdapter.getCount(); i++) {  
    	            View listItem = listAdapter.getView(i, null, listView);  
    	            listItem.measure(0, 0);  
    	            totalHeight += listItem.getMeasuredHeight();  
    	        }  
    	  
    	        ViewGroup.LayoutParams params = listView.getLayoutParams();  
    	        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
    	        Log.d("", params.height+"");
    	        listView.setLayoutParams(params);  
    	    }  
    

      以上方法限定 ListView 的Item 布局必须是LinearLayout 才行。

    方法二:

    @Override
    	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    		
    		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    					MeasureSpec.AT_MOST);
    		super.onMeasure(widthMeasureSpec, expandSpec);
    	}
    

      以上方法是要自己定义个ListView 重写里面onMeasure方法。但是会导致界面显示时ListView 直接被置顶 ,ListView上面的内容被顶没了,所以要在

    代码中加入myListView.setFocusable(false); 使一开始ListView暂无焦点,或者在布局文件中加入 一下两个属性(可在ListView 父布局,或者ListView)

    1. android:focusable="true"  
    2. android:focusableInTouchMode="true"   
  • 相关阅读:
    HDU2036 计算多边形的面积
    poj 3648 线段树成段更新
    线段树基本知识
    计算几何基本模板
    最长递增子序列问题—LIS
    poj 2503
    Python基础(5)_字符编码、文件处理
    Python基础(4)_字典、集合、bool值
    Python基础(3)_可变对象与不可变对象、列表、元祖和字典
    流程控制练习
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/3360550.html
Copyright © 2011-2022 走看看