GridView的刷新

来源:互联网 发布:域名可以干嘛 编辑:程序博客网 时间:2024/06/05 02:22
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" />

0 0
原创粉丝点击