ScrollView下的ListView问题解决

问题描述:在一个复合布局当中,有TextView、ListView、Button等,除了button之外,其余高度不定,是动态变化的,很容易超过一个屏幕的高度,这个时候我们需要给总布局添加一个ScrollView,但因为ListView集成与ScrollView,所以会导致listview的Scroll效果失效;

解决方法:找了很多方法,大体思路是动态设计ListView的Item高度,经过不断找文章,试demo,终于发现网友一个解决方法,现在分享给大家~

解决思路:重写ListView,改写onMeasure方法

package com.jby.exam;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class VoteList extends ListView {

	public VoteList(Context context) {
		super(context);
	}
	
	public VoteList(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public VoteList(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
	}
}




注意:XML布局文件引用自己的控件,需要写好包名,如:

<com.jby.exam.VoteList
      android:id="@+id/vote_listview"
      android:layout_height="wrap_content"
      style="@style/vote_listview" >
</com.jby.exam.VoteList>


猜你喜欢

转载自blog.csdn.net/op_kapu/article/details/30973753
今日推荐