Android解決SwipeRefreshLayout和webview下拉刷新衝突問題

在SwipeRefreshLayout中嵌套webview時,默認無論在網頁任何位置下拉都會觸發刷新,實際需求是隻在頂部需要,解決方法是繼承SwipeRefreshLayout,複寫其中的canScrollUp方法:

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class ScrollSwipeRefreshLayout extends SwipeRefreshLayout {
    // 子佈局 這裏爲webview
    private ViewGroup mChildViewGroup;

    public ScrollSwipeRefreshLayout(Context context) {
        super(context);
    }
    public ScrollSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ViewGroup getViewGroup() {
        return mChildViewGroup;
    }
    public void setViewGroup(ViewGroup viewGroup) {
        mChildViewGroup = viewGroup;
    }


    /**
     * @return Whether it is possible for the child view of this layout to
     *         scroll up. Override this if the child view is a custom view.
     */
    @Override
    public boolean canChildScrollUp() {
        if (null != mChildViewGroup) {
            if (mChildViewGroup.getScrollY() > 0) {
                return true;
            }
            return false;
        }
        return super.canChildScrollUp();
    }
}

然後在使用時傳入webview即可:

mRefreshLayout.setViewGroup(webView);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章