SwipeRefreshLayout标签布局。 Webview无法向上滚动

问题描述 投票:4回答:5

好的,所以我有一个Tab视图,其中有一个webview,一个listview和一些其他页面。我希望能够执行SwipeRefreshLayout来刷新每个项目。我在每个页面都有这个工作。但是,当我在webview中向下滚动时,我无法向上滚动。它会触发刷新并重建它。我希望能够在我的网页浏览中向上滚动。

布局的东西

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="20px"
    android:layout_weight="1"
    android:nestedScrollingEnabled="true">


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Splash$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:overScrollMode="ifContentScrolls"
        android:nestedScrollingEnabled="false"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerVertical="false"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />

</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

SwipeRefreshLayout正在片段内用于我的选项卡式布局。

        swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);

        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {

                @Override public void onRefresh() {
                    if(getArguments().getInt(ARG_SECTION_NUMBER) == 4 ) {
                        stringArrayList.clear();
                        new updatetheQueue().execute(ctx);
                    }
                    else {
                        swipeLayout.setRefreshing(false);
                    }
                }});
android webview swiperefreshlayout
5个回答
3
投票

OnScrollChangedListener未滚动到顶部时,您可以使用SwipeRefreshLayout禁用WebView

// Only enable swipe to refresh if the `WebView` is scrolled to the top.
webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
  @Override
  public void onScrollChanged() {
     if (webView.getScrollY() == 0) {
         swipeRefreshLayout.setEnabled(true);
      } else {
         swipeRefreshLayout.setEnabled(false);
      }
  }
});

0
投票

让这项工作非常具有挑战性。但是,可以对布局XML进行相当简单的修改以解决此问题。这意味着我无法在刷卡上更新网页浏览,但这没关系。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:overScrollMode="ifContentScrolls" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Splash$PlaceholderFragment">

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="false"
            android:visibility="visible" />
    </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>


0
投票

你可以让一个类扩展SwipeRefreshLayout并覆盖canChildScrollUp来坐scro

@Override
public boolean canChildScrollUp() {
    // TODO Auto-generated method stub


        return true; // if you want it to scroll up 

        // check if child Get To Top 

        //return false // if you want it to execute swipe down to refresh listener

}

0
投票

如果您使用的是ViewPager且选项卡是片段,则可以使用以下代码段修复WebViewFragment:

private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;

@Override 
public void onStart() { 
    super.onStart(); 

    swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
            new ViewTreeObserver.OnScrollChangedListener() {
                @Override 
                public void onScrollChanged() { 
                    if (mWebView.getScrollY() == 0) 
                        swipeLayout.setEnabled(true); 
                    else 
                        swipeLayout.setEnabled(false); 

                } 
            }); 
} 

@Override 
public void onStop() { 
    swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
    super.onStop(); 
}

更多信息在Android - SwipeRefreshLayout with empty textview


0
投票

这就是我如何解决问题,希望它会有所帮助:

mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if(mWebView.getScrollY() == 0){
                    mSwipeRefreshLayout.setEnabled(true);
                }else{
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.