I. Overview
Now most of the app's details pages are pretty much the same, almost all of the above is a picture of a product, when you slide, there will be a tab suspended on the top, so that the user experience is really good, if the tab slide up, users may also need to slide down, in order to click tab, this is really troublesome. Immersion status bar that, Guo Lin said Google did not give an immersive status bar this is clear, Google only said the immersion mode (immersive modes). However, the Immersion status bar is not a big name, with the public bar, but the Android environment is not the same as the iOS environment is particularly unified, such as the Huawei ROM and the virtual button of the millet ROM is completely different, all Android developers are not easy ...
Second, the effect of Taobao
Third, our effect
can only pass 2M, put my beauty all pressure distortion ...
Iv. Implementation Class
Custom ScrollView (Stickyscrollview)
Statusbarutil//Very good status bar tool
Five, layout
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height
= "Match_parent" > <framelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" > <com.xiaoyuan.stickyscrollview android:id= "@+id/scrollview" android:layout_width= "Match_parent" android:layout _height= "Match_parent" android:focusable= "true" android:focusableintouchmode= "true" > <linearlayout android: Id= "@+id/ll_content" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:orientation = "vertical" > <imageview android:layout_width= "match_parent" android:layout_height= "500dip" Android: background= "@mipmap/meinv"/> <textview android:id= "@+id/title" android:layout_width= "Match_parent" Android: layout_height= "50DP" android:gravity= "center" android:text= "Beauty"/> <textview android:layout_width= "Match_parent" android:layout_height= "50dip" android:gravity= "center" android:text= "female"/> <textview android: Layout_width= "Match_parent" android:layout_height= "50dip" android:gravity= "center" android:text= "Beauty"/> < TextView android:layout_width= "match_parent" android:layout_height= "50dip" android:gravity= "Center" android:text= "
No "/> <textview android:layout_width=" match_parent "android:layout_height=" 50dip "android:gravity=" center "
Android:text= "Mei"/> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"
android:orientation= "vertical" android:tag= "sticky" > <linearlayout android:layout_width= "match_parent"
android:layout_height= "45DP" android:background= "#ffffff" android:orientation= "Horizontal" > <textview Android:id= "@+id/infotext" android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "center" android:text= "Beautiful Information" android:textcolor= "#000000" android:textsize= "16DP"/> < TextView android:id= "@+id/secondtext" android:layout_width= "0DP" android:layout_height= "Match_parent" Android: layout_weight= "1" android:gravity= "center" android:text= "Beauty Introduction" android:textcolor= "#000000" android:textsize= "16DP" /> </LinearLayout> </LinearLayout> <framelayout android:id= "@+id/tabmaincontainer" android:layout _width= "Match_parent" android:layout_height= "wrap_content" android:background= "#ffffff" android:minheight= "400DP" > </FrameLayout> </LinearLayout> </com.xiaoyuan.StickyScrollView> <relativelayout android: Id= "@+id/ll_good_detail" android:layout_width= "match_parent" android:layout_height= "49DP" android:background= "# 00000000 "android:paddingtop=" @dimen/spacing_normal > <textview android:layout_width= "wrap_content" Android: layout_height= "Wrap_content" android:textcolor= "#ffffff" android:layout_alignparentleft= "true" Android:layout_ marginleft= "10dip" android:layout_centerhorizontal= "true" android:text= "return"/> <textview anDroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "#ffffff" android:layout _centerinparent= "true" android:layout_centerhorizontal= "true" android:layout_marginleft= "10dip" android:text= "Beauty" /> <textview android:layout_width= "wrap_content android:layout_height=" Wrap_content "android:textColor=" # FFFFFF "android:layout_alignparentright=" true "android:layout_marginright=" 10dip "android:layout_centerhorizontal = "true" android:text= "sharing"/> </RelativeLayout> </FrameLayout> </RelativeLayout>
Note: We set the tab to suspend to android:tag= the "sticky" attribute
Vi. Implementing code
public class Mainactivity extends appcompatactivity implements View.onclicklistener, Stickyscrollview.onscrollchangedlistener {TextView onetextview, Twotextview; private Stickyscrollview
Stickyscrollview;
private int height;
Private LinearLayout llcontent;
Private Relativelayout Lltitle;
Private Framelayout framelayout;
Private TextView title; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview ();
Initlisteners (); /** * Initialize view/private void Initview () {Stickyscrollview = (Stickyscrollview) Findviewbyid (R.id.scrollview); framelay
out = (framelayout) Findviewbyid (R.id.tabmaincontainer);
title = (TextView) Findviewbyid (r.id.title);
Onetextview = (TextView) Findviewbyid (R.id.infotext);
Llcontent = (linearlayout) Findviewbyid (r.id.ll_content);
Lltitle = (relativelayout) Findviewbyid (R.id.ll_good_detail);
Onetextview.setonclicklistener (this); Twotextview = (TextView) Findviewbyid (R.id.seconDtext);
Twotextview.setonclicklistener (this);
Stickyscrollview.setonscrolllistener (this);
Statusbarutil.settranslucentforimageview (mainactivity.this, 0, title);
Framelayout.layoutparams params = (framelayout.layoutparams) lltitle.getlayoutparams ();
Params.setmargins (0, Getstatusheight (), 0, 0);
Lltitle.setlayoutparams (params); The default setting is a FRG Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.tabmaincontainer, fragment.newinstance ()
). commit (); /** * Get status bar height * @return/private int getstatusheight () {int ResourceID = MainActivity.this.getResources (). Getidentifi
ER ("status_bar_height", "Dimen", "Android");
Return Getresources (). Getdimensionpixelsize (ResourceID); @Override public void OnClick (View v) {if (V.getid () = R.id.infotext) {Getsupportfragmentmanager (). BeginTransaction ()
. replace (R.id.tabmaincontainer, Fragment.newinstance ()). commit (); else if (v.getid () = = R.id.secondtext) {Getsupportfragmentmanager (). BeginTransaction (). Replace ( R.id.tabmaincontainer, Fragment1.newiNstance ()). commit (); } private void Initlisteners () {//Get content Total Height final viewtreeobserver vto = Llcontent.getviewtreeobserver (); Vto.addonglobal Layoutlistener (New Viewtreeobserver.ongloballayoutlistener () {@Override public void ongloballayout () {height =
Llcontent.getheight ();
Note that you want to remove the Llcontent.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
}
});
Get fragment height Viewtreeobserver viewtreeobserver = framelayout.getviewtreeobserver (); Viewtreeobserver.addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {@Override public void Ongloballayout () {height = height-framelayout.getheight ();//Note to remove Framelayout.getviewtreeobserver ().
Removeglobalonlayoutlistener (this);
}
});
Get title height Viewtreeobserver viewTreeObserver1 = lltitle.getviewtreeobserver (); Viewtreeobserver1.addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {@Override public void Ongloballayout () {height = height-lltitle.getheight ()-getstatusheight ();//Calculate the total distance of the slide StickyscroLlview.setsticktop (Lltitle.getheight () + getstatusheight ())//set distance how much suspension//note to remove Lltitle.getviewtreeobserver ().
Removeglobalonlayoutlistener (this);
}
}); @Override public void onscrollchanged (int l, int t, int oldl, int Oldt) {if (T <= 0) {Lltitle.setbackgroundcolor (Co
Lor.argb ((int) 0, 255, 255, 255));
Statusbarutil.settranslucentforimageview (mainactivity.this, 0, title); else if (T > 0 && T <= height) {Float scale = (float) t/height int alpha = (int) (255 * scale); lltitl E.setbackgroundcolor ((int) Alpha, 227, 29, 26);//Set the transparency and color of the title bar Statusbarutil.settranslucentforimageview (
Mainactivity.this, Alpha, title);/Set the transparency of the status bar else {Lltitle.setbackgroundcolor (color.argb (int) 255, 227, 29, 26));
Statusbarutil.settranslucentforimageview (Mainactivity.this, 255, title); }
}
}
Note: stickyscrollview.setsticktop (int height) We can use this method to set the tab distance of how high to start the suspension
We do this by listening to the scrollview sliding distance to constantly change the transparency of our title bar and status bar to achieve results, where we calculate several heights (sliding distances). Finally, the total distance of the slide is calculated, and the value of the transparency is calculated according to the distance of the slide and the total distance of the slide.
Statusbarutil.settranslucentforimageview (mainactivity.this, 0, title); We use tools to implement the picture depth status bar. The view inside is the view below the picture.
The above is a small set to introduce the Android immersion status bar and suspension effect, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!