Android implements a waterfall stream similar to the collection and update mechanism of the ListView mode

Source: Internet
Author: User
Tags getcolor

Blog address: http://blog.csdn.net/u010593680/article/details/43771857 (reprint please keep the original address)

Project Address: Https://git.oschina.net/0-0Xuan/XWaterFall

Analyze the problem:

In doing the project encountered the need to use waterfall flow, so as usual use scroollview pattern waterfall flow, but waterfall flow effect is easy to implement, can once load a lot of pictures, then a memory overflow, and waterfall flow often need to add a lot of pictures, memory management can be said to be necessary, So the question is, how do you manage memory in a waterfall stream?

Workaround One: Use a variety of imageloader. The bitmap of the picture are all in the Imageloader statistics management, if the picture use memory exceeds the limit, then release some of the picture bitmap, but here there is an obvious question, which is the release of the image of the bitmap? This is often the most difficult step, Many patterns take the release of the least recently used bitmap, but the use of statistical bitmap itself is a relatively difficult thing, so imageloader is mainly used for fast and efficient loading of pictures

Workaround two: Using a custom view, when the view appears on the phone screen, Windowsmanager will call the view's OnDraw method to depict the view, so that you can determine in real time which view image needs to use, and then load the corresponding image into memory, This minimizes memory usage, and this approach solves the problem of memory overflow almost perfectly, but using this method requires the image to be loaded when the OnDraw () method is called, so the delay in loading the picture will compromise the user experience and, of course, add some contact code to the code. When a view is displayed, a picture of the view near the view is loaded, but the connection to each view is very close and the code is much more difficult to write.

Workaround three: Simulating the ListView allows the waterfall stream to reclaim the view that is not displayed. If you know which view is not displayed, simply recycle the bitmap of the view's images.

This blog main introduction to the third method of solving!

Achieve the goal: first, enable waterfall to discover the view that is displayed on the screen, and can set the slide, load the view forward and reverse need to cache the number of view to load the view in advance, so that the user experience better

To achieve goal two, enable waterfall to implement a ListView-like Notifydatasetchange () method, so that after the call to Notifydatasetchange () The ListView will check whether the original view is updated, If there is an update, update it accordingly. This makes it easy to modify, add, and delete the view in the waterfall.


Use of Xwaterfall:

The first step inherits Xwaterfall, which shows the 7 required implementations after inheritance:

