(源码)自己写的ScrollView里套漂亮的圆角listview(算是漂亮吧。。。)




找了相关的资料终于写完了:
http://blog.csdn.net/jamin0107/article/details/6973845

http://emmet1988.iteye.com/blog/1097443


原来在scrollview中套listview需要将listview的高度固定
,这里就需要将listview的子类高度计算

同时还要注意子ListView的每个Item必须是LinearLayout


“引用连接中的话”------------------------------------------------------
只要在设置ListView的Adapter后调用此静态方法即可让ListView正确的显示在其父ListView的ListItem中。但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
  在ScrollView中嵌套ListView(或者ScrollView)的另外一个问题就是,子ScrollView中无法滑动的(如果它没有显示完全的话),因为滑动事件会被父ScrollView吃掉,如果想要让子ScrollView也可以滑动,只能强行截取滑动事件,有牛人在论坛中发过代码说可以。虽然我没有亲自试过,但估计是可行的。
-------------------------------------------------------------
Java代码 复制代码  收藏代码
  1. 在listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就Okay 了。      
  2. public class Utility {      
  3. public static void setListViewHeightBasedOnChildren(ListView listView) {      
  4. //获取ListView对应的Adapter      
  5. ListAdapter listAdapter = listView.getAdapter();       
  6. if (listAdapter == null) {      
  7. // pre-condition      
  8. return;      
  9. }      
  10.      
  11. int totalHeight = 0;      
  12. for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目      
  13. View listItem = listAdapter.getView(i, null, listView);      
  14. listItem.measure(00); //计算子项View 的宽高      
  15. totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度      
  16. }      
  17.      
  18. ViewGroup.LayoutParams params = listView.getLayoutParams();      
  19. params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));      
  20. //listView.getDividerHeight()获取子项间分隔符占用的高度      
  21. //params.height最后得到整个ListView完整显示需要的高度      
  22. listView.setLayoutParams(params);      
  23. }      
  24. }    
Java代码 复制代码  收藏代码
  1. Listview其他属性   
  2. 1.去滑动到顶点和底边时的黑色阴影   
  3. [html] view plaincopy   
  4. android:fadingEdge="none"     
  5.   
  6. 2.去拖动时默认黑色底色   
  7. [html] view plaincopy   
  8. android:cacheColorHint=“#00000000”     
  9.   
  10. 3.去选中时的黄色底色   
  11. [html] view plaincopy   
  12. android:listSelector="#00000000"    
Java代码 复制代码  收藏代码
  1. <ListView   
  2.             android:id="@+id/roundlistview01" android:layout_width="fill_parent"  
  3.             android:layout_height="wrap_content" android:background="@drawable/shape"  
  4.             android:cacheColorHint="#00000000" android:drawSelectorOnTop="false"  
  5.             android:fadingEdge="none" android:listSelector="#00000000"  
  6.             android:layout_marginLeft="10dip" android:layout_marginRight="10dip">   
  7.         </ListView>  
圆角android:background="@drawable/shape":
shape.xml
Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">   
  3.  <!-- 实心  透明色   
  4.  <solid android:color="#FFFFFF"/>   
  5.  -->   
  6.      <gradient android:startColor="#F0F0F0"     
  7.         android:endColor="#F0F0F0"     
  8.          android:angle="90" />   
  9.      <stroke   
  10.          android:width="2dp"  
  11.          android:color="#6C6C6C"  />   
  12.      <corners   
  13.          android:radius="10dip" />   
  14.      <padding   
  15.          android:left="0dp"  
  16.          android:top="0dp"  
  17.          android:right="0dp"  
  18.          android:bottom="0dp" />   
  19. </shape>  
源代码:下载地址1: http://dl.iteye.com/topics/download/8bb55721-9bda-3271-ac3d-576a78e22624
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值