Android布局中ScrollView与ListView的冲突的最简单方法

来源:互联网 发布:xml json 编辑:程序博客网 时间:2024/06/05 02:05

看到网上流行的一种使用方法是:

public class Utility {         public static 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));             listView.setLayoutParams(params);         }     } 


这个太麻烦了,而且效果又不是很明显。
 
有人的总结如下:
 
只要在设置ListView的Adapter后调用此静态方法即可让ListView正确的显示在其父ListView的ListItem中。但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
 
  在ScrollView中嵌套ListView(或者ScrollView)的另外一个问题就是,子ScrollView中无法滑动的(如果它没有显示完全的话),因为滑动事件会被父ScrollView吃掉,如果想要让子ScrollView也可以滑动,只能强行截取滑动事件,有牛人在论坛中发过代码说可以。虽然我没有亲自试过,但估计是可行的。
  虽然在ScrollView中显示ScrollView在技术上的难题可以攻破,但是这样的设计却是非常差的用户体验因为用户会不容易看到和操作子ScrollView中的内容。比如好的设计是,父ListView的每个Item只显示概括性的描述,然后点击其Item会进入另外一个页面来详细描述和展示以及对这个Item的操作。
 
于是找到另外两种比较简单的方法,而且又没有影响的:
 
1.在ScrollView中添加一属性 android:fillViewport="true" ,这样就可以让ListView全屏显示了
2.指定ListView的高度 android:layout_height="420dp" ;

转自:http://www.2cto.com/kf/201109/102537.html

0 0
原创粉丝点击