PullToRefreshListView用法

本文介绍了一个基于 Android 的 PullToRefresh 组件的使用方法。该组件允许用户通过下拉列表视图来触发刷新操作,并且可以自定义刷新时的提示文字。文中详细展示了如何设置组件属性、配置监听器以及实现异步数据加载。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/refresh_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrDrawable="@drawable/default_ptr_flip"
        ptr:ptrHeaderBackground="#383838"
        ptr:ptrHeaderTextColor="#FFFFFF"


        />

</RelativeLayout>



package com.example.a16_pulltorefresh_demo;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {


    private PullToRefreshListView refreshListView;
    private List<String> list = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private ILoadingLayout startLabels;
    private ILoadingLayout endLabels;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        refreshListView = (PullToRefreshListView) findViewById(R.id.refresh_list_view);


        //获取数据,,,设置适配器


        getData();




        //设置刷新时显示的刷新状态
        /**设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,
        PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
        refreshListView.setMode(PullToRefreshBase.Mode.BOTH);


        /**
         * 设置下拉和上拉的时候显示的文字
         */
        //通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的文字的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
        startLabels = refreshListView.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新");
        startLabels.setRefreshingLabel("正在刷新...");
        startLabels.setReleaseLabel("放开刷新");


        endLabels = refreshListView.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新");
        endLabels.setRefreshingLabel("正在载入...");
        endLabels.setReleaseLabel("放开刷新...");


        //设置上拉下拉的监听事件
        /**
         * 当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只
         * 调用onPullDownToRefresh()方法,
         * Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.
         */
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉的时候调用
            @Override
            public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {
                //加载数据(异步的操作)....数据完成之后,取消刷新
                //执行onRefreshComplete();方法必须在异步下执行,不能和主进程一起执行
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... voids) {


                        try {
                            //表示两秒钟请求到了数据
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }


                        return null;
                    }


                    @Override
                    protected void onPostExecute(Void aVoid) {


                        getData();


                        //下拉刷新设置刷新的时间
                        startLabels.setLastUpdatedLabel("上次刷新:2017-09-14");


                        //停止刷新
                        refreshView.onRefreshComplete();


                        super.onPostExecute(aVoid);
                    }
                }.execute();
            }


            //上拉的时候调用
            @Override
            public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {


                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... voids) {


                        try {
                            //表示两秒钟请求到了数据
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }


                        return null;
                    }


                    @Override
                    protected void onPostExecute(Void aVoid) {


                        getData();


                        endLabels.setLastUpdatedLabel("上次加载时间:09-15");


                        //停止刷新
                        refreshView.onRefreshComplete();


                        super.onPostExecute(aVoid);
                    }
                }.execute();
            }
        });


    }


    private void getData() {
        for (int i = 0;i<10;i++){
            list.add("这是条目"+i);
        }




        if (adapter == null){


            adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,list);


            refreshListView.setAdapter(adapter);


        }else {
            adapter.notifyDataSetChanged();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值