xml:
<com.handmark.pulltorefresh.library.PullToRefreshGridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true"/> | |
adapter:
package com.baway.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.baway.test.R; import com.baway.utils.ImageUtils;import com.baway.vo.MyData;public class MyGridAdapter extends BaseAdapter{ private List<MyData> list; private Context context; public MyGridAdapter(List<MyData> list, Context context) { super(); this.list = list; this.context = context; } public void setList(List<MyData> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, null); viewHolder.textView = (TextView) convertView.findViewById(R.id.title); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView.setText(list.get(position).getTitle()); ImageUtils.getImage(list.get(position).getImg(), viewHolder.imageView); return convertView; } class ViewHolder{ TextView textView; ImageView imageView; }} | |
imageutils:
package com.baway.utils; import android.widget.ImageView; import com.baway.test.R; import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;public class ImageUtils { public static void getImage(String path,ImageView imageView){ DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.ic_launcher) .showImageOnFail(R.drawable.ic_launcher) .cacheInMemory(true) .cacheOnDisk(true) .build(); //ImageSize imageSize = new ImageSize(100, 100); ImageLoader.getInstance().displayImage(path, imageView, options); }} | |
app:
package com.baway.test; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import android.app.Application; public class App extends Application{ @Override public void onCreate() { ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); }} | |
activity.java
package com.baway.test; import java.util.ArrayList; import java.util.List;import android.app.Activity; import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.widget.GridView;import com.baway.adapter.MyGridAdapter; import com.baway.utils.HttpUtils;import com.baway.vo.MyData;import com.baway.vo.MyJson;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 ShowActivity extends Activity { private PullToRefreshGridView gridView; private List<MyData> list; private List<MyData> lists = new ArrayList<MyData>(); private MyGridAdapter adapter; private int id; private int index = 0; private Intent intent; Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: adapter = new MyGridAdapter(list, ShowActivity.this); gridView.setAdapter(adapter); break; case 1: adapter.setList(lists); adapter.notifyDataSetChanged(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); gridView = (PullToRefreshGridView) findViewById(R.id.gridView); intent = getIntent(); id = intent.getIntExtra("id", 0); new Thread(new Runnable() { @Override public void run() { String data = HttpUtils.getString("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="+id+"&pn=10&rn=10"); Gson gson = new Gson(); MyJson json = gson.fromJson(data, MyJson.class); list = json.getResult().getData(); handler.sendEmptyMessage(0); } }).start(); gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) { index++; new Thread(new Runnable() { @Override public void run() { String data = HttpUtils.getString("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="+id+"&pn="+index+"&rn=10"); Gson gson = new Gson(); MyJson json = gson.fromJson(data, MyJson.class); list = json.getResult().getData(); handler.sendEmptyMessage(0); } }).start(); }@Override public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { index++; new Thread(new Runnable() { @Override public void run() { String data = HttpUtils.getString("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="+id+"&pn="+index+"&rn=10"); Gson gson = new Gson(); MyJson json = gson.fromJson(data, MyJson.class); list = json.getResult().getData(); lists.addAll(list); handler.sendEmptyMessage(1); } }).start(); } }); }} | |
httputls:
package com.baway.utils; import java.io.IOException; import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;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;public class HttpUtils { public static InputStream send(){ InputStream in=null; try { URL url=new URL("http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml"); HttpURLConnection http = (HttpURLConnection) url.openConnection(); in = http.getInputStream(); return in; } catch (Exception e) { e.printStackTrace(); System.out.println(e); } return in; } public static String getString(String path){ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(path); try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(httpResponse.getEntity(),"UTF-8"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return path; }} | |