动态设置ListView的高度的两种方法

 

今天开发遇到一个问题,就是当动态插入ListView数据的时候,由于之前ListView的高度已经固定死了,所以显示不完整的数据,并且底部显示黑边。

解决方法一如下:

首先考虑到如果要实现界面的滚动,需要使用ScrollView控件,该方法就是使用ScrollView控件实现ListView高度的动态设置。

activity中添加下面函数

<span style="font-family:Microsoft YaHei;font-size:18px;">public void setListViewHeightBasedOnChildren(ListView listView) {  
  
  ListAdapter listAdapter = listView.getAdapter();  
  
  if (listAdapter == null) {  
   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));  
  
  ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除  
  
  listView.setLayoutParams(params);  
 }  
</span>


 xml 文件代码如下:

<span style="font-family:Microsoft YaHei;font-size:18px;"><ListView  
             android:id="@+id/getInfo"  
             android:layout_width="fill_parent"  
             android:layout_height="fill_parent"  
             android:cacheColorHint="#FFF4F4F4"  
             android:dividerHeight="0.0dip"   
             android:fadingEdge="none" // 边界黑边  
             />  </span>


 ScrollView中放置ListView动态设置ListView高度的时候需要如下面的布局:

<span style="font-family:Microsoft YaHei;font-size:18px;"><ScrollView
        android:id="@+id/feedbacklayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" 
            android:paddingLeft="0px">
	        <ListView
	            android:id="@+id/mySalesPromotionListView"
	            android:layout_width="fill_parent"
	            android:layout_height="fill_parent"
	            android:layout_marginBottom="5.0dip"
	            android:layout_marginRight="5.0dip"
	            android:textColor="#000"
	            android:textSize="16.0dip" >
	        </ListView> 
        </LinearLayout>
    </ScrollView></span>

解决方法二如下:

使用ListView控件的特有属性:android:scrollbars="vertical",该属性有三个值。none(隐藏),horizontal(水平),vertical(垂直)。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
QML中的ListView是一种用于显示可滚动列表的组件,而两端遮罩是指在ListView的两端添加一个遮罩效果,使得列表在滚动到边界时能够有一个渐变的遮罩效果。 要实现ListView的两端遮罩效果,可以使用ListView的属性和信号来进行设置。以下是一种实现方式: 1. 首先,在ListView的外部包裹一个Rectangle组件,用于实现遮罩效果。设置该Rectangle的颜色为透明,并设置其渐变效果。 ```qml Rectangle { id: mask color: "transparent" gradient: Gradient { GradientStop { position: 0.0; color: "transparent" } GradientStop { position: 0.1; color: "white" } GradientStop { position: 0.9; color: "white" } GradientStop { position: 1.0; color: "transparent" } } } ``` 2. 然后,在ListView中添加onContentYChanged信号的处理函数,用于根据列表的滚动位置来更新遮罩的高度和位置。 ```qml ListView { id: listView // 设置其他属性... onContentYChanged: { var contentHeight = contentItem.height var visibleHeight = height var scrollPosition = contentY // 计算遮罩的高度和位置 var maskHeight = Math.min(visibleHeight, contentHeight) // 遮罩高度为可见区域高度或内容高度的较小值 var maskY = Math.max(0, scrollPosition) // 遮罩的Y坐标为滚动位置,但不小于0 // 更新遮罩的尺寸和位置 mask.height = maskHeight mask.y = maskY } } ``` 通过以上的设置,当ListView滚动到顶部或底部时,遮罩会渐变地显示出来,给用户一个视觉上的提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值