PullToRefresh刷新加载

原创 2016年06月01日 09:21:16

Library下载地址:导入类库
https://github.com/chrisbanes/Android-PullToRefresh

一、PullToRefreshListView的实现

1.xml布局

<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" />

2、MainActivity.java核心代码

public class MainActivity extends Activity {

      // 刷新适配器之后停止动画
                    pull_refresh_list.onRefreshComplete();

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

    PullToRefreshListView   pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        //设置类型
        pull_refresh_list.setMode(Mode.BOTH);

        pull_refresh_list.setOnRefreshListener(new OnRefreshListener2<ListView>() {

        // 下拉Pulling Down
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
               /**
               *
               *下拉数据处理
               */ 
              。。。。。。。。
        }

        // 上拉Pulling Up
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
              /**
               *
               *上拉数据处理
               */ 
              。。。。。。。。
         }
  });


    }
}

**设置属性的几种参数类型
• Mode.BOTH:同时支持上拉下拉
• Mode.PULL_FROM_START:只支持下拉Pulling Down
• Mode.PULL_FROM_END:只支持上拉Pulling Up **

二、PullToRefreshGridView实现

1.xml布局

<com.handmark.pulltorefresh.library.PullToRefreshGridView
        android:id="@+id/gv_gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"  

         />

2、核心代码

package com.example.yuekao3;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.example.adapter.BookInfoAdapter;
import com.example.bean.BookInfoBean;
import com.example.bean.BookInfoBean.MyData;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.GridView;

public class BookInfoActivity extends Activity {

    private PullToRefreshGridView gv_gridview;
    private int pn;
    private String id;
    private ArrayList<MyData> list = new ArrayList<MyData>();
    private BookInfoAdapter bookInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_info);
        //找到控件
        gv_gridview = (PullToRefreshGridView) findViewById(R.id.gv_gridview);

        //设置同时支持上拉下拉
        gv_gridview.setMode(Mode.BOTH);
        // 异步请求数据

        BookInfoAsyncTask bookInfoAsyncTask = new BookInfoAsyncTask();
        bookInfoAsyncTask
                .execute("path");

        // 设置监听事件 刷新和加载
        gv_gridview.setOnRefreshListener(new OnRefreshListener2<GridView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
             /**
               *
               *下拉数据处理
               */ 
                list.clear();
                BookInfoAsyncTask bookInfoAsyncTask = new BookInfoAsyncTask();
                bookInfoAsyncTask.execute("path");

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                 /**
               *
               *上拉数据处理
               */ 
                BookInfoAsyncTask bookInfoAsyncTask = new BookInfoAsyncTask();
                bookInfoAsyncTask.execute("path");

            }
        });

    }

    /**
     * 异步请求类
     * @author Administrator
     *
     */
    public class BookInfoAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String strJson = getInfo(params[0]);
            return strJson;
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {

                Gson g = new Gson();
                BookInfoBean info = g.fromJson(result, BookInfoBean.class);
                list.addAll(info.result.data);
                if (bookInfoAdapter == null) {

                    bookInfoAdapter = new BookInfoAdapter(
                            BookInfoActivity.this, list);
                    gv_gridview.setAdapter(bookInfoAdapter);
                } else {

                    bookInfoAdapter.notifyDataSetChanged();
                }

                //设置刷新完成
                gv_gridview.onRefreshComplete();
            }

        }

        public String getInfo(String string) {
            try {
                URL url = new URL(string);

                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setReadTimeout(3000);
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(3000);
                int responseCode = urlConnection.getResponseCode();

                if (responseCode == 200) {
                    InputStream inputStream = urlConnection.getInputStream();

                    int len;
                    byte[] b = new byte[1024];
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                    while ((len = inputStream.read(b)) != -1) {
                        outputStream.write(b, 0, len);
                    }
                    System.out.println(outputStream.toString());
                    return outputStream.toString();
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    }
}

最终效果图

这里写图片描述
这里写图片描述

PullToRefresh引入依赖,布局,适配器ListView上拉加载下拉刷新

package com.example.wanghuantao170916; import android.os.AsyncTask; import android.support.v7.app.A...
  • thw_m
  • thw_m
  • 2017年09月16日 08:55
  • 133

Android实战——改造PullToRefresh下拉刷新和上拉加载

改造PullToRefresh下拉刷新和上拉加载 前言: 本篇文章主要分为两部分,一是PullToRefresh下拉刷新和上拉加载的使用,二是PullToRefresh的改造,用自定义一个动...

pulltorefresh上拉加载下拉刷新

  • 2017年05月17日 16:24
  • 1.32MB
  • 下载

pulltorefresh扩展下拉上拉刷新加载及监听listview滚动方向

很多android应用的下拉刷新都是使用的pulltorefresh这个开源项目,但是它的扩展性在下拉刷新同时又上拉加载更多时会有一定的局限性。查了很多地方,发现这个开源项目并不能很好的同时支持下拉刷...
  • fghydx
  • fghydx
  • 2014年07月10日 17:42
  • 1704

pulltorefresh(一)使用介绍,实现下拉刷新上拉加载更多

在Android开发中,上拉刷新,下拉加载在很多项目中被需求着,这个功能具体怎么实现呢,很简单,用一个第三方插件chrisbanes写的android-PullToRefresh库即可了,或者谷歌官方...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PullToRefresh刷新加载
举报原因:
原因补充:

(最多只允许输入30个字)