【问题标题】:How to implement pull down refresh in android?如何在android中实现下拉刷新?
【发布时间】:2014-01-29 19:37:02
【问题描述】:

目前我正在研究一个片段,它只是一个带有框架布局的 webview,问题是,我想做一些类似 下拉刷新 的事情(就像列表视图的一些常见刷新功能) .

假设有一个refreshToDo()函数,我需要的只是一个布局(当我拖动正文时它显示刷新标题,当标题在一定高度时,它调用@987654324 @ ,当我释放它时,它会回到顶部并隐藏),但是如何实现呢?谢谢

layout:(它包含主要内容和目标内容,您可以简单地引入目标内容,用于在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>

片段:

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;
    }

如何添加自定义视图实现下拉刷新?谢谢

【问题讨论】:

标签: android android-layout android-fragments webview


【解决方案1】:

现在在 Android 支持库中有一个官方的小部件 SwipeRefreshLayout。它正在做你正在寻找的东西。文档在这里:

https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

这是我找到的一个很好的教程:

http://antonioleiva.com/swiperefreshlayout/

【讨论】:

    【解决方案2】:

    对于那些想要在 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>
    

    在您的 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);
     }
    

    例如,您可以设置一个 webView 客户端,当页面加载时您将取消刷新:

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView web, String url) {
            if (swipeLayout.isRefreshing()) {
                swipeLayout.setRefreshing(false);
            }
        }
    });
    

    【讨论】:

      【解决方案3】:

      查看很棒的ActionBar-PullToRefresh Library. 它使用 gmail/google+ 进度条执行拉动刷新手势,高度可配置,非常容易实现。

      【讨论】:

      • 不幸的是,这已被弃用。
      【解决方案4】:

      您可以使用 android 支持库中提供的SwipeRefreshLayout

      link 提供完整的示例代码示例和教程

      【讨论】:

        【解决方案5】:

        试试这个:LisView 自定义XListView-Android

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-01
          • 1970-01-01
          • 2015-11-28
          • 2022-06-23
          • 2018-05-14
          相关资源
          最近更新 更多