package com.baway.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.GridView;
import android.widget.Toast;
import com.baway.adapter.MyAdapter;
import com.baway.vo.Data;
import com.baway.vo.Result;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
public class MainActivity extends Activity {
private PullToRefreshGridView mPullRefreshListView;
// 网址
private String URL = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=";
List<Data> list = new ArrayList<Data>();
private MyAdapter adapter;
int z = 242;
int p = 243;
private long firstTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
mPullRefreshListView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
// 刚进入初次加载
new GetDataTask().execute(URL + z);
// 监听
mPullRefreshListView
.setOnRefreshListener(new OnRefreshListener2<GridView>() {
// 下拉刷新加载
@Override
public void onPullDownToRefresh(
PullToRefreshBase<GridView> refreshView) {
Log.e("TAG", "onPullDownToRefresh"); // Do work to
// 刷新时间
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// 页数
z = z + 1;
// AsyncTask异步交互加载数据
new GetDataTask().execute(URL + z);
}
// 上拉加载跟多
@Override
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
Log.e("TAG", "onPullUpToRefresh"); // Do work to refresh
// 页数
p = p + 1;
// AsyncTask异步交互加载数据
new GetDataTask2().execute(URL + p);
}
});
}
//监听back按键,实现连续按两次退出功能
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
long secondTime = System.currentTimeMillis();
if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒,则不退出
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
firstTime = secondTime;//更新firstTime
return true;
} else { //两次按键小于2秒时,退出应用
System.exit(0);
}
break;
}
return super.onKeyUp(keyCode, event);
}
// 下拉刷新加载数据
private class GetDataTask extends AsyncTask<String, Integer, String> {
private String s;
@Override
protected String doInBackground(String... params) {
String url = params[0];
// 请求数据
s = NetWorkUtil.loginCheck_Get_HttpClient(url);
return s;
}
@Override
protected void onPostExecute(String result) {
// 解析
Gson gson = new Gson();
Result good = gson.fromJson(result, Result.class);
List<Data> data = good.getResult().getData();
list = data;
// 适配
adapter = new MyAdapter(MainActivity.this, list);
mPullRefreshListView.setAdapter(adapter);
// 刷新适配器
adapter.notifyDataSetChanged();
// 关闭刷新下拉
mPullRefreshListView.onRefreshComplete();
}
}
// 上拉加载更多
private class GetDataTask2 extends AsyncTask<String, Integer, String> {
private String s;
@Override
protected String doInBackground(String... params) {
String url = params[0];
// 请求数据
s = NetWorkUtil.loginCheck_Get_HttpClient(url);
return s;
}
@Override
protected void onPostExecute(String result) {
// 解析
Gson gson = new Gson();
Result good = gson.fromJson(result, Result.class);
List<Data> data = good.getResult().getData();
list.addAll(data);
// 刷新适配器
adapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
// 关闭上拉加载
mPullRefreshListView.onRefreshComplete();
}
}
}