标签:
<com.handmark.pulltorefresh.library.PullToRefreshGridView
android:id="@+id/pull_refresh_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="fill"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/grid_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/grid_textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/grid_textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package com.bwie.yuekao;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.bwie.yuekao.adapter.MyAdapter;
import com.bwie.yuekao.bean.JsonBean;
import com.bwie.yuekao.bean.JsonBean.ListResult;
import com.bwie.yuekao.bean.JsonBean.MyDatas;
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 android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
public class SendActivity extends Activity {
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
String str = (String) msg.obj;
gridfata(str);
}
};
};
private String id;
private int pn = 1;
private int rn = 20;
private PullToRefreshGridView pull_refresh_grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
pull_refresh_grid = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
Intent intent = getIntent();
id = intent.getStringExtra("id");
getdata();
pull_refresh_grid
.setOnRefreshListener(new OnRefreshListener<GridView>() {
@Override
public void onRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间
getdata();
}
});
// Add an end-of-list listener //5.设置上拉加载处理
pull_refresh_grid
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
// TODO Auto-generated method stub
getdata();
Toast.makeText(getApplicationContext(), "正在加载", 1)
.show();
}
});
}
public void gridfata(String str) {
Gson gson = new Gson();
JsonBean fromJson = gson.fromJson(str, JsonBean.class);
ListResult result = fromJson.result;
List<MyDatas> data = result.data;
MyAdapter adapter = new MyAdapter(data, getApplicationContext());
GridView actualListView = pull_refresh_grid.getRefreshableView();
actualListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
pn++;
// Call onRefreshComplete when the list has been refreshed.
pull_refresh_grid.onRefreshComplete();
}
public void getdata() {
Toast.makeText(getApplicationContext(),
"pn = " + pn + " rn = " + rn, 0).show();
new Thread() {
@SuppressWarnings("deprecation")
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
HttpClient client = new DefaultHttpClient();
@SuppressWarnings("deprecation")
HttpGet httpGet = new HttpGet(
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
+ id + "&rn=" + rn + "&pn=" + pn);
try {
@SuppressWarnings("deprecation")
HttpResponse execute = client.execute(httpGet);
int statusCode = execute.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = execute.getEntity();
String json = EntityUtils.toString(entity);
Message msg = Message.obtain();
msg.what = 1;
msg.obj = json;
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package com.bwie.yuekao.adapter;
import java.util.List;
import com.bwie.yuekao.R;
import com.bwie.yuekao.bean.JsonBean.MyDatas;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
List<MyDatas> list;
Context context;
public MyAdapter(List<MyDatas> list, Context context) {
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.gridview_item, null);
viewHolder = new ViewHolder();
viewHolder.tv_title = (TextView) convertView
.findViewById(R.id.grid_textView1);
viewHolder.tv_reading = (TextView) convertView
.findViewById(R.id.grid_textView2);
viewHolder.tv_bytime = (TextView) convertView
.findViewById(R.id.grid_textView3);
viewHolder.img = (ImageView) convertView
.findViewById(R.id.grid_img1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_title.setText(list.get(position).title);
viewHolder.tv_reading.setText(list.get(position).reading);
viewHolder.tv_bytime.setText(list.get(position).bytime);
ImageLoader instance = ImageLoader.getInstance();
instance.displayImage(list.get(position).img, viewHolder.img);
return convertView;
}
class ViewHolder {
TextView tv_title, tv_reading, tv_bytime;
ImageView img;
}
}
标签:
原文地址:http://www.cnblogs.com/cuizhe/p/5448549.html