android ScrollView与ListView,GridView滑动冲突
2014-07-28 17:22
387 查看
如果是单一的冲突可以采用网络常用的解决方案:方案一:重写ListView或者GridView的onMeasure()方法@Override
而listview_items.xml上面是一个GridView
要实现这个效果则需要重写GridView。重新获取ListView的高度,但是在获取高度的时候又不能像方案二那样直接获取。那样的话会把每个GridView的items高度相加,就出现了重复相加的情况。所以需要获取GridView的行数。/**
* 重写listview的高度避免与scallview滑动冲突 由于ListView上面的item是GridView
*
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int len = listAdapter.getCount();
int totalHeight = 0;
int totalNum = 0;
for (int i = 0; i < len; i++) {
switch (i) {
case 0:
Log.e("setList", " ScrollView --> " + i);
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalNum += 1;
Log.e("setList", " totalNum " + totalNum);
totalHeight += listItem.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
break;
case 1:
View listItem1 = listAdapter.getView(i, null, listView);
listItem1.measure(0, 0);
List<HomeListParam> listOne = (List<HomeListParam>) listAdapter
.getItem(i);
int num = listOne.size();
int height = listItem1.getMeasuredHeight() / num;
if (num % 3 > 0) {
totalHeight += height * (num / 3 + 1);
totalNum += (num / 3 + 1);
} else {
totalHeight += height * (num / 3);
totalNum += (num / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
case 2:
Log.e("setList", " ScrollView --> " + i);
View listItem2 = listAdapter.getView(i, null, listView);
listItem2.measure(0, 0);
totalNum += 1;
totalHeight += listItem2.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
Log.e("setList", " totalNum " + totalNum);
break;
case 3:
View listItem3 = listAdapter.getView(i, null, listView);
listItem3.measure(0, 0);
List<HomeListParam> listTwo = (List<HomeListParam>) listAdapter
.getItem(i);
int num3 = listTwo.size();
int height3 = listItem3.getMeasuredHeight() / num3;
if (num3 % 3 > 0) {
totalHeight += height3 * (num3 / 3 + 1);
totalNum += (num3 / 3 + 1);
} else {
totalHeight += height3 * (num3 / 3);
totalNum += (num3 / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
case 4:
Log.e("setList", " ScrollView --> " + i);
View listItem4 = listAdapter.getView(i, null, listView);
listItem4.measure(0, 0);
totalNum += 1;
totalHeight += listItem4.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
Log.e("setList", " totalNum " + totalNum);
break;
case 5:
View listItem5 = listAdapter.getView(i, null, listView);
listItem5.measure(0, 0);
List<HomeListParam> listThree = (List<HomeListParam>) listAdapter
.getItem(i);
int num5 = listThree.size();
int height5 = listItem5.getMeasuredHeight() / num5;
if (num5 % 3 > 0) {
totalHeight += height5 * (num5 / 3 + 1);
totalNum += (num5 / 3 + 1);
} else {
totalHeight += height5 * (num5 / 3);
totalNum += (num5 / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
default:
break;
}}
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
int
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>
2
,
MeasureSpec.AT_MOST);
super
.onMeasure(widthMeasureSpec, heightSpec);
}
方案二:针对ListView有效,动态设置ListView的高度public static void getTotalHeightofListView(ListView listView) {ListAdapter mAdapter = listView.getAdapter();if (mAdapter == null) {return;}int totalHeight = 0;for (int i = 0; i <</SPAN> mAdapter.getCount(); i++) {View mView = mAdapter.getView(i, null, listView);mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));//mView.measure(0, 0);totalHeight += mView.getMeasuredHeight();Log.w("HEIGHT" + i, String.valueOf(totalHeight));}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1));listView.setLayoutParams(params);listView.requestLayout();}如果是如图所示的ListView与Scrolliew的解决:首先布局中main.xml中是一个ListView
而listview_items.xml上面是一个GridView
要实现这个效果则需要重写GridView。重新获取ListView的高度,但是在获取高度的时候又不能像方案二那样直接获取。那样的话会把每个GridView的items高度相加,就出现了重复相加的情况。所以需要获取GridView的行数。/**
* 重写listview的高度避免与scallview滑动冲突 由于ListView上面的item是GridView
*
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int len = listAdapter.getCount();
int totalHeight = 0;
int totalNum = 0;
for (int i = 0; i < len; i++) {
switch (i) {
case 0:
Log.e("setList", " ScrollView --> " + i);
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalNum += 1;
Log.e("setList", " totalNum " + totalNum);
totalHeight += listItem.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
break;
case 1:
View listItem1 = listAdapter.getView(i, null, listView);
listItem1.measure(0, 0);
List<HomeListParam> listOne = (List<HomeListParam>) listAdapter
.getItem(i);
int num = listOne.size();
int height = listItem1.getMeasuredHeight() / num;
if (num % 3 > 0) {
totalHeight += height * (num / 3 + 1);
totalNum += (num / 3 + 1);
} else {
totalHeight += height * (num / 3);
totalNum += (num / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
case 2:
Log.e("setList", " ScrollView --> " + i);
View listItem2 = listAdapter.getView(i, null, listView);
listItem2.measure(0, 0);
totalNum += 1;
totalHeight += listItem2.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
Log.e("setList", " totalNum " + totalNum);
break;
case 3:
View listItem3 = listAdapter.getView(i, null, listView);
listItem3.measure(0, 0);
List<HomeListParam> listTwo = (List<HomeListParam>) listAdapter
.getItem(i);
int num3 = listTwo.size();
int height3 = listItem3.getMeasuredHeight() / num3;
if (num3 % 3 > 0) {
totalHeight += height3 * (num3 / 3 + 1);
totalNum += (num3 / 3 + 1);
} else {
totalHeight += height3 * (num3 / 3);
totalNum += (num3 / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
case 4:
Log.e("setList", " ScrollView --> " + i);
View listItem4 = listAdapter.getView(i, null, listView);
listItem4.measure(0, 0);
totalNum += 1;
totalHeight += listItem4.getMeasuredHeight();
Log.e("setList", "Total Height-->" + totalHeight);
Log.e("setList", " totalNum " + totalNum);
break;
case 5:
View listItem5 = listAdapter.getView(i, null, listView);
listItem5.measure(0, 0);
List<HomeListParam> listThree = (List<HomeListParam>) listAdapter
.getItem(i);
int num5 = listThree.size();
int height5 = listItem5.getMeasuredHeight() / num5;
if (num5 % 3 > 0) {
totalHeight += height5 * (num5 / 3 + 1);
totalNum += (num5 / 3 + 1);
} else {
totalHeight += height5 * (num5 / 3);
totalNum += (num5 / 3);
}
Log.e("setList", " totalNum " + totalNum);
break;
default:
break;
}}
相关文章推荐
- android SwipeMenuListView ScrollView 滑动冲突
- android 滑动冲突 ScrollView Viewpager listview 等
- Android ScrollView与ListView,GridView共存冲突解决方案
- Android ScrollView中嵌套ListView,滑动冲突问题的解决
- Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突
- Android滑动冲突解决方式(下拉刷新上拉加载更多,适配RecyclerView/ListView/ScrollView)
- android之ScrollView里嵌套ListView或GridView解决冲突
- Android HorizontalScrollView 水平滑动 里面放listView 解决滑动冲突
- Android中ViewPager与HorizontalListView的滑动冲突处理
- Android 去掉 listView Srollerview HorizontalScrollView 滑动到顶点时出现的阴影
- Android listview viewpager解决冲突 滑动
- android ListView/GridView与ScrollView嵌套的滑动冲突解决
- Android listview viewpager解决冲突 滑动
- Android ScrollView中嵌套GridView,ListView只显示一行的解决办法
- AndroidSwipeLayout的使用(listview,gridview,view中滑动显示隐藏按钮的使用)
- ScrollView+ListView滚动冲突,没有滑动效果 解决办法
- 完美解决HorizontalScrollView与ListView滑动事件冲突
- Android HorizontalScrollView 水平滑动 在listview上面动态添加图片
- android scrollview listview (GridView)显示不全
- android ScrollView下与子控件滑动冲突,如百度地图的mapview