The effect is this kind of:
Layout ideas:
The outermost parent form is a relativelayout (not pictured), starting from the inner layer.
Black: Custom ScrollView
Red: LinearLayout, in order to hold the inner layer three layout, orientation level.
Green: Three linearlayout, used to achieve the waterfall flow effect of the hash. Orientation is vertical.
Gap: Actually does not exist, here just to be able to see the structure.
Layout Code:
<COM.EXAMPLE.STREAM.SV android:id= "@+id/sv" android:layout_width= "Fill_parent" Android:layout_ height= "Fill_parent" > <linearlayout android:layout_width= "fill_parent" Android:layout_ height= "wrap_content" android:orientation= "Horizontal" > <linearlayout android: Id= "@+id/ll1" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" > </LinearLayout> <linearlayout android:id= "@+id/ll2" android:layout_width= "0DP" Android:lay out_height= "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" > </LinearLayout> <linearlayout android:id= "@+id/ll3" android:layou T_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:orientation= "ver Tical "> </LinearLayout> </LinearLayout> </com.example.stream.SV>
Custom ScrollView
The custom ScrollView is used because the native ScrollView does not have a swipe listener event. Rewrite the Ontouchevent () method with the custom view described in the previous blog post. Sets the callback interface for page loading.
<span style= "FONT-SIZE:18PX;" >public class SV extends Scrollview{private onscrolllistener listener;public SV (context context) {super (context);} Public SV (context context, AttributeSet Attrs) {Super (context, attrs);} <span style= "color: #ff0000;" >//passed in an Interface object public interface onscrolllistener{void Loadmore (); </span><span style= "color: #ff0000;" >//Set slide listener public void Setonscrolllistener (Onscrolllistener listener) {This.listener = listener;} </span>//finger-to-screen touch event---"Listener is sliding to the bottom @overridepublic boolean ontouchevent (motionevent ev) {View Childview = Getchildat (0);//Get measurement height (total height) int measuredheight = Childview.getmeasuredheight ();//Get Draw screen height int scrolly = getscrolly ();//Get visual area int Height = getheight (); <span style= "White-space:pre" ></span>//determine position if (measuredheight==scrolly+height) { Toast.maketext (GetContext (), "In the end!" ", 0). Show (); Listener.loadmore ();} return super.ontouchevent (EV);}} </span>
This calculation is used to determine if the ScrollView is sliding to the bottom and whether the data can continue to be requested.
GetHeight () +getscrolly () ==getmeasureheight ();
Mainactivity
The note is already clear.
<span style= "FONT-SIZE:18PX;" >public class Mainactivity extends Activity {private LinearLayout ll1, Ll2, ll3;//each imageview width, is the width of the screen 1/3private in T imgvwidth;//the number of private int pageSize = 15;//load per load, pageindex++private int pageIndex = 0;private string[] Imgname ;//Layout has three, added to the array, in order to update the UI when used. Private LinearLayout lls[] = new linearlayout[3];//custom ScrollView svprivate SV SV; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); init ();} private void Init () {SV = (SV) Findviewbyid (R.ID.SV);//used to hold screen information displaymetrics outmetrics = new Displaymetrics (); Getwindo Wmanager (). Getdefaultdisplay (). Getmetrics (outmetrics);//Set the width of each linearlayout for the entire screen 1/3imgvwidth = Outmetrics.widthpixels/3;ll1 = (linearlayout) Findviewbyid (r.id.ll1); ll2 = (linearlayout) Findviewbyid (R.ID.LL2); ll3 = (linearlayout) Findviewbyid (R.ID.LL3); lls[0] = ll1;lls[1] = ll2;lls[2] = ll3;try {//get all filenames Imgname = Getassets (). List (" IMGs ");} Catch(IOException e) {E.printstacktrace ();} Load Picture LoadImage () for the first time,//set ScrollView Scroll listener sv.setonscrolllistener (new Onscrolllistener () {@Overridepublic void Loadmore () {if (PageIndex * pageSize >= imgname.length) {Toast.maketext (Getapplicationcontext (), "Data loading Complete", 0). Show () ;} else {loadimage ();}});} Do picture loading private void LoadImage () {//If the number of loads is less than all files, each load 15 for (int i = PageIndex * pageSize; i < PageIndex * PageSize + pagesize&& I < imgname.length; i++) {//Load picture, instantiate a imageviewimageview IMGV = new ImageView (mainactivity.this);//width is the 1/3layoutparams LP = new Layoutpar of the screen AMS (imgvwidth,layoutparams.wrap_content); imgv.setlayoutparams (LP);//Load Picture resource Loadimagefromassets.load ( Mainactivity.this, Imgname[i], IMGV);//each time a different linearlayout is placed with a picture of imageviewlls[i% 3].addview (IMGV);} pageindex++;}} </span>
Loading data from assets
<span style= "FONT-SIZE:18PX;" >public class Loadimagefromassets {public static void load (final Activity activity,final String name,final ImageView IV {New Thread () {public void run () {try {//) Gets the picture resource from Assets/imgs, passing in the picture name InputStream stream = Activity.getassets (). Open (" imgs/"+name), final Bitmap Bitmap = Bitmapfactory.decodestream (stream);//main thread update uiactivity.runonuithread (New Runnable ( {public void Run () {//Get picture width height int bith = bitmap.getheight (); int bitw = Bitmap.getwidth ();//Get Layoutparams set width height, Fills the picture with the entire imageviewlayoutparams LP = (layoutparams) iv.getlayoutparams ();/** * The aspect ratio of the picture is equal to the imageview aspect ratio because the imageview width is determined ( 1/3 parent form), by calculating * Get imageview height */lp.height = bith*lp.width/bitw;//Set the parameter to IMAGEVIEWIV.SETLAYOUTPARAMS (LP); Iv.setimagebitmap (bitmap);}});} catch (IOException e) {e.printstacktrace ();}}}. Start ();}} </span>
writing is not good, a lot of things only in the code comments to speak out, blog I will always adhere to the process of finishing, but also in thinking how to write Good blog. I've been working on it.
[Android] Waterfall Flow Layout--page load (detailed code)