Android之ScrollView嵌套ListView和GridView冲突的解决方法
那么里面的scrollview高度计算就会出现问题。我们也就无法得到想要的效果。
核心解决方案: 重写listview或者gridview的onmesure 方法。
public class mylistview extends listview {
public mylistview(context context) {
super(context);
}
public mylistview(context context, attributeset attrs) {
super(context, attrs);
}
public mylistview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int expandspec = measurespec.makemeasurespec(integer.max_value >> 2,
measurespec.at_most);
super.onmeasure(widthmeasurespec, expandspec);
}
}
gridview
public class mygridview extends gridview {
private boolean havescrollbar = true;
public mygridview(context context) {
super(context);
}
public mygridview(context context, attributeset attrs) {
super(context, attrs);
}
public mygridview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
/**
* 设置是否有scrollbar,当要在scollview中显示时,应当设置为false。 默认为 true
*
* @param havescrollbars
*/
public void sethavescrollbar(boolean havescrollbar) {
this.havescrollbar = havescrollbar;
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
if (havescrollbars == false) {
int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most);
super.onmeasure(widthmeasurespec, expandspec);
} else {
super.onmeasure(widthmeasurespec, heightmeasurespec);
}
}
}
如对本文有疑问,
点击进行留言回复!!
网友评论