PullToRefreshListView的使用

PullToRefreshListView是Android-PullToRefresh开源项目中的一个自定义组件,Android-PullToRefresh是一个强大的拉动刷新开源项目,支持各种控件下拉刷新,ListView、ViewPager、WebView、ExpandableListView、GridView、ScrollView、Horizontal ScrollView、Fragment 上下左右拉动刷新。

今天就讲解PullToRefreshListView的使用,其他的组件会在接下来的博客中讲解。

开源地址:https://github.com/chrisbanes/Android-PullToRefresh

下载源码后,打开项目,如图

将上图的library导入Eclipse中,然后关联你的 项目,如果不会关联的话,可以到这里去看看http://blog.csdn.net/a_person_alone/article/details/50847823

好了,一切工作准备好了。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000"
    >
    <!-- 上述的xmlns:ptr="http://schemas.android.com/apk/res-auto"是必须的,下面的属性才可以用 -->
    <!--   替代ListView -->

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#ffffff"
        android:dividerHeight="1dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrHeaderTextColor="#ffffff"
      />

</LinearLayout>

xmlns:ptr=”http://schemas.android.com/apk/res-auto”是必须的,只有这样ptr:ptrHeaderTextColor才可以用,还有其他属性,可以在library中的attrs.xml中找到,其他属性没有什么解释的。如图

MainActivity文件代码,先全部贴出。

package com.zengfeng.pulltorefreshlistviewdemo;

import java.util.Arrays;
import java.util.LinkedList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    //数据
    private LinkedList<String> mListItems;
    //更新布局listView
    private PullToRefreshListView mPullRefreshListView;
    //适配器
    private ArrayAdapter<String> madapter;
    //数据
    private String[] mListItem={"Android","Java","Phthy","C#","PHP"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化mPullRefreshListView
        mPullRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list);

        //设置刷新更新监听方式
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            //跟新方法
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

                //设置下拉时的时间显示
                String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                //更新label显示
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                //进行异步更新
                new GetDataTask().execute();
            }
        });

        //设置滑动尾部的监听器
        mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {

                //滑动到底部是,提示已经到底部了
                Toast.makeText(MainActivity.this, "已经滑动到底部了", Toast.LENGTH_LONG).show();

            }
        });

        //刷新时是否允许滑动
        //mPullRefreshListView.isScrollingWhileRefreshingEnabled();

        //在刷新时允许继续滑动
        mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);

        //得到模式,有三种,分别是Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
        //mPullRefreshListView.getMode();

        //设置上下都可以刷新
        mPullRefreshListView.setMode(Mode.BOTH);

        //得到刷新的listView
        ListView actualListView=mPullRefreshListView.getRefreshableView();

        mListItems=new LinkedList<>();
        mListItems.addAll(Arrays.asList(mListItem));
        madapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mListItems);
        //设置适配器,下面两个效果相同
        //mPullRefreshListView.setAdapter(madapter);
        actualListView.setAdapter(madapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void>{

        @Override
        //后台执行任务
        protected Void doInBackground(Void... params) {

            try {
                 Thread.sleep(1000);
            } catch (Exception e) {

            }
            return null;
        }

         @Override
        protected void onPostExecute(Void result) {

             //得到刷新当前模式
            Mode mode=mPullRefreshListView.getCurrentMode();
            if(mode==Mode.PULL_FROM_START){
                //如果模式是开头刷新效果
                mListItems.addFirst("START Add");
            }else{
                //如果模式是尾部刷新效果
                mListItems.addLast("Later Add");
            }

            //通知数据已经改变了
            madapter.notifyDataSetChanged();
            //加载完以后停止刷新
            mPullRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }

    }

}

上述代码已经注释了,应该看懂是不难的。

测试效果图:

刷新前

好了,PullToRefreshListView的使用学会了,有什么疑问可以留言。

时间: 2024-08-06 16:00:48

PullToRefreshListView的使用的相关文章

PullToRefresh------ListView的使用

第一步 :写出布局文件的设置 <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000&q

Android PullToRefreshListView和ViewPager的结合使用

其实这个不是什么新东西了,在介绍(一)中我们就知道了PullToRefreshListView的用法,这里只要将其放入到ViewPager中就行啦.ViewPager还是和以往一样的定义和使用,在适配器中存视图的时候放入PullToRefreshListView就行. 1.ViewPager的布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml

PullToRefreshListView上拉加载、下拉刷新 eclipse项目

说明:此项目实在fragment中的,需要依赖library完成,还用到了Xuitls.jar包.使用了Pull解析XML package com.bwie.test.fragment; import java.io.ByteArrayInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List; import me.XListView; import org.xmlpull.v1.

PullToRefreshListView上拉加载、下拉刷新

说明:此项目使用studio完成的.需要导入library作为依赖,使用了xuitls获得网络请求.使用Pull解析了XML //布局文件 <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent

PullToRefreshListView 应用讲解

PullToRefreshListView 用法和ListView 没有什么区别  listview能用的属性 pulltorefresh也能用 我一直认为动手是最好的学习方法... 一:首先看布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" an

关于PullToRefreshListView的使用

方法比较简单,直接按照附件中的代码修改即可,附件代码来自互联网,经本人修改后综合,能实现上下拉都更新列表,其中需要注意的是:在project.properties中添加语句android.library.reference.1=../library,后部分为工程名,可以是library成为另一个工程作为库: 关于PullToRefreshListView的使用,布布扣,bubuko.com

95秀-PullToRefreshListView 示例

  正在加载.暂无数据页面 public class RefreshGuideTool { private RelativeLayout rl_loading_guide;//整个View private LinearLayout ll_loading_guide_parent;//无数据时显示的布局 private TextView tv_loading_guide;//什么都没有,下拉刷新 private RelativeLayout mPgLoaing;//正在加载时显示的布局 publi

Android学习笔记之PullToRefreshListView和BaseAdapter的使用

下拉刷新是很多应用都使用的很流行的一种效果,今天也算是彻底的理解了一下PullToRefreshListView的使用,但是弄了一天却在一个很傻的地方犯了错误. @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context

使用Android-PullToRefresh库中的PullToRefreshListView添加header时遇到的问题

在开发中我们常常有向ListView中添加header的需求,并且这个ListView还要提供下拉刷新的功能.下拉刷新库Android-PullToRefresh是个不错的下拉刷新库,不仅支持ListView,还支持GridView,WebView,ScrollView.但是当我想向PullToRefreshListViews中添加header的时候我并没有找到addHeaderView方法.后来发现PullToRefreshListViews并不是ListView的子类.他里边持有ListVi