Android ListView嵌套
来源:互联网 发布:安卓版爱剪辑软件 编辑:程序博客网 时间:2024/06/10 07:55
技术初段,还请各位多多赐教,不吝感谢。
最近做到朋友圈以及相关评论的功能,朋友圈每个item下需要实现评论列表。(做的比较简陋)
实现方案是在ListView的item下嵌套一个子ListView实现评论列表。另外一说,评论框我用的是PopupWindow做的,而不是在item下隐藏布局,不知道其他软件是如何实现的。
用到子ListView时就遇到问题了,子ListView高度的问题。可以根据子item来重新计算高度,在setAdapter方法后调用设置高度的方法setListViewHeightBasedOnChildren(mListView).
/**
* 重新计算子listview高度适配
* @param listView
*/
public static 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));
listView.setLayoutParams(params);
}
调用了这个方法之后,发现还有一个问题,子item评论不是一行的话,高度计算就会出错,只能计算Textview一行的高度。所以还需要重写子listview,在onMeasure
()方法中 将其高度设置为尽可能显示全部内容高度--MeasureSpec.AT_MOST
。代码如下:
public
class
MyListView
extends
ListView {
public
MyListView(android.content.Context context, android.util.AttributeSet attrs) {
super
(context, attrs);
}
@Override
public
boolean
dispatchTouchEvent(MotionEvent ev) {
return
super
.dispatchTouchEvent(ev);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
int
expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>
2
,
MeasureSpec.AT_MOST);
super
.onMeasure(widthMeasureSpec, expandSpec);
}
}
这样可以基本的完成评论列表功能。如图
- Android 中ListView 嵌套 ListView
- Android listview 中嵌套 listview
- android ScrollView嵌套ListView
- Android ExpandableListView嵌套ListView
- android ScrollView嵌套ListView
- Android ListView嵌套
- android ScrollView嵌套Listview
- android 嵌套 listview
- android scrollview 嵌套listview
- Android ScrollView嵌套ListView
- Android ExpandableListView嵌套ListView
- Android Scollowview嵌套ListView
- listView 嵌套gridView android
- Android之ScrollView嵌套ListView
- Android之ScrollView嵌套ListView
- android scrollView嵌套listview问题
- Android中ScrollView嵌套ListView
- Android之ScrollView嵌套ListView
- linux内的正则表达式
- 回忆是个陷阱,不小心掉进往事的长河,那些过往的殇,就会把我拖进沼泽
- Enable Geolocation in a WebView (Android)
- sgu245:Black-White Army(技巧暴力)
- 织梦返回上一页面
- Android ListView嵌套
- Android---手动创建线程与GUI线程同步(二)
- 凯云水利水电工程造价系统 (四) 配合比单价模块
- 项目管理修炼之道(一 启动项目)
- Linux系统编程——进程间通信:管道(pipe)
- Spring MVC的@ResponseBody返回JSON串时Content-Type编码问题
- 读写xml文件
- 到老的爱情
- js类型检测