PullToRefresh上啦刷新下拉加载
来源:互联网 发布:淘宝对比价格的软件 编辑:程序博客网 时间:2024/04/30 11:45
//首先导入libary包然后build版本更改ImageLoader包导入Gson包导入写入MyApp类
package com.example.zhangshaohang1511b20171117;import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.util.List;public class MainActivity extends AppCompatActivity { private PullToRefreshListView lv; private TextUtils utils; private static final String JSON_URL = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1"; private int peaNam=1; private List<SuperClass.ResultsBean> results; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { peaNam=1; new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String str=utils.getJsonget(JSON_URL+peaNam); return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); SuperClass fromJson = gson.fromJson(s, SuperClass.class); results = fromJson.getResults(); adapter = new MyAdapter(results,MainActivity.this); lv.setAdapter(adapter); lv.onRefreshComplete(); } }.execute(); } }); lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() { @Override public void onLastItemVisible() { peaNam++; new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String str=utils.getJsonget(JSON_URL+peaNam); return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); SuperClass fromJson = gson.fromJson(s, SuperClass.class); List<SuperClass.ResultsBean> aresults = fromJson.getResults();// MyAdapter adapter = new MyAdapter(aresults,MainActivity.this);// lv.setAdapter(adapter); results.addAll(aresults); adapter.notifyDataSetChanged(); } }.execute(); } }); } private void initView() { //找到控件 lv = (PullToRefreshListView) findViewById(R.id.lv); utils = new TextUtils(); new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String str=utils.getJsonget(JSON_URL); return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); SuperClass fromJson = gson.fromJson(s, SuperClass.class); List<SuperClass.ResultsBean> results = fromJson.getResults(); MyAdapter adapter = new MyAdapter(results,MainActivity.this); lv.setAdapter(adapter); } }.execute(); }}//网络请求类
package com.example.zhangshaohang1511b20171117;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;/** * Created by 心所在梦就在 on 2017/11/17. */public class TextUtils { private static final String JSON_URL = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1"; public String getJsonget(String param){ String data = ""; try { URL url = new URL(param); HttpURLConnection Conn = (HttpURLConnection) url.openConnection(); Conn.setConnectTimeout(5000); Conn.setReadTimeout(5000); int responseCode = Conn.getResponseCode(); if (responseCode==200){ InputStream inputStream=Conn.getInputStream(); ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); byte[] b=new byte[1024]; int len=-1; while ((len=inputStream.read(b))!=-1){ outputStream.write(b, 0, len); } data = outputStream.toString(); } } catch (Exception e) { e.printStackTrace(); } return data; }}//适配器
package com.example.zhangshaohang1511b20171117;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by 心所在梦就在 on 2017/11/16. */public class MyAdapter extends BaseAdapter { List<SuperClass.ResultsBean> list; Context context; public MyAdapter(List<SuperClass.ResultsBean> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; View view; if (convertView==null){ holder=new ViewHolder(); view= View.inflate(context,R.layout.item,null); holder._id = (TextView) view.findViewById(R.id._id); holder.createdAt = (TextView) view.findViewById(R.id.createdAt); holder.desc = (TextView) view.findViewById(R.id.desc); holder.publishedAt = (TextView) view.findViewById(R.id.publishedAt); holder.source = (TextView) view.findViewById(R.id.source); holder.type = (TextView) view.findViewById(R.id.type); holder.url = (TextView) view.findViewById(R.id.url); holder.who = (TextView) view.findViewById(R.id.who); holder.img = (ImageView) view.findViewById(R.id.img); view.setTag(holder); }else{ view=convertView; holder= (ViewHolder) view.getTag(); } SuperClass.ResultsBean bean = list.get(position); holder._id.setText(list.get(position).get_id()); holder.createdAt.setText(list.get(position).get_id()); holder.desc.setText(list.get(position).get_id()); holder.publishedAt.setText(list.get(position).get_id()); holder.source.setText(list.get(position).get_id()); holder.type.setText(list.get(position).get_id()); holder.url.setText(list.get(position).get_id()); holder.who.setText(list.get(position).get_id()); ImageLoader.getInstance().displayImage(bean.getUrl(),holder.img); return view; } class ViewHolder{ TextView _id,createdAt,desc,publishedAt,source,type,url,who; ImageView img; }}
//MyApp
package com.example.zhangshaohang1511b20171117;import android.app.Application;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;/** * Created by 心所在梦就在 on 2017/11/17. */public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration build=new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(build); }}
//布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zhangshaohang1511b20171117.MainActivity"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>//适配器item XML文件
<?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/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/_id" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/createdAt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/publishedAt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/source" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/type" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/url" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/who" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
阅读全文
1 0
- pullToRefresh上啦刷新,下拉加载
- PullToRefresh上啦刷新下拉加载
- 上啦刷新,下拉加载
- 使用Android开源工具PullToRefresh实现上啦刷新下拉加载
- pulltorefresh 上拉加载 下拉刷新
- 下拉刷新,上拉加载更多 PullToRefresh
- Android-PullToRefresh下拉刷新,上拉加载
- PullToRefresh实现上拉加载下拉刷新
- PullToRefresh上拉刷新下拉加载
- PullToRefresh上拉加载,下拉刷新
- PullToRefresh上拉刷新下拉加载
- 上拉刷新下拉加载控件-PullToRefresh
- 上拉加载下拉刷新 PullToRefresh
- PullToRefresh上拉加载下拉刷新
- pulltorefresh上拉加载 下拉刷新
- PullToRefresh下拉刷新和上拉加载
- PullToRefresh的上拉加载下拉刷新
- pulltorefresh上拉加载以及下拉刷新
- 属性动画
- [leetcode]714. Best Time to Buy and Sell Stock with Transaction Fee
- 第七个实验 SYSTIC实验
- tensorflow之inception_v3模型的部分加载及权重的部分恢复(23)---《深度学习》
- 连续数组的最大和
- PullToRefresh上啦刷新下拉加载
- 118 Pascal's Triangle
- mybatis常用jdbcType数据类型
- Java-线程池
- 关闭微软cortana
- Visual svn server + TouristSVN集成
- 数据结构学习笔记(一):概念
- eclipsejavaweb项目启动tomcat 404错误
- 利用Pycharm打开Scrapy项目