android下拉刷新

2018-09-02  本文已影响0人  ZemelZhu
  1. 导入依赖
compile 'in.srain.cube:ultra-ptr:1.0.11'
  1. 布局文件中引用
<in.srain.cube.views.ptr.PtrFrameLayout xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
                                        xmlns:android="http://schemas.android.com/apk/res/android"
                                        android:id="@+id/ptr"
                                        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="2.2"
                                        cube_ptr:ptr_resistance="1.7">

    <LinearLayout
        android:id="@+id/content_loading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="visible">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="加载中..."/>

        <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

    <ListView
        android:id="@+id/content_blog_list"
        android:dividerHeight="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</in.srain.cube.views.ptr.PtrFrameLayout>

  1. 获取控件
private PtrFrameLayout mPtrFrame;

mPtrFrame = (PtrFrameLayout) view.findViewById(R.id.ptr);//下拉刷新插件
  1. 自定义
        View view = View.inflate(context, R.layout.content_list, null);
        LinearLayout loading = (LinearLayout) view.findViewById(R.id.content_loading);
        blogList = (ListView) view.findViewById(R.id.content_blog_list);
        mPtrFrame = (PtrFrameLayout) view.findViewById(R.id.ptr);//下拉刷新插件
        loading.setVisibility(View.VISIBLE);

        final PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(context);
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, 0);
        mPtrFrame.setHeaderView(header);
        mPtrFrame.addPtrUIHandler(header);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                //当listview在第一个的时候才能下拉刷新
                if (blogList.getChildAt(0) != null) {
                    if (blogList.getChildAt(0).getTop() == 0) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPtrFrame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrame.refreshComplete();
                        getDataFromNet();
                        mPtrFrame.autoRefresh();//自动刷新
                    }
                }, 1800);
            }


        });
        loading.setVisibility(View.INVISIBLE);
        mPtrFrame.autoRefresh();
上一篇 下一篇

猜你喜欢

热点阅读