ListView在显示较少数据是,高度由item的个数决定,wrap_content有效
来源:互联网 发布:黑客用函数语言编程 编辑:程序博客网 时间:2024/05/20 13:11
有时候我们需要如下图效果:
我们只需要显示6条数据,在ListView下面显示一个按键,用来清除ListView中的数据。可是我们无法实现,我们的按键总是被挤压到屏幕的最底部。我们如何实现呢?方法很简单。
注意:这种方法,在数据超出一屏幕的时候,是能使用的,因为该listview不可以滑动。
网上找了也别的的方法,需要计算list中显示条目的个数,然后在在代码中设置listview的高度。
这里看看我怎么实现的
我们的布局代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/search_title_layout" android:orientation="vertical"> <com.xis.read.view.ListViewForScrollView android:id="@+id/lv_history" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_clear_history" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_common_gray_background" android:gravity="center" android:padding="10dp" android:text="@string/claer_history" android:textColor="@color/readpage_rose" /> </LinearLayout>
上面的重要的只有一条ListView
的高度用wrap_content
接下来我们只要自定义ListView就好了。这个自定义ListView也可以用于ScrollView中嵌套使用
package cn.xs.reader.view;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * Created by Saud on 16/2/15. */public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context) { super(context); } public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override /** * 重写该方法,达到使ListView适应ScrollView的效果 */ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
这就解决了我们想要的Listview 的 wrap_content效果。
0 0
- ListView在显示较少数据是,高度由item的个数决定,wrap_content有效
- 由item动态设置listview的高度
- 适配item的不同高度决定ListView的高度
- 获取listview的所有子目录高度(使用item较少情况)
- listview,gridview 邪恶的wrap_content高度属性导致数据错乱
- ListView中根据手机屏幕高度动态设置只显示一屏幕的item个数
- ListView中根据手机屏幕高度动态设置只显示一屏幕的item个数
- ListView item点击事件问题:第一次点击有效,再次点击item无效。场景是在item的展开效果
- ListView显示全部Item高度
- listview item的高度
- listview的item中显示高度不确定的图片
- listview item子view的自适应高度后不显示
- SrollView嵌套Gridview,ListView高度设置wrap_content显示不全解决
- 解决:有时listview设置为wrap内容时,listview高度只显示一条item的高度
- Listview的Item高度计算
- Android ListView item显示时高度变化
- 重新计算了ListView的高度,发现最后一个item项却没有完整显示出来,或是数量越大最后几个是显示不出来的
- Android 在ListView中获取item高度
- 一笔画问题 nyoj42
- iOS 百度地图当前位置方向问题解决方案
- php常用的一些函数
- java设计模式——结构型之适配器模式
- 通过一个大型项目来学习分布式算法(2)
- ListView在显示较少数据是,高度由item的个数决定,wrap_content有效
- Maven实现远程热部署
- 23. 手势识别之UIRotationGestureRecognizer
- lintcode:Reorder List
- call()、apply()、bind()
- HDOJ1241 Oil Deposits
- iOS 证书的奇葩问题
- 我们要学习的算法
- 类加载机制