【Android】Scrollview 顶端固定 or 滑动锚钉(续)(ScrollView里嵌套ListView)

来源:互联网 发布:小米路由wol网络唤醒 编辑:程序博客网 时间:2024/05/23 15:47

实际应用的时候因业务需求,需要让ListView(gridview)具有下拉刷新、下拉刷新功能,需要在ScrollView里嵌套ListView
PS:关键点——
自定义ListView,重写其onMeasure方法:

    @Override    /**     * 重写该方法,达到使ListView适应ScrollView的效果     */    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        DebugU.sop("list measure");        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }

PS:有位大哥总结了4种方法

此后在自定义的一个可以上下拉刷新的布局中,放置子item:ScrollView,在这个自定义的ScrollView中暴露onScrollListener监听器,这样就可以再Activity中设置ScrollView的监听,按照之前博客:Scrollview 顶端固定 or 滑动锚钉,中的动态添加删除方法,修改其中的onScroll方法如下:

 public void onScroll() {        if (mPinnedView == null) return;        int[] posPinnedView = new int[2];        mPinnedViewParent.getLocationInWindow(posPinnedView);        int[] posLayout = new int[2];        getLocationInWindow(posLayout);        //利用pinview与其所在的posLayout在屏幕中的相对位置来决定是否将pinView固定在FrameLayout的最顶端,        pinView(posPinnedView[1] > posLayout[1]);    }

PS:获取控件坐标
抽象布局(Include,merge,ViewStub)
Android 带你从源码的角度解析Scroller的滚动实现原理
Android UI测量、布局、绘制过程探究
如何正确的获得一个view的宽和高

0 0
原创粉丝点击