PullToRefreshListView实现多条目加载上拉刷新和下拉加载

作者:dp666666 时间:2022-07-21 02:26:01 

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

XML布局

主页面main布局


<com.handmark.pulltorefresh.library.PullToRefreshListView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/pull"
 >
</com.handmark.pulltorefresh.library.PullToRefreshListView>

pulllist布局


<ImageView
 android:layout_width="120dp"
 android:layout_height="100dp"
 android:id="@+id/iv"
 />
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tv"
 />

java代码

App页面


package com.example.duanpengpenglx20171108;

import android.app.Application;

import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

/**
* Created by 朋 on 2017/11/8.
*/

public class App extends Application {
 @Override
 public void onCreate() {
   super.onCreate();
   ImageLoaderConfiguration config = new ImageLoaderConfiguration
       .Builder(this)
       .memoryCacheExtraOptions(480,800)
       .threadPoolSize(5)
       .threadPriority(Thread.NORM_PRIORITY - 2)
       .denyCacheImageMultipleSizesInMemory()
       .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
       .memoryCacheSize(2 * 1024 * 1024)
       .tasksProcessingOrder(QueueProcessingType.LIFO)
       .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
       .imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
       .writeDebugLogs() // Remove for release app
       .build();//开始构建
//初始化imageloader;
   ImageLoader.getInstance().init(config);
 }
}

MainActivity页面


public class MainActivity extends AppCompatActivity {

List<Jbean.DataBean> arr = new ArrayList();
 private View view;
 private ListView lv;
 private Adapter adapter;
 int i = 0;
 private PullToRefreshListView pull;
 private Adapter adapter1;

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   pull = (PullToRefreshListView) findViewById(R.id.pull);
   pull.setMode(PullToRefreshBase.Mode.BOTH);
   ListView lv = pull.getRefreshableView();
   adapter1 = new Adapter();
   lv.setAdapter(adapter1);

new MyAsyncTask().execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0");
   pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
     @Override
     //上拉
     public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
       arr.clear();
       i = 0;
       new MyAsyncTask().execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + i);
     }
     @Override
     //下拉加载20条
     public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
       i = i + 20;new MyAsyncTask().execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + i);

}
   });
 }
 class MyAsyncTask extends AsyncTask<String, Void, String> {

@Override
   protected String doInBackground(String... params) {
     String string = "";
     StringBuffer stringBuffer = new StringBuffer();
     try {
       URL url = new URL(params[0]);
       URLConnection urlConnection = url.openConnection();
       InputStream inputStream = urlConnection.getInputStream();
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
       while ((string = bufferedReader.readLine()) != null) {
         stringBuffer.append(string);
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
     return stringBuffer.toString();
   }

@Override
   protected void onPostExecute(String s) {
     super.onPostExecute(s);
     Jbean fragone_bean = new Gson().fromJson(s, Jbean.class);
     List<Jbean.DataBean> data = fragone_bean.getData();
     arr.addAll(data);
     adapter1.notifyDataSetChanged();
     pull.onRefreshComplete();
   }
 }

class Adapter extends BaseAdapter {

@Override
   public int getCount() {
     return arr.size();
   }

@Override
   public Object getItem(int position) {
     return arr.get(position);
   }

@Override
   public long getItemId(int position) {
     return position;
   }

@Override
   public View getView(int position, View convertView, ViewGroup parent) {
     Sj sj = new Sj();
     Jbean.DataBean dataBean = arr.get(position);
     if (convertView==null){
       convertView = View.inflate(MainActivity.this,R.layout.pulllist,null);
       sj.image=convertView.findViewById(R.id.iv);
       sj.tv=convertView.findViewById(R.id.tv);
       convertView.setTag(sj);
     }else {
       sj =(Sj) convertView.getTag();
     }
     sj.tv.setText(dataBean.getSUBTITLE());
     DisplayImageOptions options = new DisplayImageOptions.Builder()
         .cacheInMemory(true)
         .cacheOnDisk(true)
         .showImageOnLoading(R.drawable.zhengzaijiazai)
         .showImageForEmptyUri(R.drawable.zhengzaijiazai)
         .showImageOnFail(R.drawable.zhengzaijiazai)
         .bitmapConfig(Bitmap.Config.RGB_565)
         .build();
     ImageLoader.getInstance().displayImage((String) dataBean.getIMAGEURL(),sj.image,options);

return convertView;
   }
 }
 class Sj{
   ImageView image;
   TextView tv;
 }
 }

来源:https://blog.csdn.net/dp666666/article/details/78481938

标签:PullToRefreshListView,下拉加载,上拉刷新
0
投稿

猜你喜欢

  • Java实现五子棋游戏的完整代码

    2022-07-01 15:32:34
  • Jaxb2实现JavaBean与xml互转的方法详解

    2023-11-25 11:51:38
  • IDEA启动tomcat控制台中文乱码问题的解决方法(100%有效)

    2021-06-25 10:45:23
  • Android实现App中导航Tab栏悬浮的功能

    2021-11-22 23:11:31
  • Spring的连接数据库以及JDBC模板(实例讲解)

    2023-03-21 05:56:36
  • Android WebView实现全屏播放视频

    2023-04-12 17:12:39
  • Springboot+SpringSecurity+JWT实现用户登录和权限认证示例

    2021-11-14 11:06:11
  • 基于C#实现的屏幕指定区域截屏代码

    2021-12-31 19:42:09
  • C# 异步多线程入门基础

    2022-01-19 05:23:05
  • Android 设置Edittext获取焦点并弹出软键盘

    2022-02-28 12:02:02
  • Java Floyd算法求有权图(非负权)的最短路径并打印

    2023-04-10 12:53:42
  • 浅谈Android中多线程切换的几种方法

    2022-03-12 06:03:17
  • Mybatis-plus多租户项目实战进阶指南

    2023-10-10 18:14:43
  • C#使用Selenium+PhantomJS抓取数据

    2022-09-13 23:18:02
  • Android开发之图片压缩工具类完整实例

    2023-07-02 10:22:08
  • java private关键字用法实例

    2022-01-16 10:08:06
  • MyBatis 中 SqlMapConfig 配置文件详解

    2023-10-19 21:23:01
  • Java使用substring()截取(提取)子字符串

    2023-09-29 17:02:04
  • 基于ClasspathResource路径问题的解决

    2022-03-29 21:23:22
  • Java设计模式之工厂模式实现方法详解

    2023-11-26 07:55:51
  • asp之家 软件编程 m.aspxhome.com