Android之ScrollView嵌套ListView和GridView冲突的解决方法
- 作者: ----27414689
- 来源: 51数据库
- 2021-10-19
那么里面的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);
}
}
}
核心解决方案: 重写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);
}
}
}
推荐阅读
热点文章

android中Bitmap用法(显示,保存,缩放,旋转)实例分析
12

android 仿微信聊天气泡效果实现思路
1

Android的尺度,drawable-xxxxxxx
2

Codeforces Round #656 (Div. 3) (C、D题)
1

Android之handler异步消息处理机制解析
6

GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题
0

AsyncTask的简单使用
5

两个简单Fragment之间的通信(三种方式)
18

uboot修改设置boot参数命令
41

android中实现从相册中一次性获取多张图片与拍照,并将选中的图片显示出来
2