标签:
做这个需要自己去git hub上下载个pull-to-refresh 里面有个library为依赖包自己导到自己的项目中
(下载地址:https://github.com/chrisbanes/Android-PullToRefresh)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- The PullToRefreshGridView replaces a standard GridView widget. --> <com.handmark.pulltorefresh.library.PullToRefreshGridView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" ptr:ptrDrawable="@drawable/ic_launcher" ptr:ptrMode="both" /> </LinearLayout>
我们添加了一个属性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。
可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)
gridview中item布局
gridview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/image_src" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/image_src" android:text="TextView" /> </RelativeLayout>
MainActivity.java
package com.bawei.myxgridview; 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.widget.BaseAdapter; import android.widget.GridView; import com.bawei.vo.Good; import com.bawei.vo.MyData; 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://api.expoon.com/AppNews/getNewsList/type/1/p/"; List<MyData> list = new ArrayList<MyData>(); private BaseAdapter adapter; int z = 10; int p = 10; @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); } }); } // 下拉刷新加载数据 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(); Good good = gson.fromJson(result, Good.class); List<MyData> data = good.getData(); list = data; // 适配 adapter = new Adper(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(); Good good = gson.fromJson(result, Good.class); List<MyData> data = good.getData(); list.addAll(data); // 刷新适配器 adapter.notifyDataSetChanged(); // Call onRefreshComplete when the list has been refreshed. // 关闭上拉加载 mPullRefreshListView.onRefreshComplete(); } } }
请求数据NetWorkUtil.java
package com.bawei.myxgridview; import java.io.IOException; 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.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; public class NetWorkUtil { public static String loginCheck_Get_HttpClient(String url) { // TODO Auto-generated method stub String result = ""; HttpGet get = new HttpGet(url);//创建httpClient的get请求对象 //设置请求参数 HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5*1000); HttpConnectionParams.setSoTimeout(params, 5*1000); HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象 try { HttpResponse res = client.execute(get);//执行请求,获得结果 if(res.getStatusLine().getStatusCode() == 200){ HttpEntity entity = res.getEntity();//获得相应结果,是一个HttpEntity对象 result = EntityUtils.toString(entity, "utf-8");//转换为字符串 } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
VO类 两个
Good.java
package com.bawei.vo; import java.util.List; public class Good { private List<MyData> data; public Good(List<MyData> data) { super(); this.data = data; } @Override public String toString() { return "Good [data=" + data + "]"; } public List<MyData> getData() { return data; } public void setData(List<MyData> data) { this.data = data; } public Good() { super(); } }
MyData.java
package com.bawei.vo; public class MyData { private String news; private String news_summary; private String news_title; private String pic_url; public String getNews() { return news; } public void setNews(String news) { this.news = news; } public String getNews_summary() { return news_summary; } public void setNews_summary(String news_summary) { this.news_summary = news_summary; } public String getNews_title() { return news_title; } public void setNews_title(String news_title) { this.news_title = news_title; } public String getPic_url() { return pic_url; } public void setPic_url(String pic_url) { this.pic_url = pic_url; } @Override public String toString() { return "MyData [news=" + news + ", news_summary=" + news_summary + ", news_title=" + news_title + ", pic_url=" + pic_url + "]"; } public MyData(String news, String news_summary, String news_title, String pic_url) { super(); this.news = news; this.news_summary = news_summary; this.news_title = news_title; this.pic_url = pic_url; } public MyData() { super(); } }
适配器 Adper.java
package com.bawei.myxgridview; 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.bawei.vo.MyData; import com.lidroid.xutils.BitmapUtils; public class Adper extends BaseAdapter { Context context; List<MyData> list; public Adper(Context context, List<MyData> list) { // 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 arg0, View convertView, ViewGroup arg2) { // TODO Auto-generated method stub viewholder vh; if(convertView==null){ convertView=LayoutInflater.from(context).inflate(R.layout.gridview, null); vh=new viewholder(); vh.image=(ImageView) convertView.findViewById(R.id.image_src); vh.title=(TextView) convertView.findViewById(R.id.text_title); convertView.setTag(vh); }else{ vh=(viewholder) convertView.getTag(); } BitmapUtils bitmapUtils=new BitmapUtils(context); bitmapUtils.display(vh.image,list.get(arg0).getPic_url()); vh.title.setText(list.get(arg0).getNews_title()); return convertView; } class viewholder{ ImageView image; TextView title; } }
Android PullToRefresh (GridView 下拉刷新上拉加载)
标签:
原文地址:http://www.cnblogs.com/1426837364qqcom/p/5421368.html