我将为 webview 制作一个下拉刷新选项,我得到 xml 代码及其工作,但我不知道如何使用java使其正常工作,我正在使用app抽屉活动,这是我的第一个片段(代码中的home_fragment)
这是 xml 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fb.jaisonjoseph.facebookbasic.Home_Fragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat"
android:indeterminate="false"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这是我在Home_Fragment.java中的java代码
package com.fb.jaisonjoseph.facebookbasic;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.view.KeyEvent;
/**
* A simple {@link Fragment} subclass.
*/
public class Home_Fragment extends Fragment {
public ProgressBar bar;
SwipeRefreshLayout mySwipeRefreshLayout;
public WebView mwebView;
public Home_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home_, null);
final WebView view=(WebView) rootView.findViewById(R.id.webView);
view.loadUrl("https://mbasic.facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
return rootView;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
}
}
答案 0 :(得分:1)
首先在.Java文件中声明swiperefresh。
SwipeRefreshLayout swiperefresh=(SwipeRefreshLayout) findViewById(R.id.swiperefresh);
然后你需要使用setOnRefreshListener,所以每当刷新布局时,都会调用setOnRefreshListener
swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// code to refresh
swiperefresh.setRefreshing(false); //code to stop refresh animation
}
});
答案 1 :(得分:0)
禁用WebView
中的垂直滚动,将其'height'属性设置为'wrap_content'
。通过这样做,WebView
将占据整个网页的高度。然后将WebView
添加到ScrollView
,然后将其放入SwipeRefreshLayout
。
答案 2 :(得分:0)
将其添加到onCreateView()方法
中mySwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.swiperefresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// logic to refresh then add the following line to stop refresh animation
mSwipeRefreshLayout.setRefreshing(false);
}
});