Android SwipeRefreshLayout не позволяет прокрутку WebView - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь реализовать Android SwipeRefreshLayout в моем приложении WebView. Когда я пролистываю вниз, он обновляет страницу, но больше не позволяет прокручивать вверх и вниз, за ​​исключением пролистывания.

Я попробовал какое-то базовое исправление, которое я нашел в Интернете, но оно все еще не работает.

private WebView mywebview, handshakemywebview;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mywebview = findViewById(R.id.MainWebInterface);
    handshakemywebview = findViewById(R.id.HandShakeWebInterface);
    mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);

    mywebview.loadUrl("https://example.com");
    mySwipeRefreshLayout.setOnRefreshListener(
       new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
             shopwebview.reload();
          }
       }
    );
}



@Override
protected  void onRestart(){
    super.onRestart();
    mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
       @Override
       public void onScrollChanged() {
          if (mywebview.getScrollY() == 0) {
             mySwipeRefreshLayout.setEnabled(true);
          } else {
            mySwipeRefreshLayout.setEnabled(false);
          }
       }
    });
}

@Override
protected void onPause(){
   super.onPause();
  mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
}

Макет XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:contentDescription="@string/activityLabel"
            android:visibility="visible"
            android:id="@+id/MainWebInterface"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <WebView
        android:id="@+id/HandShakeWebInterface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@color/colorPrimary"
        android:visibility="gone" />

</RelativeLayout>

Ответы [ 2 ]

0 голосов
/ 15 октября 2019

Добавить NestedScrollView в swiperefreshlayout. Вы можете обернуть веб-просмотр в макет Swipe refesh, как это

  <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <WebView
                    android:id="@+id/MainWebInterface"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="@color/colorPrimary"
                    android:contentDescription="@string/activityLabel"
                    android:visibility="visible" />

            </androidx.core.widget.NestedScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
0 голосов
/ 15 октября 2019

Написать код OnRestart в onResume. Это потому, что вы добавляете addOnScrollChangedListener при перезапуске, а onRestart не называется Always

...