您的位置:首页 > 移动开发 > Android开发

android ScrollView与ListView,GridView滑动冲突

2014-07-28 17:22 387 查看
如果是单一的冲突可以采用网络常用的解决方案:方案一:重写ListView或者GridView的onMeasure()方法@Override
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;
}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: