码迷,mamicode.com
首页 > 其他好文 > 详细

PullToRefresh实现下拉刷新和上拉加载

时间:2015-05-09 10:22:11      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:android   上拉刷新   

一、导入Library
下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

技术分享

二、导入项目
1.新建工程后,右键->Properties->Android->Add 选择上面的Library
技术分享

2.main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#ffffff">

    <!-- The PullToRefreshListView replaces a standard ListView widget. -->
    <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="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</LinearLayout>

com.handmark.pulltorefresh.library.PullToRefreshListView 相当于ListView控件,用这段来代替原是ListView控件的代码

package com.example.fresh;

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.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.format.DateUtils;
import android.widget.ListView;

public class MainActivity extends Activity {
    private String[] mStrings = {"a","b","c","b","c","b","c","b","c","b","c","b","c","b","c","b","c","b","c","b","c"};

    private PullToRefreshListView mPullRefreshListView;
    private myPayedAdapter my_pay_adapter;

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

        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        mPullRefreshListView.setMode(Mode.BOTH);

        mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel(
                "上拉加载更多...");
        mPullRefreshListView.getLoadingLayoutProxy(false, true)
                .setRefreshingLabel("加载中...");
        mPullRefreshListView.getLoadingLayoutProxy(false, true)
                .setReleaseLabel("松开加载更多...");

        // Set a listener to be invoked when the list should be refreshed.
        mPullRefreshListView
                .setOnRefreshListener(new OnRefreshListener<ListView>() {
                    @Override
                    public void onRefresh(
                            PullToRefreshBase<ListView> refreshView) {

                            if (mPullRefreshListView.isHeaderShown()) {  
                                // Do work to refresh the list here.
                                new GetDataTask1().execute();
                            } else if (mPullRefreshListView.isFooterShown()) {  
                                // Do work to refresh the list here.
                                new GetDataTask2().execute();
                            } 

                    }
                });

        my_pay_adapter = new myPayedAdapter(this, mStrings);
        mPullRefreshListView.setAdapter(my_pay_adapter);

    }

    private class GetDataTask1 extends AsyncTask<Void, Void, String> {
        // 后台处理部分
        @Override
        protected String doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            String str = "Added after refresh...I add";
            return str;
        }

        // 这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
        // 根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
        @Override
        protected void onPostExecute(String result) {
            mStrings[0] = "aaaa";
            // 通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
            my_pay_adapter.notifyDataSetChanged();
            // Call onRefreshComplete when the list has been refreshed.
            mPullRefreshListView.onRefreshComplete();

            super.onPostExecute(result);// 这句是必有的,AsyncTask规定的格式
        }
    }
    private class GetDataTask2 extends AsyncTask<Void, Void, String> {
        // 后台处理部分
        @Override
        protected String doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            String str = "Added after refresh...I add";
            return str;
        }

        // 这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
        // 根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
        @Override
        protected void onPostExecute(String result) {
            mStrings[0] = "bbbbb";
            // 通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
            my_pay_adapter.notifyDataSetChanged();
            // Call onRefreshComplete when the list has been refreshed.
            mPullRefreshListView.onRefreshComplete();

            super.onPostExecute(result);// 这句是必有的,AsyncTask规定的格式
        }
    }
}

另外:

1、设置向上拉刷新还是向下拉刷新的代码:

mPullRefreshListView.setMode(Mode.PULL_FROM_END);//向下拉刷新

mPullRefreshListView.setMode(Mode.PULL_FROM_START);//向上拉刷新

mPullRefreshListView.setMode(Mode.BOTH);//两端刷新

2.上拉加载设置文字,这三行就是改变下边部分的文字

mExpandList.setMode(Mode.BOTH);  
mExpandList.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load));  
mExpandList.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading));  
mExpandList.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load));  

3.实现上拉的事件监听,只需要实现OnRefreshListener2就可以了,同时setMode(Mode.BOTH)

参考链接http://blog.csdn.net/harvic880925/article/details/17680305

PullToRefresh实现下拉刷新和上拉加载

标签:android   上拉刷新   

原文地址:http://blog.csdn.net/woaic__/article/details/45586679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!