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

XListView实现多条目网络数据刷新加载 网络加载图片

程序员文章站 2023-11-18 17:10:10
本文实例为大家分享了xlistview实现刷新加载的具体代码,供大家参考,具体内容如下 mainactivity.java protected void onc...

本文实例为大家分享了xlistview实现刷新加载的具体代码,供大家参考,具体内容如下

mainactivity.java

protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);

 pager = findviewbyid(r.id.viewpager);
 group = findviewbyid(r.id.group);

 li = new arraylist<>();
 li.add(new afragment());
 li.add(new bfragment());
 li.add(new cfragment());
 li.add(new dfragment());

 pager.setadapter(new fragmentpageradapter(getsupportfragmentmanager()) {
  @override
  public fragment getitem(int position) {
  return li.get(position);
  }

  @override
  public int getcount() {
  return li.size();
  }
 });

 group.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {
  @override
  public void oncheckedchanged(radiogroup radiogroup, int i) {

  switch (i){
   case r.id.r1:
   pager.setcurrentitem(0);
   break;
   case r.id.r2:
   pager.setcurrentitem(1);
   break;
   case r.id.r3:
   pager.setcurrentitem(2);
   break;
   case r.id.r4:
   pager.setcurrentitem(3);
   break;
  }
  }
 });

 }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".ui.mainactivity">
 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >
 <android.support.v4.view.viewpager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="9"
  >
 
 </android.support.v4.view.viewpager>
 
 <radiogroup
  android:id="@+id/group"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:orientation="horizontal"
  >
  <radiobutton
  android:id="@+id/r1"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="afrag"/>
  <radiobutton
  android:id="@+id/r2"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="bfrag"/>
  <radiobutton
  android:id="@+id/r3"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="cfrag"/>
  <radiobutton
  android:id="@+id/r4"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="dfrag"/>
 
 </radiogroup>
 
 </linearlayout>
 
</android.support.constraint.constraintlayout>

afragment.java

public class afragment extends fragment{
 private int page=1;
 private xlistview listview;
 private mybaseadapter adapter;
 responsebean responsebean;
 
 @nullable
 @override
 public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
  view view = view.inflate(getactivity(), r.layout.afrag, null);
 
  listview = view.findviewbyid(r.id.xlv);
  adapter = new mybaseadapter(getlayoutinflater());
  listview.setadapter(adapter);
 
  listview.setpullloadenable(true);
  listview.setxlistviewlistener(new xlistview.ixlistviewlistener() {
  @override
  public void onrefresh() {
   page=0;
   loaddata(page);
  }
 
  @override
  public void onloadmore() {
   loaddata(page);
 
  }
  });
  loaddata(page);
  return view;
 }
 
 private string url =
  "http://apis.juhe.cn/cook/query?key=3ec004200a6a2f4cf4774e480c006375&menu=%e8%a5%bf%e7%ba%a2%e6%9f%bf&rn=10&pn=";
 
 private void loaddata(int page){
 
  string urlstring=url+page;
  new asynctask<string,void,list<dataitem>>(){
 
  @override
  protected list<dataitem> doinbackground(string... strings) {
   try {
   url url = new url(strings[0]);
   httpurlconnection urlconnection = (httpurlconnection) url.openconnection();
   urlconnection.setrequestmethod("get");
   urlconnection.setreadtimeout(5000);
   urlconnection.setconnecttimeout(5000);
   int responsecode = urlconnection.getresponsecode();
   if (responsecode==200){
    string result=string2(urlconnection.getinputstream());
    responsebean = new gson().fromjson(result, responsebean.class);
   }else {
 
   }
   return responsebean == null ?null:responsebean.getresult().getdata();
   } catch (malformedurlexception e) {
   e.printstacktrace();
   } catch (ioexception e) {
   e.printstacktrace();
   }
   return null;
  }
 
  @override
  protected void onpostexecute(list<dataitem> dataitems) {
   super.onpostexecute(dataitems);
   if (dataitems == null) {
   toast.maketext(getactivity(), "请求数据错误", toast.length_long).show();
   return;
   }
   updatedata(dataitems);
   loadcompleted();
 
  }
  }.execute(urlstring);
 }
 
 private void updatedata(list<dataitem> dataitems){
  if (page==0){
  adapter.setdatas(dataitems);
  }else {
  adapter.adddatas(dataitems);
 
  }
 
 }
 
 private void loadcompleted(){
  page++;
  listview.stoprefresh();
  listview.stoploadmore();
 }
 
 private string string2(inputstream stream){
  stringbuilder sb = new stringbuilder();
  bufferedreader reader = new bufferedreader(new inputstreamreader(stream));
  try {
  for (string tmp=reader.readline();tmp!=null;tmp=reader.readline()){
   sb.append(tmp);
  }
  } catch (ioexception e) {
  e.printstacktrace();
  }
  return sb.tostring();
 }
 }

