//布局
<?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()
}
}