Android开发之PullToRefreshGridView下拉刷新上拉加载

首先把PullToRefresh的第三方依赖库导入到工程中,并依赖它:(依赖库如下图)

布局文件:

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

    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="3"
        ptr:ptrMode="both" />

</LinearLayout>

 

下面是Activity的核心代码:

package com.bwie.main;

import java.util.List;

import com.bwie.adapter.JsonAdapter;
import com.bwie.bean.JsonResult;
import com.bwie.bean.Jsonarray;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

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

public class ItemActivitytwo extends Activity {
    private int index = 0;
    private String xmlid;
    private List<Jsonarray> list;
    private GridView gridView;
    private JsonAdapter adapter;
    private PullToRefreshGridView mPullRefreshGridView;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.itemactivity);

        Intent intent = getIntent();
        xmlid = intent.getStringExtra("lsid");
        // 获得PullToRefreshGridView
        mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
        gridView = mPullRefreshGridView.getRefreshableView();
        gethttp();
        // 实现上拉刷新与下拉加载
        mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2() {

            @SuppressWarnings("rawtypes")
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                gethttp();
            }

            @SuppressWarnings("rawtypes")
            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                gethttp();
            }

        });

    }

    private void gethttp() {
        // TODO Auto-generated method stub
        String url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
                + xmlid + "&pn=5&rn=" + index + "";
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub
                Log.i("TAG", "请求失败啦");
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                // gson解析
                Gson gson = new Gson();
                JsonResult fromJson = gson.fromJson(arg0.result,
                        JsonResult.class);
                list = fromJson.getResult().getData();
                Log.i("TAG", list.toString());
                adapter = new JsonAdapter(list, getApplicationContext());
                gridView.setAdapter(adapter);
                index = index++;
                // 适配器刷新
                adapter.notifyDataSetChanged();
                mPullRefreshGridView.onRefreshComplete();
            }
        });
    }

}

 

posted on 2016-05-04 21:02  小李子的博客园  阅读(1006)  评论(0编辑  收藏  举报