將原生ListView替換為下方自定義的,即可解決問(wèn)題,GridView同理
/**
* Created by wgd on 2018/6/11.
*/
public class ListViewForScrollView extends ListView{
public ListViewForScrollView(Context context) {
super(context);
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
/**
* 重寫該方法,達(dá)到使ListView適應(yīng)ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
簡(jiǎn)單介紹一個(gè)原理:
這種解決方法是代表,讓ListView自己填充高度,那么在測(cè)量方法中,就需要在onMeasure按照測(cè)量機(jī)制傳值。
具體分析:參考博客https://www.cnblogs.com/RabbitLx/p/5858031.html