I'm trying to add a simple swipe refresh to a webview layout. I have searched all over and tried different methods from things that I have found on GitHub and Android Developers pages but nothing seems to work. When I load the web page with the code without the OnRefresh method in the code it loads just fine. But as soon as I enable the method the page just closes the application. Any advice would be great as I'm new to android (1 yr exp) and trying to learn. Thanks
Here is a sample of the code I'm working with. Hope I don't post to much info every time I ask a question here I get told I'm posting too much!!
private AboutViewModel AboutViewModel;
SwipeRefreshLayout swipe;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root;
AboutViewModel = new ViewModelProvider(this).get(AboutViewModel.class);
root = inflater.inflate(R.layout.fragment_about, container, false);
WebView webView = root.findViewById(R.id.web_view_about);
webView.loadUrl("https://example.com");
webView.setWebViewClient(new WebViewController());
swipe = (SwipeRefreshLayout) container.findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
return root;
}
This is the layout xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@ id/web_view_about"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
CodePudding user response:
Replace container with root like below:
swipe = root.findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
swipe.setRefreshing(false); // Call this at the end of loading
}
});