ListView嵌套在ScrollView
来源:互联网 发布:淘宝js 编辑:程序博客网 时间:2024/05/16 14:35
今天在做项目中遇到要把ListView嵌套在ScrollView,在网上找了一下,很多人都用来这个方法
/**
* 设置listview的高度
*
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
listView.post(new Runnable() {
@Override
public void run() {
((ScrollView) findViewById(R.id.scSpace))
.scrollTo(0, 0);
}
});
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
adapter.notifyDataSetChanged();// 通知listView刷新数据
}
这个方法确实好用,我listView的Item布局为:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/family_log_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 的说法螺丝扣搭街坊考六级大风是的风景士大夫dfsdfs" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/xiangp_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
<ImageView
android:id="@+id/xiangp_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
<ImageView
android:id="@+id/xiangp_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/theme_color" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/family_log_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="标题"
android:textSize="18sp" />
<TextView
android:id="@+id/family_log_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:text="2015-05-12"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/family_ruond" />
</RelativeLayout>
我运行起来,老实报listItem.measure(0, 0);这里是空指针,我找了一个早上都没有找出原因,因为我之前的项目里用都是好好的,没办法啦,我只能去改了listVIew的Item布局;<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/family_log_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 的说法螺丝扣搭街坊考六级大风是的风景士大夫dfsdfs" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/xiangp_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
<ImageView
android:id="@+id/xiangp_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
<ImageView
android:id="@+id/xiangp_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/toux_img" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/theme_color" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/family_log_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="标题"
android:textSize="18sp" />
<TextView
android:id="@+id/family_log_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:text="2015-05-12"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/family_ruond" />
</RelativeLayout>
</LinearLayout>
改为这样就可以了
- 在ScrollView嵌套ListView
- ListView嵌套在ScrollView
- 在ScrollView中嵌套ListView
- 在ScrollView中嵌套ListView
- 在ScrollView中嵌套ListView
- 在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- 总结:在ScrollView中嵌套ListView
- Android: 如何在ScrollView中嵌套ListView
- 总结:在ScrollView中嵌套ListView
- 总结:在ScrollView中嵌套ListView
- 总结:在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- Android:如何在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- 如何在ScrollView中嵌套ListView
- Python 之 @property
- 设计模式(结构型)之代理模式(Proxy Pattern)
- Android lint发现隐藏的Bug
- Php操作redis写入对象和查询对象
- POI导出Excel经典实现
- ListView嵌套在ScrollView
- [LeetCode] Combination Sum II
- java 通过System.getProperties()获取系统参数
- Magento 报错 connection string is empty
- 设置Tomcat编码
- 4.UI篇 UIKit,view与layer,frame 与bounds等 CoreText auto layout drawRect
- VMware下OSSIM 5.0安装和使用小结
- packstack + vxlan 利用vbox虚拟机搭建1控制节点+2计算节点openstack
- c# 无法加载DLL“###.dll”,: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E),解决办法总结