Custom Pulltorefreshlistview Inherits ListView and Adds a Drop-down header layout to the ListView header. Exactly the same as ListView usage.
The custom ListView code is detailed to refer to: http://www.jb51.net/article/97845.htm
Detailed code for adapter is described here.
1. First to adapter binding ListView layout.
2. Second, create a class of hierarchical corresponding components, associating the corresponding components and objects to improve efficiency.
3. Then with the landing of the picture path to download the picture asynchronously, because you do not know the number of the microblogging picture, so in the ListView add a Girlview control or girllayout layout, and then add the resulting picture to it, and according to the specified property values arranged.
* * Created by d&ll on 2016/6/2. * * public class Weiboadapter extends Baseadapter {/** * for efficiency, a custom class that caches data preparation corresponds to a microblog data */class Weiboholder {Publi
C ImageView Wbicon;
Public TextView Wbtext, Wbtime, Wbuser;
Private homeactivity homeactivity = null;
DataSet public arraylist<weiboinfo> weibolist = null; Public Weiboadapter (homeactivity homeactivity, arraylist<weiboinfo> weibolist) {this.homeactivity =
homeactivity;
This.weibolist = weibolist;
///Micro Bo picture of the asynchronous download class Asyncimageloader Asyncimageloader; @Override public View getview (int position, View Convertview, ViewGroup parent) {Asyncimageloader = new asyncimageload
ER (); Document the Layout object Convertview = Layoutinflater.from (This.homeActivity.getApplicationContext ()) for each of the tweets that need to be displayed on what layout. Inflate (
R.layout.listview, NULL);
Creates a class weiboholder wh = new Weiboholder () of a hierarchical corresponding component;
The corresponding components and objects are associated to improve efficiency Wh.wbicon = (ImageView) Convertview.findviewbyid (R.id.wbicon); Wh.wbtext = (TextView) convertview.findvIewbyid (R.id.wbtext);
Wh.wbtime = (TextView) Convertview.findviewbyid (r.id.wbtime);
Wh.wbuser = (TextView) Convertview.findviewbyid (R.id.wbuser); /* Wh.wbimage = (ImageView) Convertview.findviewbyid (r.id.wbimage);//obtain a microblog data weiboinfo WB = This.weiboList.get (pos
ition);
if (WB!= null) {Convertview.settag (Wb.getid ());
Wh.wbuser.setText (Wb.getusername ());
Wh.wbtime.setText (Wb.gettime ()); Wh.wbtext.setText (Stringutils.getemotioncontent (Convertview.getcontext (), Wh.wbtext, Wb.gettext ()),
TextView.BufferType.SPANNABLE); if (Wb.gethaveimage ()) {//whether there is picture information//asynchronous loading picture content drawable[] Wb_image = new Drawable[wb.getimage_context (). length
];
imageview[] Wbimage = new Imageview[wb.getimage_context (). length];
GridLayout layout = (GridLayout) Convertview.findviewbyid (r.id.imagelayout); Iterate through downloading all the pictures and adding them to ListView for (int i = 0; i < Wb.getimage_context (). length; i++) {Wbimage[i] = new ImageView (t His.homeActivity.getApplicationContext ());
Wbimage[i].setpadding (5, 5, 5, 5);
Wbimage[i].setscaletype (ImageView.ScaleType.FIT_XY);
Gridlayout.spec row = Gridlayout.spec (I/3);
Gridlayout.spec Colum = Gridlayout.spec (i% 3);
Gridlayout.layoutparams params = new Gridlayout.layoutparams (row, colum);
Params.setgravity (Gravity.fill);
Params.width = 250;
Params.height = 250; Wb_image[i] = asyncimageloader.loaddrawable (Wb.getimage_context () [i], wbimage[i], new ASYNCIMAGELOADER.IMAGECALLBAC
K () {@Override public void imageloaded (drawable imagedrawable, ImageView ImageView, String imageUrl) {
Imageview.setimagedrawable (imagedrawable);
}
});
Wbimage[i].setimagedrawable (Wb_image[i]);
Layout.addview (Wbimage[i], params); }///Asynchronously load user avatar data drawable cachedimage = asyncimageloader.loaddrawable (Wb.getusericon (), Wh.wbicon, new Asyncimage Loader.imagecallback () {@Override public void imageloaded (drawable imagedrawable, ImAgeview ImageView, String imageUrl) {imageview.setimagedrawable (imagedrawable);
}
});
if (cachedimage = = null) {} else {wh.wbicon.setImageDrawable (cachedimage);
} return Convertview;
@Override public int GetCount () {return this.weiboList.size ();
@Override public Object getitem (int position) {return this.weiboList.get (position);
@Override public long getitemid (int position) {return position;
}
}
The method for downloading pictures asynchronously. The use of SoftReference is a soft reference for the system to better recycle the variable; after getting the picture path from the cache, create a new thread to download the picture.
* * Created by d&ll on 2016/6/2. * * public class Asyncimageloader {//softreference is a soft reference, is to better for the system recycling variable private hashmap<string, softreference<
Drawable>> Imagecache;
Public Asyncimageloader () {Imagecache = new hashmap<string, softreference<drawable>> ();
Public drawable loaddrawable (final String imageUrl, Final ImageView ImageView, final imagecallback imagecallback) { if (Imagecache.containskey (IMAGEURL)) {//get softreference<drawable> SoftReference = Imagecache.get from cache (
IMAGEURL);
drawable drawable = Softreference.get ();
if (drawable!= null) {return drawable; } final Handler Handler = new Handler () {public void Handlemessage (Message message) {Imagecallback.imageloa
Ded ((drawable) message.obj, ImageView, IMAGEURL);
}
};
Create a new thread to download a new threads () {@Override public void run () {drawable drawable = Loadimagefromurl (IMAGEURL); Imagecache.put (IMAGEURL, new softreference<drawable> (drawable));
Message message = Handler.obtainmessage (0, drawable);
Handler.sendmessage (message);
}}.start ();
return null;
public static drawable loadimagefromurl (String url) {URL m;
InputStream i = null;
try {m = new URL (URL);
i = (InputStream) m.getcontent ();
catch (Malformedurlexception E1) {e1.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
} drawable D = Drawable.createfromstream (i, "src");
return D; }//Callback interface public interface Imagecallback {public void imageloaded (drawable imagedrawable, ImageView ImageView, String
IMAGEURL);
}
}
Effect Chart:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.