package com.bwie.text;
import java.util.ArrayList;
import com.bwie.text.Bean.Result.Data;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
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.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.GridView;
import android.widget.Toast;
public class SecondActivity extends Activity {
private PullToRefreshGridView mPullRefreshGridView;
private GridView gridView;
private int start;
private MyAdapters adapters;
private String id;
boolean flag=true;
private ArrayList<Data> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
id = intent.getStringExtra("id");
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
// 上拉加載
gridView = mPullRefreshGridView.getRefreshableView();
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener<GridView>() {
@Override
public void onRefresh(
PullToRefreshBase<GridView> refreshView) {
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// 顯示最後更新的時間
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// 模拟加載任務
getInfo();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mPullRefreshGridView.onRefreshComplete();
}
});
// 下拉重新整理
mPullRefreshGridView
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@SuppressLint("ShowToast")
@Override
public void onLastItemVisible() {
// 模拟加載任務
Toast.makeText(SecondActivity.this, "正在加載", 1).show();
getInfo();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mPullRefreshGridView.onRefreshComplete();
}
});
getInfo();
}
private void getInfo() {
// 擷取網址
String url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="+id+"&pn="
+ start + "&rn=20";
// 解析
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
start += 10;
// 擷取Json串
String result = arg0.result;
// 使用Gson解析
Gson gson = new Gson();
Bean bean = gson.fromJson(result, Bean.class);
if(adapters==null){
list = bean.result.data;
adapters=new MyAdapters(SecondActivity.this, list);
gridView.setAdapter(adapters);
}else{
ArrayList<Data> list2 = bean.result.data;
for (Data data : list2) {
list.add(data);
}
adapters.notifyDataSetChanged();
}
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
if(flag){
Toast.makeText(SecondActivity.this, "再按一次退出", 0).show();
flag=false;
}else{
finish();
}
}
}
//布局
<com.handmark.pulltorefresh.library.PullToRefreshGridView
android:id="@+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="3" />