如题,解决方案有三种:
1. 自定义ListView,重载onMeasure()方法,设置全部显示(推荐,最简便快捷)
<span style="font-family:SimSun;">package cn.llzg.widget;
import android.widget.ListView;
public class ScrollViewWithListView extends ListView {
public ScrollViewWithListView(android.content.Context context,
android.util.AttributeSet attrs) {
super(context, attrs);
}
/**
* Integer.MAX_VALUE >> 2,如果不设置,系统默认设置是显示两条
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
</span>2、计算listview总高度并设置
<span style="font-family:SimSun;">ListView listView = (ListView) findViewById(id);
YourAdapter adapter = new MyAdapter("初始化你的适配器");
listView.setAdapter(adapter);
setListViewHeightBasedOnChildren(listView);(在setAdapter后调用自定义的方法)</span><span style="font-family:SimSun;"> public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ReplyListAdapter listAdapter = (ReplyListAdapter) listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int tmp = 0;
//Log.i("------------->", listAdapter.getCount() + "");
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
Log.i("------------>i", i+"");
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(desiredWidth, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
tmp = listItem.getMeasuredHeight();
Log.i("------------>totalHeight", totalHeight+"");
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}</span>1、继承LinearLayout,既然会冲突那就不用ListView 改成线性布局做动态布局效果
2、继承BaseAdapter ,可以参照一下Android app源码中 Widget 目录下的SimpleAdapter 为前面扩展的LinearLayout做数据。
3、模拟数据填充扩展后的BaseAdapter 为扩展后的LinearLayout 加载数据
395



被折叠的 条评论
为什么被折叠?



