gpt4 book ai didi

java - SwipeRefreshLayout 与 Webview 不工作

转载 作者:行者123 更新时间:2023-11-29 06:28:43 25 4
gpt4 key购买 nike

这是我的带有 SwipeRefreshLayout 的 webview 应用程序代码。但是 SwipeRefresh 不工作请检查它。但所有其他事情都正常工作。像 webview,菜单按钮。

下面是我的activity_main.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/webView"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

这是我的 MainActivity.java 文件

    public class MainActivity extends AppCompatActivity {

WebView webView;
SwipeRefreshLayout swipe;

@Override
public boolean onCreateOptionsMenu (Menu manu){
getMenuInflater().inflate(R.menu.main, manu);
return true;
}

@Override
public boolean onOptionsItemSelected (MenuItem item){
int id=item.getItemId();
if(id==R.id.id_about){
Intent intentabout=new Intent(MainActivity.this, About.class);
startActivity(intentabout);

return true;
}
if(id==R.id.id_terms){

return true;
}
if(id==R.id.id_privacy){

return true;
}
return true;
}


@Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
LoadWeb();
}
});

LoadWeb();

}

public void LoadWeb(){
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webView.loadUrl("https://google.com");
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");

}

public void onPagefinished(WebView view, String url){
swipe.setRefreshing(false);
}
});
}

}

最佳答案

你必须将你的 WebView 放在 SwipeRefreshLayout 中:

public class MainActivity extends AppCompatActivity {

WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
String currentUrl = "https://www.google.co.in/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

webView.loadUrl(currentUrl);
webView.setWebViewClient(new MyWebViewClient());

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.loadUrl(currentUrl);
}
});
}

public class MyWebViewClient extends WebViewClient{

@Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
}
}

swipeRefreshLayout.setRefreshing(false) 停止动画。

关于java - SwipeRefreshLayout 与 Webview 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com