当前位置: 移动技术网 > 移动技术>移动开发>Android > Android之ScrollView嵌套ListView和GridView冲突的解决方法

Android之ScrollView嵌套ListView和GridView冲突的解决方法

2019年07月24日  | 移动技术网移动技术  | 我要评论
那么里面的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);  
        }  
    }  
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网