现在使用ListView和ScrollView这个控件联合使用时经常使用了,但相应的也产生很多问题
1:ScrollView中的子布局不能满铺 这里非常简单你只需要将
android:fillViewport="true" 这样子布局面铺效果产生
2.因为ListView其实就本身也是存在ScrollView了,当存在两个ScrollView时问题就产生了,如何能事ListView继续滑动,
网上有很多方法,总要分为两种:
1:将ListView的高度写死,就是固定ListView的高度
2:就是这段代码,但是如果你完全复制这段代码,恭喜你,你有可能实现不了,下面是别人总结的知识:
只要在设置ListView的Adapter后调用此静态方法即可让ListView正确的显示在其父ListView的ListItem中。但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
所以在使用这段代码是,查看你的Item的父类布局是什么.
/***
* 动态设置listview的高度
*
* @param listView
*/
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, 40);
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);
}
public class CustomListView extends ListView {
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}