ScrollView 嵌套listview 如果子item数量多的话 listview高度不准确问题

<pre name="code" class="java">重新写一个类  继承 ListView 
重写
onTouch    onScroll
 
onMeasure
三个方法
 
onMeasure计算listview 的高度


 

</pre><pre name="code" class="java">
package com.jyt.msct.famousteachertitle.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;

@SuppressLint("DrawAllocation")
public class MyListView extends ListView implements OnTouchListener,
		OnScrollListener {

	private int listViewTouchAction;
	private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		listViewTouchAction = -1;
		setOnScrollListener(this);
		setOnTouchListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (getAdapter() != null
				&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
			if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
				scrollBy(0, -1);
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int newHeight = 0;
		final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		if (heightMode != MeasureSpec.EXACTLY) {
			ListAdapter listAdapter = getAdapter();
			if (listAdapter != null && !listAdapter.isEmpty()) {
				int listPosition = 0;
				for (listPosition = 0; listPosition < listAdapter.getCount()
						&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
					View listItem = listAdapter.getView(listPosition, null,
							this);
					// now it will not throw a NPE if listItem is a ViewGroup
					// instance
					if (listItem instanceof ViewGroup) {
						listItem.setLayoutParams(new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
					}
					listItem.measure(widthMeasureSpec, heightMeasureSpec);
					newHeight += listItem.getMeasuredHeight();
				}
				newHeight += getDividerHeight() * listPosition;
			}
			if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
				if (newHeight > heightSize) {
					newHeight = heightSize;
				}
			}
		} else {
			newHeight = getMeasuredHeight();
		}
		setMeasuredDimension(getMeasuredWidth(), newHeight);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (getAdapter() != null
				&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
			if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
				scrollBy(0, 1);
			}
		}
		return false;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值