package com.example.gridviewdemo;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.GridView;
import com.bwei.adapter.MyAdapter;
import com.bwei.vo.All;
import com.bwei.vo.Data;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;
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;
public class GridViewActivity extends Activity {
private PullToRefreshGridView mPullRefreshGridView;
private String url = "http://m.yunifang.com/yunifang/mobile/goods/getall?random=9949&encode=6c2154232994e7d36ad46le3caa68ca7";
private List<Data> data;
private List<Data> datas;
private MyAdapter adapter;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
XUtils();
/**
* 设置声音
*/
SoundPullEventListener<GridView> soundListener = new SoundPullEventListener<GridView>(
this);
soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
mPullRefreshGridView.setOnPullEventListener(soundListener);
/**
* 设置下拉上拉文字
*/
ILoadingLayout startLabels = mPullRefreshGridView
.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("你可劲拉,拉...");// 刚下拉时,显示的提示
startLabels.setRefreshingLabel("好嘞,正在刷新...");// 刷新时
startLabels.setReleaseLabel("你敢放,我就敢刷新...");// 下来达到一定距离时,显示的提示
ILoadingLayout endLabels = mPullRefreshGridView.getLoadingLayoutProxy(
false, true);
endLabels.setPullLabel("你可劲拉,拉2...");// 刚下拉时,显示的提示
endLabels.setRefreshingLabel("好嘞,正在刷新2...");// 刷新时
endLabels.setReleaseLabel("你敢放,我就敢刷新2...");// 下来达到一定距离时,显示的提示
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
/**
* 下拉的方法
* @param refreshView
*/
@Override
public void onPullDownToRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
handler.postDelayed(new Runnable() {
@Override
public void run() {
XUtils();
onLoad();
//必须关闭
mPullRefreshGridView.onRefreshComplete();
}
}, 2000);
}
/**
* 上拉的方法
* @param refreshView
*/
@Override
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getdataflush();
adapter.notifyDataSetChanged();
onLoad();
mPullRefreshGridView.onRefreshComplete();
}
}, 2000);
}
});
}
private void getdataflush() {
// TODO Auto-generated method stub
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
Gson gson = new Gson();
All all = gson.fromJson(arg0.result, All.class);
List<Data> d = all.getData();
datas.addAll(d);
}
});
}
private void XUtils() {
// TODO Auto-generated method stub
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.i("aaa", "===========");
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
Gson gson = new Gson();
All all = gson.fromJson(arg0.result, All.class);
data = all.getData();
datas = data;
adapter = new MyAdapter(GridViewActivity.this, datas);
mPullRefreshGridView.setAdapter(adapter);
}
});
}
private void onLoad() {
String label = DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
mPullRefreshGridView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
}
}