使用 android-Ultra-Pull-To-Refresh 實現 WebView 下拉重新整理
阿新 • • 發佈:2019-02-02
Layout:
<?xml version="1.0" encoding="utf-8"?> <in.srain.cube.views.ptr.PtrClassicFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cube_ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/ptrFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent" cube_ptr:ptr_duration_to_close="200" cube_ptr:ptr_duration_to_close_header="1000" cube_ptr:ptr_keep_header_when_refresh="true" <--!是否保持重新整理頭-->
cube_ptr:ptr_pull_to_fresh="false" cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2" cube_ptr:ptr_resistance="1.7"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </in.srain.cube.views.ptr.PtrClassicFrameLayout>
Activity:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final PtrClassicFrameLayout ptrFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptrFrameLayout); final WebView webView = (WebView) findViewById(R.id.webView); webView.loadUrl("http://10.0.0.17:7000/mask"); final WebSettings webSettings = webView.getSettings(); //設定頁面支援JS webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //設定使用快取 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); ptrFrameLayout.setLastUpdateTimeRelateObject(this); ptrFrameLayout.setPtrHandler(new PtrHandler() { @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { //check 是否滾動到Html頂部,如果是則重新整理 return !content.canScrollVertically(-1); } @Override public void onRefreshBegin(PtrFrameLayout frame) { // 清空WebView的快取 webView.clearHistory(); webView.clearCache(true); // 延遲1秒重新載入webView 並結束重新整理 frame.postDelayed(new Runnable() { @Override public void run() { // 重新載入url webView.loadUrl("http://10.0.0.17:7000/mask"); // 完成重新整理 隱藏重新整理頭 ptrFrameLayout.refreshComplete(); } }, 1000); /** * 使用pullToRefreshListView的時候: * 如果資料重新整理速度過快,導致重新整理頭一直顯示不消失,延遲1秒左右呼叫onRefreshComplete方法即可 */ } }); } }