PullToRefreshGridView下拉刷新

来源:互联网 发布:rc522 stc源码 编辑:程序博客网 时间:2024/06/11 10:04
package com.baway.test;


import java.util.ArrayList;
import java.util.LinkedList;
import com.baway.test.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.app.Activity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;


public class MainActivity extends Activity {
private ArrayList<Beanlist> list = new ArrayList<Beanlist>();
private Myadapter adapter;
static final int MENU_SET_MODE = 0;
private LinkedList<String> mListItems;
private PullToRefreshGridView mPullRefreshGridView;
private GridView mGridView;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.gridview);
mGridView = mPullRefreshGridView.getRefreshableView();
getdata();
// Set a listener to be invoked when the list should be refreshed.
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);
// 模拟加载任务
getdata();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mPullRefreshGridView.onRefreshComplete();
}
});


mPullRefreshGridView
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
// 模拟加载任务
Toast.makeText(MainActivity.this, "正在加载",1).show();
getdata();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mPullRefreshGridView.onRefreshComplete();
}
});


}

private void getdata() {
int in_pn=0;
HttpUtils httpUtils = new HttpUtils();
httpUtils
.send(HttpMethod.GET,
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=10&pn="+in_pn,
new RequestCallBack<String>() {


@Override
public void onFailure(HttpException arg0,
String arg1) {


}


@Override
public void onSuccess(ResponseInfo<String> arg0) {

String result = arg0.result;
Gson gson = new Gson();
Bean fromJson = gson.fromJson(result,
Bean.class);
String pn = fromJson.result.pn;
String rn = fromJson.result.rn;
String totalNum = fromJson.result.totalNum;
ArrayList<Data> data = fromJson.result.data;
for (Data d : data) {
String bytime = d.bytime;
String img = d.img;
String reading = d.reading;
String sub1 = d.sub1;
String title = d.title;
list.add(new Beanlist(reading, bytime, title, img, sub1));
}
adapter = new Myadapter(
MainActivity.this, list);
mPullRefreshGridView.setAdapter(adapter);
}
});
}


}









    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:numColumns="3" 
        />
    




0 0
原创粉丝点击