/** * Notifydatasetchange when called to get child view, do not recommend that a large number of memory operations, chip bitmap loading, the operation should be placed in <span style= "font-family:arial, Helvetica, Sans-serif; " >onresumeview (int position, view view) </span> * * @param position * @return Returns the view */@Overridepublic view that needs to be added GetView (int position) {//TODO auto-generated method Stubreturn null;} /** * If you can determine the height of the corresponding view can be overridden in the subclass of the method * * @param position * @return */@Overridepublic int getheight (int position) {//TODO Auto -generated method Stubreturn 0;} /** * Returns the identity of the position corresponding view to determine if the view needs to be updated * * @param position * @return */@Overridepublic Object getmark (int position) {/ /TODO auto-generated method Stubreturn null;}  /** * * @return Waterfall number of neutron view */@Overridepublic int getcontentchildcount () {//TODO auto-generated method Stubreturn 0;} /** * * @return once notifydatasetchanged () the maximum number of view added */@Overridepublic int getnewchildnum () {//TODO auto-generated Metho d Stubreturn 0;} /** * Call this method when there is a new view beyond the display * * @param position * @param view */@Overrideprotected void OnrecyCleview (int position, view view) {//TODO auto-generated method stub}/** * When a new View is entered on the display, call it * * @param position * @para M view */@Overrideprotected void Onresumeview (int position, view view) {//TODO auto-generated method stub}
Understanding the implementation requirements of the method, we now implement a display of beauty and beauty position waterfall flow, the implementation of the following:

The code implemented is:

public class Imagewaterfall extends Xwaterfall {private String TAG = "Imagewaterfall";p rivate Context context;private List <String> imgs;private layoutinflater inflater;public imagewaterfall (context context, list<string> IMGs) { Super (context,3,0);//Set Waterfall context, number of columns, Width//TODO auto-generated constructor stubthis.context = context; This.inflater = Layoutinflater.from (context); This.imgs = IMGs;} @Overridepublic view GetView (int position) {//TODO auto-generated method Stubview View = Inflater.inflate (r.layout.item_ My_image, NULL); Myimageview IV = (Myimageview) View.findviewbyid (R.ID.IV); Iv.setname ("Beauty" +position); TextView TV = (TextView) View.findviewbyid (r.id.tvname); Tv.settext ("Beauty:" + position); return view;} @Overridepublic int Getcontentchildcount () {//TODO auto-generated method Stubreturn imgs.size ();} @Overridepublic int Getnewchildnum () {//TODO auto-generated method Stubreturn 12;} @Overridepublic void Onscrolltobottom () {//TODO auto-generated method stubnotifydatasetcHanged ();} @Overridepublic Object getmark (int position) {//TODO auto-generated method Stubreturn imgs.get (position);} @Overrideprotected void Onrecycleview (int position, view view) {//TODO auto-generated method Stubmyimageview IV = (MYIMAG Eview) View.findviewbyid (R.ID.IV); TextView TV = (TextView) View.findviewbyid (r.id.tvname); Tv.setbackgroundcolor (Context.getresources (). GetColor ( R.color.orange)); String url = imgs.get (position); Bitmap BM = imageloader.getbitmap (URL); if (BM! = null &&!bm.isrecycled ()) bm.recycle (); Iv.setimagebitmap (NULL) ; Imageloader.deletebitmap (URL); Android.view.ViewGroup.LayoutParams params = Iv.getlayoutparams ();p arams.height = Iv.getheight ();p arams.width = Iv.getwidth (); Iv.setlayoutparams (params); iv.recycle ();} @Overrideprotected void Onresumeview (int position, view view) {//TODO auto-generated method Stubtextview TV = (TextView) v Iew.findviewbyid (R.id.tvname); Tv.setbackgroundcolor (Context.getresources (). GetColor (R.color.green)); Myimageview IV = (Myimageview)View.findviewbyid (R.ID.IV); Imageloader.loadimg (Imgs.get (position), iv); Iv.resume ();} @Overridepublic int getchildheight (int position) {//TODO auto-generated method Stubreturn 0;}}


It is important to note that:

One, Getchildheight () returns the height of the child view, if you can not know the height of the view in advance, you may return 0 or negative, then the default selection of the shortest column is automatically used by the algorithm

Second, when the view updates and additions, and so on, will call the GetView method, but it may not be in the display and buffer, it is obviously inappropriate to consume a lot of memory here, and when the view first appears in the display and buffer, will call the Onresumeview () method, So Onresumeview () is better suited as a place to load pictures or other very memory-intensive calls

Third, ensure that a view of the Onresumeview () method will be after GetView (), and only when the view first appears in the display and buffer, will be called

Onrecycleview () is called when a view first leaves the display and buffer.

Onresumeview () and Onrecycleview () are called alternately and are guaranteed to call Onresumeview () before it is possible to call Onrecycleview ()


The 3rd test is also performed in the Imagewaterfall code: when Onresumeview () and Onrecycleview () are called, Myimageview's Resume () and recycle () are called respectively.

public class Myimageview extends ImageView {private string TAG = ' Myimageview ';p rivate String name;private int state;//Used to Test Onresumeview and Onrecycleview alternate execution, and first Onresumeview then Onrecycleviewpublic Myimageview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor stub this.setonclicklistener (onclick);// Xwaterfall does not affect onclicklistener such as the upper View}public void SetName (String name) {this.name = name;} @Overridepublic void Setimagebitmap (Bitmap BM) {//TODO auto-generated method stub//log.v (name, "Setimagebitmap"); super . SETIMAGEBITMAP (BM);} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method stub//log.v (name, "OnDraw:" + drawnum++); t ry {super.ondraw (canvas);} catch (Exception e) {e.printstacktrace (); LOG.V (TAG, name);}} public void Recycle () {LOG.V (tag, "xx recycle" + name); State--;if (state!= 0) {LOG.V (tag, "XX Error: State:" + state);}} public void Resume () {LOG.V (tag, "xx recovery" + name); State++;if (state! = 1) {LOG.V (tag, "XX Error: State:" + state);}} OnclicKlistener onclick = new Onclicklistener () {@Overridepublic void onclick (View v) {//TODO auto-generated method STUBLOG.V (T AG, "Onclicklistener");}};}


The implementation of the Xwaterfall is ready, and the next step is to use it specifically:

First step: Create: Imagewaterfall IWF = new Imagewaterfall (this, IMGs);

Step two: Add to a container: Llcontent.addview (IWF);

Step three: Update the content in Imagewaterfall: iwf.notifydatasetchanged ();

The first two steps are similar to the normal view, and the third step is to add the Imagewaterfall child view operation

You can also set the number of buffers in the positive and negative directions, and set a large buffer count to help reduce the time users wait to see and improve the user experience

(the finger is going to slide upward, then is ready to show the view below, so the bottom is the positive direction, the above is the opposite direction, the finger is going to slide, then ready to display the above view, so the top is the positive direction, the lower is the opposite direction)

<span style= "White-space:pre" ></span>iwf.setpositioncachenum (18);// Sets the number of cached view Iwf.setoppositecachenum (18) When the view is loaded,//sets the number of cached view that is reversed when the view is loaded

Next, you'll learn how to update, delete, and insert view into Imagewaterfall:

Switch (View.getid ()) {case r.id.btrefrash://update position to 1 Viewimgs.set (1, "http://g.hiphotos.baidu.com/image/pic/ Item/1ad5ad6eddc451da9f2e8e8cb5fd5266d11632f8.jpg "); iwf.notifydatasetchanged (); Break;case R.id.btDelete:// Delete position 1 viewimgs.remove (1); iwf.notifydatasetchanged (); Break;case r.id.btinseart:// Insert a new Viewimgs.add (1, "http://h.hiphotos.baidu.com/image/pic/item/") before the view of position 1 810a19d8bc3eb1350c58efbca41ea8d3fd1f441d.jpg "); iwf.notifydatasetchanged (); break;}
Note that the above IMGs corresponds to the private list<string> IMGs in Imagewaterfall; this usage is very similar to the usage in the ListView

Xwaterfall the use of the introduction is complete, the source code at the top of the blog, if there are any questions, welcome feedback!!




Android implements a waterfall stream similar to the collection and update mechanism of the ListView mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.