package com.baway.test;
import java.util.List;
import com.baway.adapter.MyBaseAdapter;
import com.baway.utils.NetWork;
import com.baway.vo.Mydata;
import com.baway.vo.Supper1;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
private String url="http://m.yunifang.com/yunifang/mobile/goods/getall?random=9949&encole=6c2154232994e7d36ad461e3caa68ca7";
private PullToRefreshGridView mPullRefreshGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
new Myasnktask().execute(url);
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener2<GridView>()
{
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);
new Myasnktask().execute(url);
}
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView)
{
Log.e("TAG", "onPullUpToRefresh"); // Do work to refresh
// the list here.
new Myasnktask().execute(url);
}
});
}
class Myasnktask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String ss=params[0];
String s=NetWork.httpclient_get(ss);
return s;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Gson gson=new Gson();
Supper1 supper1=gson.fromJson(result, Supper1.class);
final List<Mydata> list=supper1.getData();
mPullRefreshGridView.setAdapter(new MyBaseAdapter(list, getApplicationContext()));
mPullRefreshGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,NewActivity.class);
intent.putExtra("id", list.get(position).getId());
startActivity(intent);
}
});
mPullRefreshGridView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
xml里面的布局文件;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrMode="both" />
/>
</RelativeLayout>