欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-23 14:59:18
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 xml布局 主页面main布局

本文实例为大家分享了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;
  }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。