没有带有拉动刷新的互联网消息 webview 片段
Posted
技术标签:
【中文标题】没有带有拉动刷新的互联网消息 webview 片段【英文标题】:No internet message webview fragment with pull do to refresh 【发布时间】:2017-12-28 00:34:12 【问题描述】:我在这里搜索“没有互联网消息”的网页视图,我尝试了一些代码,但没有成功。我在片段中有一个 WebView,我想检查设备是否有互联网。如果是,webview 将加载。如果没有,将加载另一个片段,并且这个新片段将膨胀一个没有互联网消息的布局,并且这个布局将有一个刷新按钮(或下拉刷新)。那么,如何实现网络检查和刷新呢?
主要活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if(null == savedInstanceState)
// set you initial fragment object
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, InicioPagina.newInstance());
transaction.commit();
WebView 片段
public class InicioPagina extends Fragment
private ProgressBar prg;
SwipeRefreshLayout mySwipeRefreshLayout;
public WebView myWebView;
public static InicioPagina newInstance()
InicioPagina fragment = new InicioPagina();
return fragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.iniciopagina, container, false);
prg = (ProgressBar)rootView.findViewById(R.id.progressBar2);
mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipelayout);
myWebView = (WebView) rootView.findViewById(R.id.inicio_pagina);
myWebView.loadUrl("http://example.com.br/");
//*Ativar javascript
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//*Forçar links para abrir no WebView ao invés do navegador
myWebView.setWebViewClient(new WebViewClient());
myWebView.setVerticalScrollBarEnabled(false);
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
myWebView.reload();
mySwipeRefreshLayout.setRefreshing(false);
);
myWebView.setOnKeyListener(new View.OnKeyListener()
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
if(event.getAction() == KeyEvent.ACTION_DOWN)
WebView webView = (WebView) v;
switch(keyCode)
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
webView.goBack();
return true;
break;
return false;
);
return rootView;
public class WebViewClient extends android.webkit.WebViewClient
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
prg.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
@Override
public void onPageFinished(WebView view, String url)
prg.setVisibility(View.GONE);
super.onPageFinished(view, url);
内容主xml
<android.support.constraint.ConstraintLayout
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_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context="br.com.example.example.MainActivity">
<RelativeLayout
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/content"
android:layout_
android:layout_></FrameLayout>
</RelativeLayout>
非网络布局
<TextView
android:layout_
android:layout_
android:text="Sem acesso a internet"
android:textAlignment="center"
android:textSize="30sp" />
【问题讨论】:
【参考方案1】:您可以通过访问网络状态来检查互联网可用性:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
您可以通过这种方法检查互联网:
private boolean haveNetworkConnection()
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
return haveConnectedWifi || haveConnectedMobile;
前:
if (!haveNetworkConnection())
//inflate your fragment, THERE IS NO INTERNET!!
else
//you can reload or what ever you want now.
【讨论】:
我有一个错误:无法解析方法“getSystemService(java.lang.String)” @TonyStarkus 如果您在片段中使用,请使用 `getActivity() 我在 WebView 片段中使用了这个方法。我将“getSystemService”更改为“getActivity()”,现在错误出现在下一部分:“(Context.CONNECTIVITY_SERVICE);”出现错误 当我关闭互联网并打开应用程序时,我会强制关闭。在“如果”中,我只需输入“inflater.inflate(R.layout.nointernet, container, false);”【参考方案2】:在我的例子中,我使用 AlertDialogBox 来显示 Internet 错误并在重试按钮上检查 Internet 连接
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
//Clearing the WebView
try
webView.stopLoading();
catch (Exception e)
try
webView.clearView();
catch (Exception e)
if (webView.canGoBack())
webView.goBack();
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
finish();
startActivity(getIntent());
);
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialogInterface, int i)
finish();
);
alertDialog.setCancelable(false);
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
【讨论】:
以上是关于没有带有拉动刷新的互联网消息 webview 片段的主要内容,如果未能解决你的问题,请参考以下文章
拉动刷新后,带有 UiRefreshControl 的 Tableview 卡住了
Xamarin.Forms - 如果 webView 包含在 Android 上打开的模式或菜单,有没有办法禁用拉刷新?
listview 没有在带有 viewpager 选项卡的片段中刷新