在scrollview中嵌套listview或者gridview时
如果listview或者gridview的高度时wrap-content会导致两者只显示一行多一点
原因:
listview和gridview都继承于scrollview 两个scrollview嵌套会导致高度有问题。
核心解决方案:
重写listview和gridview的onMeasure方法 如下:
package come.cake.customview;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.ListView;/** * Created by apple on 16/1/20. */public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_MOVE) { return true;//禁止listview进行滑动 } return super.dispatchTouchEvent(ev); }}
package come.cake.customview;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.GridView;public class MyGridView extends GridView { public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_MOVE) { return true;//禁止Gridview进行滑动 } return super.dispatchTouchEvent(ev); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}gridView.setFocusable(false);//scrollview内部嵌套gridview防止从底部显示,加上这行代码就会从头部显示