6

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

布局的东西

<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);
                    }
                }});
4

5 回答 5

6

当没有滚动到顶部时,您可以使用OnScrollChangedListener禁用。SwipeRefreshLayoutWebView

// 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);
      }
  }
});
于 2015-10-15T22:29:20.487 回答
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>

于 2014-07-24T03:34:31.043 回答
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

}
于 2014-12-27T00:18:03.140 回答
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 与空 textview

于 2015-01-13T07:09:54.890 回答
0

这就是我解决问题的方法,希望它会有所帮助:

mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if(mWebView.getScrollY() == 0){
                    mSwipeRefreshLayout.setEnabled(true);
                }else{
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });
于 2019-04-28T19:04:19.340 回答