分享Android中pullToRefresh的使用心得_Android

pullToRefresh的导入

首先,点击new按钮 -> import Module

 

然后在 New Module界面选择已经在本地的含有源代码的pullToRefresh。

打开如下图所示的open Module Settings 按钮

 

点击app中的Dependencies 中右边框的"+"按钮,选择第三个 ,如下所示

 

选择Modules : pullToRefreshLibrary ,点击OK

 然后在build.gradle(Module:app)或者你自己要写的那个android 程序的根文件夹的build.gradle中加入下面一句话

 compile project(':pullToRefreshLibrary')

自此,pullToRefresh已经导入成功,可以新建一个pullToRefrenshListView验证一下。

pullToRefreshListView的基本使用

pullToRefreshListView和ListView的使用基本差的不多,只不过ListView的xml要换成

com.handmark.pulltorefresh.library.PullToRefreshListView

 例子如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <com.handmark.pulltorefresh.library.PullToRefreshListView
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/refresh_list_view"
    ptr:ptrDrawable="@drawable/default_ptr_flip"
    ptr:ptrAnimationStyle="flip"
    ptr:ptrHeaderBackground="#383838"
    ptr:ptrHeaderTextColor="#FFFFFF" >
  </com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout> 

上面的例子中pullToRefreshListView中多了几个属性 分别以ptr开头,这是指定pullToRefreshListView在刷新的时候出现的特效,比如第一个是指定刷新时显示的图片,第二个是指定刷新的图片以何种方式显示出来,第三个是指定刷新时头部的背景,第四个是指定刷新时头部字体的颜色。

以上这些都可以在代码中设置。

ListView中每个item的xml还是不变的,adapter的使用和写法也是不变的,需要改变的只有设定刷新事件。

接下来在代码中设定pullToRefreshListView的一些基本属性和事件。

步骤一 绑定控件,设置属性

绑定控件代码如下:

 private PullToRefreshListView listview;
listview = (PullToRefreshListView) findViewById(R.id.refresh_list_view); 

设置刷新时显示的刷新状态

 //对pullToListView绑定adapter
