我正試圖為一個webview布局添加一個簡單的重繪 功能。我在GitHub和Android開發者頁面上搜索并嘗試了不同的方法,但似乎都不奏效。當我用代碼加載網頁時,如果代碼中沒有OnRefresh方法,它就能正常加載。但是當我啟用該方法時,頁面就會關閉應用程式。任何建議都很好,因為我是安卓新手(1年的經驗),正在努力學習。謝謝
。這是我正在使用的代碼的一個例子。希望我沒有發布太多的資訊,每次我在這里問問題的時候,都會被告知我發布的資訊太多!
在這里,我希望我沒有發布太多的資訊。
private AboutViewModel AboutViewModel。
SwipeRefreshLayout swipe。
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
視圖根。
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。
}
這就是布局xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:id="@ id/swipe"。
android:layout_width="match_parent"/span>
android:layout_height="match_parent">
<WebView
android:id="@ id/web_view_about"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
uj5u.com熱心網友回復:
用root替換容器,如下:
swipe = root.findViewById(R.id.swipe)。
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload()。
swipe.setRefreshing(false); //在加載結束時呼叫這個。
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/306692.html
標籤:
上一篇:與launchMode混淆
