博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pulltorefresh
阅读量:5100 次
发布时间:2019-06-13

本文共 6946 字,大约阅读时间需要 23 分钟。

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;
}
}

 
   

转载于:https://www.cnblogs.com/nanze/p/5417068.html

你可能感兴趣的文章
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
Django中间件
查看>>
xcode 5.1安装vvdocument
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
MySQL更改默认的数据文档存储目录
查看>>
替代微软IIS强大的HTTP网站服务器工具
查看>>
6.5 案例21:将本地数据库中数据提交到服务器端
查看>>