PullToRefresh上拉加载,下拉刷新

本文介绍了一个名为畅读书城的应用布局实现方式及数据加载逻辑,通过PullToRefreshGridView实现了上下拉刷新功能,并使用Gson进行JSON解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="9">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="畅读书城"
            android:textSize="30sp"/>

    </RelativeLayout>

    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        android:layout_weight="1" 
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        ptr:ptrMode="both"       
        ></com.handmark.pulltorefresh.library.PullToRefreshGridView>
</LinearLayout>
package com.example.location;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

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

import com.example.adapter.MyBookAdapter;
import com.example.bean.A;
import com.example.bean.C;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
public class InfoActivity extends Activity{
    String id;
    PullToRefreshGridView gridview;
    String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182";
    List<A> list_push=new ArrayList<A>();
    int index=0;

    Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg){
            if(msg.what==1){
                     List<A> ldata = (List<A>) msg.obj;                 
                    gridview.setAdapter(new MyBookAdapter(InfoActivity.this,ldata));
                    gridview.onRefreshComplete();
            }
            if(msg.what==2){
                    List<A> ldata = (List<A>) msg.obj;
                    gridview.setAdapter(new MyBookAdapter(InfoActivity.this,ldata));
                    gridview.onRefreshComplete();   
            }
            if(msg.what==3){
                List<A> ldata = (List<A>) msg.obj;
                list_push.addAll(ldata);
                gridview.setAdapter(new MyBookAdapter(InfoActivity.this,list_push));
                gridview.onRefreshComplete();
                gridview.setAdapter(new MyBookAdapter(InfoActivity.this,list_push));
                new MyBookAdapter(InfoActivity.this,list_push).notifyDataSetChanged();              
            }
        };
    };




    @Override
    protected void onCreate(Bundle savedInstanceState){
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infoactivity);
        gridview=(PullToRefreshGridView) findViewById(R.id.gridview);

        Intent intent=getIntent();
        id=intent.getStringExtra("id");     
        getdata(1);


        gridview.setOnRefreshListener(new OnRefreshListener2<GridView>(){
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView){
                // TODO Auto-generated method stub
                index=0;
                getdata(2);
            }
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView){
                // TODO Auto-generated method stub
                index++;
                getdata(3);
            }
        });



    }

    private void getdata(int what){
        new Thread(){
            public void run(){
                HttpClient httpclient=new DefaultHttpClient();
                HttpGet httpget=new HttpGet(url+"&catalog_id=+"+id+"&pn="+index+"&rn=20");
                try {
                    HttpResponse execute=httpclient.execute(httpget);
                    if(execute.getStatusLine().getStatusCode()==200){
                        HttpEntity entity = execute.getEntity();
                        String string=EntityUtils.toString(entity, "utf-8");
                        Gson gson=new Gson();
                        C c=(C) gson.fromJson(string, C.class);
                        List<A> list=c.getResult().getData();
                        if(list!=null){
                            handler.sendMessage(handler.obtainMessage(1, list));
                        }
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值