android ScrollView与ListView,GridView滑动冲突

如果是单一的冲突可以采用网络常用的解决方案:
      方案一:重写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的高度

  1. public static void getTotalHeightofListView(ListView listView) {  
  2.     ListAdapter mAdapter = listView.getAdapter();   
  3.    if (mAdapter == null) {  
  4.        return;  
  5.    }  
  6.     int totalHeight = 0;  
  7.     for (int i = 0; i <</SPAN> mAdapter.getCount(); i++) {  
  8.         View mView = mAdapter.getView(i, null, listView);  
  9.         mView.measure(  
  10.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  11.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  12.         //mView.measure(0, 0);  
  13.         totalHeight += mView.getMeasuredHeight();  
  14.         Log.w("HEIGHT" + i, String.valueOf(totalHeight));  
  15.     }  
  16.     ViewGroup.LayoutParams params = listView.getLayoutParams();  
  17.     params.height = totalHeight           + (listView.getDividerHeight() * (mAdapter.getCount() - 1));  
  18.     listView.setLayoutParams(params);  
  19.     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;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值