listview.setAdapter(adapter);
 /*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
listview.setMode(PullToRefreshBase.Mode.BOTH);
initRefreshListView();
initRefreshListView方法设置刷新显示的状态
 public void initRefreshListView() {
  ILoadingLayout Labels = listview.getLoadingLayoutProxy(true, true);
  Labels.setPullLabel("快点拉");
  Labels.setRefreshingLabel("正在拉");
  Labels.setReleaseLabel("放开刷新");
} 

这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态 。如果想区分上拉和下拉状态的不同,可以分别设置getLoadingLayoutProxy ,例子如下:

 public void initRefreshListView(){
  ILoadingLayout startLabels = pullToRefresh
        .getLoadingLayoutProxy(true, false);
  startLabels.setPullLabel("下拉刷新");
  startLabels.setRefreshingLabel("正在拉");
  startLabels.setReleaseLabel("放开刷新");
  ILoadingLayout endLabels = pullToRefresh.getLoadingLayoutProxy(
        false, true);
  endLabels.setPullLabel("上拉刷新");
  endLabels.setRefreshingLabel("正在载入...");
  endLabels.setReleaseLabel("放开刷新..."); 

这样pullToRefreshListView刷新时状态就设定好了。

步骤二 pullToRefreshListView监听事件的设置

这里主要设置setOnRefreshListener 事件,根据刚才设置的不同的刷新模式,在里面写的匿名内部类也不一样。

 规则如下:

 如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。 

如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。

当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.

这样在进入该Activity时候,手动上拉和下拉就会实现刷新和加载。

 如果想刚进入Activity的时候就执行加载,则要调用如下方法

 listview.setRefreshing();

接下来只需要在onPullDownToRefresh和onPullUpToRefresh 编写要获取listview新数据的方法。

 我这里的例子如下:

 listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
   @Override
   public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
      adapter.addToTop();
      new FinishRefresh().execute();
}
   @Override
   public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
      adapter.addToBottom();
      new FinishRefresh().execute();
}
}); 

我这里在自定义的adapter中写了2个新方法 addToTop 和addToBottom 分别在头部加入数据和在尾部加入数据
 方法如下:

private void addToTop() {
  for (int i = 0; i < 2; i++) {
     Item item = new Item();
     item.setText("在头部加入第" + i + "数据");
     item.setImageid(R.mipmap.ic_launcher);
     listItems.add(i, item);
  }
}
private void addToBottom() {
  for (int i = 0; i < 2; i++) {
     Item item = new Item();
     item.setText("在尾部加入第" + i + "数据");
     item.setImageid(R.mipmap.ic_launcher);
     listItems.add(item);
   }
} 

这里并没有考虑去重的问题,就是每次刷新结束后会显示出刷新的结果,当再次刷新后,又会执行和上次一样的结果,实际上,这是不符合逻辑的,当第二次在刷新的时候应该进行判断,如果数据一样就不把数据加入到list当中。

接下来 new FinishRefresh().execute(); 是这里我比较疑惑的一个固定写法,在这个com.handmark.pulltorefresh.library.PullToRefreshListView 框架下,执行onRefreshComplete();方法必须在异步下执行,不能和主进程一起执行,如果直接在下拉,上拉监听方法中写入onRefreshComplete(); 则在实际刷新中刷新状态的显示header是不会收回去的,换句话说 刷新一直不会完成。

所以要在继承AsyncTask的类下调用onRefreshComplete();

 private class FinishRefresh extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {

      }
      return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
      listview.onRefreshComplete();
      adapter.notifyDataSetChanged();
    }
  }

至此,pullToRefreshListview就实现了简单的上拉,下拉使用

步骤三 pullToRefresListView 的其他监听方法

关于步骤三今天时间有限,先给大家分享到这里,后续持续更新。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索pulltorefresh使用
, pulltorefresh的使用
android_pulltorefresh
androidpulltorefresh、android pullrefresh、pulltorefresh、pulltorefreshlayout、pullrefreshlistview,以便于您获取更多的相关知识。

时间: 2024-09-08 13:50:07

分享Android中pullToRefresh的使用心得_Android的相关文章

分享Android中Toast的自定义使用_Android

1.Toast源码分析 老规矩,我们先去看Toast的源码. Toast有两种显示布局方式,一种最常见调用Toast.makeText()  ,看源码是这样写的 public static Toast makeText(Context context, CharSequence text, @Duration int duration) { Toast result = new Toast(context); LayoutInflater inflate = (LayoutInflater) c

分享Android中ExpandableListView控件使用教程_Android

本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源.直接上代码如下: 程序结构图: layout目录下的 main.xml 文件源码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android

详解Android中Intent的使用方法_Android

一.Intent的用途 Intent主要有以下几种重要用途: 1. 启动Activity:可以将Intent对象传递给startActivity()方法或startActivityForResult()方法以启动一个Activity,该Intent对象包含了要启动的Activity的信息及其他必要的数据. 2. 启动Service:可以将Intent对象传递给startService()方法或bindService()方法以启动一个Service,该Intent对象包含了要启动的Service的

Android中AlertDialog用法实例分析_Android

本文实例分析了Android中AlertDialog用法,分享给大家供大家参考,具体如下: Android中AlertDialog为一些程序提供了对话框,有些功能能够进一步满足程序的需要.下面举例介绍. 程序如下: import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.

Android 中的注解深入探究_Android

本文系GDG Android Meetup分享内容总结文章 注解是我们经常接触的技术,Java有注解,Android也有注解,本文将试图介绍Android中的注解,以及ButterKnife和Otto这些基于注解的库的一些工作原理. 归纳而言,Android中的注解大概有以下好处 提高我们的开发效率 更早的发现程序的问题或者错误 更好的增加代码的描述能力 更加利于我们的一些规范约束 提供解决问题的更优解 准备工作 默认情况下,Android中的注解包并没有包括在framework中,它独立成一个

Android中SurfaceView用法简单实例_Android

本文实例讲述了Android中SurfaceView用法.分享给大家供大家参考,具体如下: 这里贴上一个小程序代码,主要运用SurfaceView来实现在屏幕上画一个圆,你可以通过按方向键和触摸屏幕来改变圆的位置 代码: Activity: package com.view; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowMa

Android中Notification用法实例总结_Android

本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本.现在我就把代码公布出

Android 中 Fragment的使用大全_Android

 Fragment必须总是被嵌入到一个Activity中,并且它的生命周期直接受宿主Activity生命周期的影响. 本文内容可以分为下面的几部分: 使用支持库 创建一个Fragment 创建一个动态UI 多个Fragment之间的通信 在一个Activity中,一个Fragment代表一种行为或者用户界面的一部分.你可以联合起来多个Fragment在一个Activity中创建多面板的UI,并且可以重用一个Fragment在多个activity中.你可以认为一个Fragment是一个Activi

Android中AlarmManager基本用法分析_Android

本文实例讲述了Android中AlarmManager基本用法.分享给大家供大家参考,具体如下: AlarmManager的作用文档中的解释是:在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent. 对应AlarmManager更深层的了解可以参考: http://www.jb51.net/article/90491.htm android提供了四种类型的闹钟: ① ELAPSED_REALT