listview嵌套listview视图显示不全以及子listview不能滑动的处理
2017-07-19 23:20
369 查看
在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Item。不能将子Item全部显示,原因是在控件绘制出来之前要对ListView的大小进行计算,要解决将子ListView全部显示出来的问题,就是重新计算一下其大小告知系统即可。后面这个问题比较好解决,网上已经给出解决方案:
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下
/**
* 设置Listview的高度
*/
public void setListViewHeight(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的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了
public class MyListView extends ListView {
public MyListView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView (Context context) {
super(context);
}
public MyListView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
这是解决让作为Item的ListView显示全部内容的方案,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下android对事件的分发机制了。
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
具体自定义父ListView代码 :
public class Parent
90cc
ListView extends ListView {
public ParentListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ParentListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ParentListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//将 onInterceptTouchEvent的返回值设置为false,取消其对触摸事件的处理,将事件分发给子view
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return false;
}
}
上面的方法同样适合在ScrollView中嵌套可以滑动View的情况。
前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下
/**
* 设置Listview的高度
*/
public void setListViewHeight(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的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。
再一个思路相同,但是,不是额外做方法来实现onMeasure()方法的计算LIstView的大小,而是自己继承ListView,重写ListView的onMeasure()方法,来自己计算ListView的高度,然后再xml中直接使用这个自定义的ListView就可以了
public class MyListView extends ListView {
public MyListView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView (Context context) {
super(context);
}
public MyListView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
这是解决让作为Item的ListView显示全部内容的方案,但是有些时候我们是想让作为Item的ListView不用全部显示,而是可以进行滑动,要解决这个问题就需要了解一下android对事件的分发机制了。
我的解决方案是集成ListView,重写interceptTouchEvent使其返回false来取消父ListView对触摸事件的拦截,将触摸事件分发到子View来处理。然后在使用的时候,将其作为父ListView使用,就可以使子ListView可以滑动了。思想来源于下面链接的6楼
具体自定义父ListView代码 :
public class Parent
90cc
ListView extends ListView {
public ParentListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ParentListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ParentListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//将 onInterceptTouchEvent的返回值设置为false,取消其对触摸事件的处理,将事件分发给子view
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return false;
}
}
上面的方法同样适合在ScrollView中嵌套可以滑动View的情况。
相关文章推荐
- 解决ListView嵌套在ScrollerView中显示不全以及滑动冲突的问题
- Android 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- ListView套上ScrollView; ListView显示不全; ListView只显示一条 不能全局滑动问题
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的有关问题
- ListView嵌套ListView涉及到的子ListView无法显示完全,以及父ListView无法执行item的点击事件的处理
- 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- android listview嵌套时,显示不全和不能滑动的解决
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的问题
- listview高度显示不全处理
- ListView嵌套出现子Listview显示不全问题解决
- 在ListView中嵌套ListView的事件处理(listview显示不全)
- listview实现网格布局显示不全,不滑动
- 安卓学习笔记---ScrollView嵌套RecyclerView显示不全,以及滑动冲突、滑动不流畅
- Android自定义ListView和GridView解决滑动冲突和显示不全
- UITextView不能滑动及显示不全问题(iOS7)
- scrollView和listView的滑动冲突和listView显示不全
- Android 联系人分章节显示以及ListView快速滑动显示联系人首字母
- SwipeMenuListView在ScrollView里上下滑动导致菜单不能显示完全的bug解决方法
- ScrollView嵌套ListView,GridView,RecyclerView以及RecyclerView嵌套RecyclerView显示不全的解决方法
- ScrollView 嵌套ListView 滑动冲突,与显示不全