PullToRefresh实现上拉加载下拉刷新

欢迎光临老邢的Android教程,今天为大家带来的是如何在Android Studio中使用第三方库PullToRefresh的使用。

前言
使用的是Android Studio2.2.2

目录

  1. 导入Library库
  2. 引用Library库
  3. 布局中使用PullToRefreshListView控件
  4. Activity中设置PullToRefreshListView

效果图:
下拉刷新

下拉刷新

上拉加载
上拉加载

1、导入

首先下载Library库,下载地址:https://github.com/chrisbanes/Android-PullToRefresh
将下载的Library库import Module到AS的项目中,加载之后注意要修改grade文件的编译版本
下面为我的Library库的grade文件:

Library库的grade:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

注意:.PullToRefresh支持以下几种控件
1.PullToRefreshListView,效果类似ListView
2.PullToRefreshExpandableListView,效果类似ExpandableListView
3..PullToRefreshGridView,效果类似GridView
4..PullToRefreshWebView,效果类似WebView
5.PullToRefreshScrollView,效果类似ScrollView


2、在Module中引用

选择对应的Module右键 open Module Setting点击右边的Dependencies,点击加号,选择第三个选择对应的Library库,就可以啦。
或者在当前的Module的grade文件中直接添加:
compile project(‘:Library库名称’)

3、使用PullToRefreshListView

在对应的布局中使用对应的标签

布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_pull_to_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.frxm.myhight.PullToRefreshActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_horizontal"
        android:text="暂无数据,请下拉"
        android:id="@+id/tv_empty"/>
<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ptr:ptrAnimationStyle="flip"
    android:id="@+id/plv">

</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>

自定义属性说明:
attr:ptrDrawable=“” 上拉下拉图标
attr:ptrAnimationStyle=”” 图标动画 取值: flip:翻转 rotate旋转
attr:ptrHeaderBackground=”” 上拉下拉时 头部的背景色 attr:ptrHeaderTextColor=”” 上拉下拉时 文字颜色
注:attr就是一个自定义命名的本地节点名称引用对应的命名空间 xmlns:attr=”http://schemas.android.com/apk/res-auto”

4、Activity中设置PullToRefreshListView

在Activity中完成创建PullToRefreshListView对象,并设置刷新模式和刷新事件

代码如下
package org.frxm.myhight;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.LinkedList;
/*使用PullToRefreshListView*/
public class PullToRefreshActivity extends AppCompatActivity
implements PullToRefreshBase.OnRefreshListener2{

    //声明UI控件和集合对象
    private TextView tvEmpty;
    private PullToRefreshListView plv;
    private LinkedList<String> list;
    private ArrayAdapter<String> adapter;
    private Handler handler;
    private int no;//序号
    private final int DOWN=0,UP=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh);
        init();
    }
    //初始化
    private void init() {
        //完成UI的实例化和集合、适配器的实例化
     plv=(PullToRefreshListView)findViewById(R.id.plv);
        tvEmpty=(TextView)findViewById(R.id.tv_empty);
        list=new LinkedList<>();
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        plv.setAdapter(adapter);
        plv.setEmptyView(tvEmpty);
        //获取下拉刷新信息设置对象
        //设置下拉的刷新信息
        ILoadingLayout layDown=plv.getLoadingLayoutProxy(true,false);
        layDown.setPullLabel("下拉刷新");
        layDown.setRefreshingLabel("正在刷新数据");
        layDown.setReleaseLabel("松开刷新");
        //设置上拉的加载信息
        ILoadingLayout layUp=plv.getLoadingLayoutProxy(false,true);
        layUp.setPullLabel("上拉加载");
        layUp.setReleaseLabel("正在加载下一页");
        layUp.setRefreshingLabel("松开加载");
        //设置模式
        plv.setMode(PullToRefreshBase.Mode.BOTH);
        //设置刷新事件
        plv.setOnRefreshListener(this);
        //实例化Handler对象
        handler=new Handler();
    }
    //加载数据
    private void loadData(final int type) {
        //加载数据,
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                switch (type)
                {
                    case DOWN://下拉刷新数据,每次刷新5条
                        for (int i = no+5,index=0; i >no ; i--,index++) {
                            list.add(index,"今天雾霾很严重+"+i);
                        }
                        no+=5;
                        adapter.notifyDataSetChanged();
                        break;
                    case UP://上拉加载数据,每次加载10条
                        for (int i = no+1; i <=no+10 ; i++) {
                            list.add("今天吃"+i+"个馒头");
                        }
                        no+=10;
                        adapter.notifyDataSetChanged();
                        break;
                }
                //刷新结束
                plv.onRefreshComplete();
            }
        },3000);
    }
    //下拉刷新
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData(DOWN);
    }
    //上拉加载
    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        loadData(UP);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值