Waterfall flow in just out of time is very fire, we all feel very cool, so I also played a, but the results, when applied to specific scenarios, we need to use the pull-up refresh function, open source effect can not be achieved, so, after the great God, re-realized the effect.
First, we need to refer to the project of waterfall flow, GitHub project address: Https://github.com/paulzeng/AndroidStaggeredGrid, this project I have fork to my homepage, download, The library is then referenced.
Then we introduced the pull-down refresh project, GitHub project address: Https://github.com/paulzeng/Android-PullToRefresh, this project I also fork, download, introduced to the library. We need to modify the Pulltorefreshbase.java class and add two methods to determine whether it is pull-up or drop-down.
public Boolean Isheadershown () {return getheaderlayout (). Isshown ();} public Boolean Isfootershown () {return getfooterlayout (). Isshown (); }
at this point, the reference to the engineering library, we have done, and next, we need to customizePulltorefreshstaggeredgridviewclass that merges the waterfall stream with the refresh component.
Import Android.annotation.suppresslint;import Android.annotation.targetapi;import Android.content.context;import Android.os.build.version;import Android.os.build.version_codes;import Android.os.bundle;import Android.util.attributeset;import Android.view.view;import Com.etsy.android.grid.staggeredgridview;import Com.handmark.pulltorefresh.library.overscrollhelper;import Com.handmark.pulltorefresh.library.PullToRefreshBase ; Import COM.HANDMARK.PULLTOREFRESH.LIBRARY.R; @SuppressLint ("Newapi") public class Pulltorefreshstaggeredgridview extendspulltorefreshbase<staggeredgridview> {private static final onrefreshlistener<staggeredgridview> Defaultonrefreshlistener = new Onrefreshlistener<staggeredgridview> () {@Overridepublic void Onrefresh ( Pulltorefreshbase<staggeredgridview> refreshview) {}};p ublic Pulltorefreshstaggeredgridview (context context) {Super (context);/** * Added so "by default, Pull-to-refresh refreshes the page */setonrefreshlistener (defaultonrefres HlistEner);} Public Pulltorefreshstaggeredgridview (context context, AttributeSet Attrs) {Super (context, attrs);/** * Added so "by D Efault, Pull-to-refresh refreshes the page */setonrefreshlistener (Defaultonrefreshlistener);} Public Pulltorefreshstaggeredgridview (Context context, Mode mode) {Super (context, mode);/** * Added-so "by default, Pu Ll-to-refresh refreshes the page */setonrefreshlistener (Defaultonrefreshlistener);} Public Pulltorefreshstaggeredgridview (Context context, mode Mode,animationstyle style) {Super (context, mode, style);/* * * Added so ' by default, Pull-to-refresh refreshes the page */setonrefreshlistener (Defaultonrefreshlistener);} @Overridepublic final Orientation getpulltorefreshscrolldirection () {return orientation.vertical;} @Overrideprotected Staggeredgridview Createrefreshableview (Context context,attributeset attrs) {Staggeredgridview Gridview;if (VERSION. Sdk_int >= version_codes. Gingerbread) {GridView = new InternalStaggeredGridViewSDK9 (context, attrs);} ElSE {gridView = new Staggeredgridview (context, attrs);} Gridview.setid (R.id.gridview); return gridview;} @Overrideprotected Boolean Isreadyforpullstart () {Boolean result = false; View v = Getrefreshableview (). Getchildat (0); if (Getrefreshableview (). getfirstvisibleposition () = = 0) {if (v! = NULL) {//G Ettop () and Getbottom () are relative to the listview,//so if GetTop () is negative, it's not fully visibleboolean Istopfu llyvisible = V.gettop () >= 0;result = istopfullyvisible;}} return result;} @Overrideprotected Boolean Isreadyforpullend () {Boolean result = False;int last = Getrefreshableview (). Getchildcount ()- 1; View v = Getrefreshableview (). Getchildat (last); int firstvisibleposition = Getrefreshableview (). Getfirstvisibleposition (); int visibleitemcount = Getrefreshableview (). Getchildcount (); int itemCount = Getrefreshableview (). Getadapter (). GetCount (); if (firstvisibleposition + visibleitemcount >= itemCount) {if (v! = NULL) {Boolean islastfullyvisible = V.getbottom () <= getrefreshableView (). GetHeight (); result = islastfullyvisible;}} return result;} @Overrideprotected void Onptrrestoreinstancestate (Bundle savedinstancestate) {super.onptrrestoreinstancestate ( Savedinstancestate);} @Overrideprotected void Onptrsaveinstancestate (Bundle saveState) {super.onptrsaveinstancestate (saveState);} @TargetApi (9) Final class InternalStaggeredGridViewSDK9 extends Staggeredgridview {//WebView doesn ' t always scroll back T o It ' s edge so we add some//fuzzinessstatic final int overscroll_fuzzy_threshold = 2;//WebView seems quite reluctant to Overscroll so we use the scale//factor to scale it ' s valuestatic final float overscroll_scale_factor = 1.5f;public Intern AlStaggeredGridViewSDK9 (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected boolean Overscrollby (int deltax, int deltay, int scrollx,int scrolly, int scrollrangex, int scrollrange Y,int maxoverscrollx, int maxoverscrolly, Boolean istouchevent) {Final Boolean returnvalue = Super.overscrollby (DeltaX, D Eltay, Scrollx, scrolly, Scrollrangex, Scrollrangey,maxoverscrollx, maxoverscrolly, istouchevent);//Does All of the hard work. .. Overscrollhelper.overscrollby (Pulltorefreshstaggeredgridview.this,deltax, Scrollx, DeltaY, GetScrollRange (), istouchevent); return returnvalue;} /** * taken from the AOSP ScrollView source */private int Getscrollrange () {int scrollrange = 0;if (Getchildcount () > 0 {View child = getchildat (0); scrollrange = Math.max (0, Child.getheight ()-(GetHeight ()-Getpaddingbottom ()-Getpaddingt OP ()));} return scrollrange;}}}
Then we customize the layout file and use our re-written controls
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http// Schemas.android.com/apk/res-auto " android:layout_width=" match_parent " android:layout_height=" Wrap_ Content " android:background=" #FFF " android:orientation=" vertical "> < Com.thomas.pulltorefreshstaggeredgridview.PullToRefreshStaggeredGridView android:id= "@+id/pull_grid_view" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_margin= " 10DP " android:background=" #F0F0F0 " android:cachecolorhint=" #00000000 " android:fadingedge=" None " android:overscrollmode= "Never" android:scrollbars= "None" app:column_count= "2" app:item_ Margin= "8DP"/></linearlayout>
And then we coding in the activity.
Import Java.util.arraylist;import Java.util.list;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Com.etsy.android.grid.staggeredgridview;import Com.handmark.pulltorefresh.library.pulltorefreshbase;import Com.handmark.pulltorefresh.library.pulltorefreshbase.mode;import Com.handmark.pulltorefresh.library.pulltorefreshbase.onrefreshlistener;import Com.nostra13.universalimageloader.cache.disc.naming.md5filenamegenerator;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.imageloaderconfiguration;import Com.nostra13.universalimageloader.core.assist.QueueProcessingType; @SuppressLint ("Newapi") public class Mainactivity extends Activity implementsonrefreshlistener<staggeredgridview> {private list<photobean> Photos;private staggeredgridview mdongtaigridview;private myphotoadapter madapter;/** down Refresh **/private Pulltorefreshstaggeredgridview MpulltorefresHstaggerdgridview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initimageloader (this); Setcontentview (r.layout.activity_main);p Hotos = Getphotos (); madapter = new Myphotoadapter ( this, photos); Mpulltorefreshstaggerdgridview = (Pulltorefreshstaggeredgridview) This.findviewbyid (R.id.pull_grid_ view); Mpulltorefreshstaggerdgridview.setmode (Mode.pull_from_start); Mpulltorefreshstaggerdgridview.setmode ( Mode.both); Mpulltorefreshstaggerdgridview.setonrefreshlistener (this); Mdongtaigridview = Mpulltorefreshstaggerdgridview.getrefreshableview (); Mdongtaigridview.setadapter (MAdapter);} Private list<photobean> Getphotos () {list<photobean> photos = new arraylist<photobean> (); Photobean Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629140736_5JR8c.jpeg", " Summer Wind ");p Hotos.add (Photobean); Photobean = new Photobean ("Http://img5q.duitang.com/uploads/item/201506/29/20150629141607_LNFkx.thumb.700_0.jpeg" , "Summer Wind");p HotoS.add (Photobean); Photobean = new Photobean ("Http://img5q.duitang.com/uploads/item/201506/29/20150629141550_MuVm5.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img5q.duitang.com/uploads/item/201506/29/20150629141740_TUB2H.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629141716_nCUrG.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://cdnq.duitang.com/uploads/item/201506/29/20150629141656_5GBzS.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629141634_CEida.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img5q.duitang.com/uploads/item/201506/29/20150629141236_sAHim.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629141218_ezRd4.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("http://cdnq.duitang.com/uploads/iTem/201506/29/20150629141200_nykzy.jpeg "," Summer Wind ");p Hotos.add (Photobean); Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629141125_FCtVN.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img4q.duitang.com/uploads/item/201506/29/20150629141108_mBrUc.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://cdnq.duitang.com/uploads/item/201506/29/20150629141052_cdFuA.thumb.700_0.jpeg", "Summer Wind");p Hotos.add (Photobean); Photobean = new Photobean ("Http://cdnq.duitang.com/uploads/item/201506/29/20150629141030_zv2Rt.jpeg", "Summer Wind"); Photos.add (Photobean); Photobean = new Photobean ("Http://img5q.duitang.com/uploads/item/201506/29/20150629140948_UMWTQ.jpeg", "Summer Wind"); Photos.add (Photobean); return photos;} @Overridepublic void Onrefresh (pulltorefreshbase<staggeredgridview> refreshview) {//TODO auto-generated method Stubif (Refreshview.isheadershown ()) {} else {}}public void Initimageloader (context context) {Imageloaderconfiguration Config= new Imageloaderconfiguration.builder (context). ThreadPriority (thread.norm_priority-2). Denycacheimagemultiplesizesinmemory (). Disccachefilenamegenerator (New Md5filenamegenerator ()). Tasksprocessingorder (Queueprocessingtype.lifo). Writedebuglogs ()//Remove for release App.build (); I Mageloader.getinstance (). init (config);}}
Next, take a look at:
if it's not clear, you can go to my GitHub homepage and download the full code demo
Click to open link
Project I will further complete on GitHub, but also hope to work with you to improve.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ability to pull down the refresh and pull-up load of the waterfall stream