mybaseadapter.java

public class mybaseadapter extends baseadapter{

 private list<dataitem> datas;
 private layoutinflater layoutinflater;

 public mybaseadapter(layoutinflater layoutinflater) {
 this.layoutinflater = layoutinflater;
 datas=new arraylist<>();
 }

 public void setdatas(list<dataitem> datas) {
 datas.clear();
 if (datas!=null){
  datas.addall(datas);
 }
 notifydatasetchanged();
 }

 public void adddatas(list<dataitem> dataitems) {
 if (dataitems!=null){
  datas.addall(dataitems);
  notifydatasetchanged();
 }

 }

 public final int view_type_count=2;
 public final int image_text_type=0;
 public final int text_type=1;
 @override
 public int getitemviewtype(int position) {
 if (position % 2==0){
  return image_text_type;
 }else {
  return text_type;
 }

 }

 @override
 public int getviewtypecount() {
 return view_type_count;
 }

 @override
 public int getcount() {
 return datas.size();
 }

 @override
 public object getitem(int i) {
 return datas.get(i);
 }

 @override
 public long getitemid(int i) {
 return i;
 }

 @override
 public view getview(int i, view view, viewgroup viewgroup) {

 if (getitemviewtype(i)==image_text_type){
  viewholder viewholder=null;
  if (view==null){
  view = layoutinflater.inflate(r.layout.item, viewgroup,false);
  viewholder = new viewholder(view);

  }else {
  viewholder = (viewholder) view.gettag();
  }
  viewholder.binddata((dataitem) getitem(i));
 }else {
  viewholder2 viewholder2;
  if (view==null){
  view = layoutinflater.inflate(r.layout.item_text, viewgroup, false);
  viewholder2 = new viewholder2();
  viewholder2.textview = view.findviewbyid(r.id.text);
  view.settag(viewholder2);
  }else {
  viewholder2 = (viewholder2) view.gettag();
  }

  viewholder2.textview.settext(datas.get(i).gettitle());
 }

 return view;
 }


 public class viewholder{
 private imageview icon;
 private textview title;
 private textview date;


 public viewholder(view view) {
  icon = view.findviewbyid(r.id.icon);
  title = view.findviewbyid(r.id.title);
  date = view.findviewbyid(r.id.date);

  view.settag(this);
 }

 public void binddata(dataitem item) {
  title.settext(item.gettitle());
  date.settext(item.getid());

  imageloader.getinstance().displayimage(item.getfirst(),icon
   , imageloaderconfigs.getdefult(icon.getcontext()));
 }
 }

 public class viewholder2{
 textview textview;

 }

}

imageloaderconfigs.java

public class imageloaderconfigs {
 public static imageloaderconfiguration getimageloader(context context){
 imageloaderconfiguration configuration = new imageloaderconfiguration.builder(context)
  .memorycachesizepercentage(13)
  .diskcachesize(50*1024*1024)
  .defaultdisplayimageoptions(displayimageoptions.createsimple())
  .writedebuglogs()
  .build();
 return configuration;
 }

 public static displayimageoptions getdefult(context context){
 displayimageoptions imageoptions = new displayimageoptions.builder()
  .cacheinmemory(true)
  .cacheondisk(true)
  .bitmapconfig(bitmap.config.rgb_565)
  .imagescaletype(imagescaletype.exactly_stretched)
  .displayer(new circlebitmapdisplayer())
  .build();
 return imageoptions;
 }

}

app.java

public class app extends application{
 @override
 public void oncreate() {
 super.oncreate();
 imageloader.getinstance().init(imageloaderconfigs.getimageloader(this));
 }
}

responsbean.java

public class responsebean {
 private int resultcode;
 private string reason;
 private result result;

 public int getresultcode() {
 return resultcode;
 }

 public string getreason() {
 return reason;
 }

 public result getresult() {
 return result;
 }

 public class result{
 private list<dataitem> data;

 public list<dataitem> getdata() {
  return data;
 }
 }
}

dataitem.java

public class dataitem {
 private string id;
 private string title;
 private list<string> albums;

 public string getid() {
 return id;
 }

 public string gettitle() {
 return title;
 }

 public list<string> getalbums() {
 return albums;
 }
 public string getfirst(){
 return albums==null || albums.size()==0? "" : albums.get(0);
 }

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