【问题标题】:SwipeRefreshLayout Tab layout. Webview can't scroll upSwipeRefreshLayout 选项卡布局。 Webview 无法向上滚动
【发布时间】:2014-07-24 00:01:30
【问题描述】:

好的,所以我有一个 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);
                    }
                }});

【问题讨论】:

    标签: android webview swiperefreshlayout


    【解决方案1】:

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

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

    【讨论】:

    • 像魅力一样工作
    • 非静态方法'setEnabled(boolean)'
    • @bhupathi-turaga,此方法与 Java 中的许多其他方法一样,无法从静态上下文中调用。您需要重构代码以将命令移动到可以直接访问SwipeRefreshLayout 的非静态上下文。
    • 我怎样才能使动态
    • 解决问题的方式很大程度上取决于您的代码究竟是什么样的。你应该用你的代码相关部分的 sn-ps 提出一个新问题。
    【解决方案2】:

    完成这项工作非常具有挑战性。但是,可以对布局 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>
    

    【讨论】:

      【解决方案3】:

      你可以让一个类扩展 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
      
      }
      

      【讨论】:

        【解决方案4】:

        如果您使用的是ViewPager 并且 tabs 是 Fragments,您可以使用以下 sn-p 修复您的 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 中的更多信息。

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

          • 单视图工作正常,但如果网站有任何弹出滚动则无法正常工作。
          猜你喜欢
          • 2017-08-22
          • 1970-01-01
          • 1970-01-01
          • 2015-10-03
          • 1970-01-01
          • 2018-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多