关闭

PullToRefresh刷新加载

144人阅读 评论(0) 收藏 举报

Library下载地址:导入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核心代码

@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);
        // 设置初期数据
        initData();
        pull_refresh_list.setOnRefreshListener(new OnRefreshListener2<ListView>() {

        // 下拉Pulling Down
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
               /**
               *
               *下拉的时候数据重置
               */ 
               mList_all.clear();
              initData();
        }

        // 上拉Pulling Up
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
              /**
               *
               *上拉的时候数据重置
               */   
               ............
         }
  });

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

二、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:BookInfoActivity.java核心代码

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;
        }

    }
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4450次
    • 积分:318
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条