First, the following effects are achieved:
At first, the search on the internet to achieve such an effect, the United States network, the public comments on the "buy box" suspension effect is also the case, but the author to achieve more trouble, I think about it according to the features of the ListView provided a simple implementation.
The entire main layout is a ListView, if the ListView has content and high height, it can be used as the header of the ListView, This also avoids the hassle of scrollview nesting the ListView (keep in mind that there will be no ScrollView and listview problems in the usual way). To achieve the hover effect, the main operation is based on which entry is the first entry visible to the ListView. Note that the "hover" here is actually done by hiding/showing the hover part: There is a hidden "hover section" in the ListView "behind", when the suspended portion of the ListView entry becomes the first visible part of the ListView, The floating portion of the non-ListView entry is displayed so that the "hover" part is floating at the top, while the first entry in the ListView section is not suspended, and the hidden part of the Non-ListView section continues to hide. Expressive ability is not strong, oneself are almost dizzy by themselves, in short, is 2 suspension parts: ListView entry and in the top of the layout of the hidden (gone/invisible) is not shown at the beginning, through the two parts of the operation to complete the function. I hope readers can understand ...
Main code:
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_sticky); Invis = (linearlayout) Findviewbyid (r.id.invis); STRs = new String[100];for (int i = 0; i < 20 ; i++) {Strs[i] = "Data-----" + I;} LV = (ListView) Findviewbyid (r.id.lv); View Header = View.inflate (this, r.layout.stick_header, null);//header Content Lv.addheaderview (header)//Add head Lv.addheaderview (View.inflate (this, r.layout.stick_action, null)); the suspended portion of the//listview entry is added to the head lv.setadapter (new arrayadapter<string > (this,android. R.layout.simple_list_item_1, STRs)); Lv.setonscrolllistener (new Onscrolllistener () {@Overridepublic void Onscrollstatechanged (abslistview view, int scrollstate) {} @Overridepublic void Onscroll (abslistview view, int Firstvisibleitem,int visibleitemcount, int totalitemcount) {if (Firstvisibleitem >= 1) {invis.setvisibility ( view.visible);} else {invis.setvisibility (view.gone);}});}
Layout:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "> <textview android: Id= "@+id/title" android:layout_width= "match_parent" android:layout_height= "30DP" android:background= " #332b3b "android:gravity=" center "android:text=" title "android:textcolor=" #ffffff "/> <framelay Out android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_below= "@i D/title "> <listview android:id=" @+id/lv "android:layout_width=" Match_parent " android:layout_height= "Match_parent"/> <linearlayout android:id= "@+id/invis" Android : layout_width= "fill_parent" android:layout_height= "50DP" android:background= "#ccedc7" and roid:orientation= "Horizontal" Android:visibility= "Gone" > <textview android:id= "@+id/tv" Android: Layout_width= "Match_parent" android:layout_height= "50DP" android:gravity= "center" android:text= "suspension section"/> </LinearLayout> </FrameLayout></RelativeLayout>
Code: http://download.csdn.net/download/ljfbest/7804769
Android Simple implementation of ListView top suspension effect