use*_*104 6 android webview android-layout android-fragments
目前我正在研究一个片段,它只是一个带有framelayout的webview,问题是,我想做一些像下拉刷新的东西(就像列表视图常见的一些刷新功能).
假设有一个refreshToDo()函数,我需要的只是一个布局(当我拖动它显示刷新标题时,当标题位于特定高度时,它调用refreshToDo(),当我释放它时,它会返回顶部并隐藏),但是如何实现呢?谢谢
布局:(它包含主要内容和目标内容,您可以简单地插入目标内容,它用于在webview中全屏播放视频):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:id="@+id/target_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:visibility="gone" >
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
分段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.web_fragment, container,
false);
mTargetView = (FrameLayout) rootView.findViewById(R.id.target_view);
mContentView = (FrameLayout) rootView.findViewById(R.id.main_content);
mWebView = (WebView) rootView.findViewById(R.id.webView);
mWebView.setVerticalScrollBarEnabled(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.setWebViewClient(new MyWebViewClient(getActivity()));
mWebView.setWebChromeClient(new MyWebChromeClient(getActivity()));
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(this, "jsinterface");
// default go to video page
mWebView.loadUrl("file://" + getActivity().getFilesDir().toString()
+ StorageUtils.finalfoldername.toString() + "video_list.html");
return rootView;
}
Run Code Online (Sandbox Code Playgroud)
如何添加自定义视图以实现下拉刷新?谢谢
小智 10
现在Android支持库中有一个官方小部件SwipeRefreshLayout.它正在做你正在寻找的东西.文档在这里:
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
这是我发现的一个很好的教程:
http://antonioleiva.com/swiperefreshlayout/
对于那些想在webView中使用此功能的人!
在您的xml布局中,将您打包WebView成SwipeRefreshLayout:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
在您的Activity/Fragment类中:
//declare it as global var for future cancel refresh
private SwipeRefreshLayout swipeLayout;
//where you initialize your views:
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//your method to refresh content
}
});
//don't forget to cancel refresh when work is done
if(swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
Run Code Online (Sandbox Code Playgroud)
例如,您可以设置webView客户端,当页面加载时,您将取消刷新:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView web, String url) {
if (swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15800 次 |
| 最近记录: |