PullToRefresh刷新加载

原创 2016年05月31日 21:40:14

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

    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

PullToRefresh上拉加载,下拉刷新

//布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_wi...

PullToRefresh使用详解(三)--实现异步加载的下拉刷新列表

 前言:根据前几篇的阶段性成果(下拉刷新、异步加载),将其集成,就成了这篇文章,这篇文章代码量比较大,对于异步加载的部分,除了更改了getView()里绑定部分的代码,其它的都没有动,所以异步刷...

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

PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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