android - Making a middle element to get stuck in the header (ScrollView/ListView) -


i want make element shown in middle of scrollview (or listview) @ first , gets stuck in header of screen when it’s scrolled.

it’s prototype implementation in css+js: http://jsfiddle.net/minhee/apcv4/embedded/result/.

at first glance make scrollview include listview, official docs says:

you should never use scrollview listview, because listview takes care of own vertical scrolling. importantly, doing defeats of important optimizations in listview dealing large lists, since forces listview display entire list of items fill infinite container supplied scrollview.

so, approaches can try achieve ui?

update: tried stickylistheaders, but: “it not possible have interactive elements in header, buttons, switches, etc. work when header not stuck.” plus, find it’s not suitable situation. don’t need multiple headers, 1 middle element stuck in header.

i have used(or rather, tried use) stickylistheaders library in past. after having issues it, came following. not different other posters have suggested.

the main layout file activity_layout.xml consists of listview , linearlayout invisible default. using onscrolllistener's onscroll() method, linearlayout's visibility toggled. don't need inflate layout or add views dynamically layout's parent. onscroll method looks like:

public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {     if (firstvisibleitem > 3) {        // 5th row stick         llheader.setvisibility(view.visible);     } else {         llheader.setvisibility(view.gone);     } } 

simply, toggle visibility desired effect. can take @ following code. working example of can expect. activity contains listview strictly barebone extension of baseadapter. listview populated numbered buttons(one on each row, starting @ 0, , going 19).

public class stickyheader extends activity {          linearlayout llheader;       listview lv;     shadapter shadapter;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_layout);          lv = (listview) findviewbyid(r.id.listview1);                 llheader = (linearlayout) findviewbyid(r.id.llheader);                 shadapter = new shadapter();                 lv.setadapter(shadapter);          lv.setonscrolllistener(new onscrolllistener() {              @override             public void onscrollstatechanged(abslistview view, int scrollstate) {}              @override             public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {                 if (firstvisibleitem > 3) {                     llheader.setvisibility(view.visible);                 } else {                     llheader.setvisibility(view.gone);                 }             }         });     }         public class shadapter extends baseadapter {          button btcurrent;         int[] arr = new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};          @override         public int getcount() {             return 20;         }          @override         public object getitem(int arg0) {             return arr[arg0];         }          @override         public long getitemid(int position) {             return 0;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             convertview = getlayoutinflater().inflate(r.layout.list_item_layout, null);                      btcurrent = (button) convertview.findviewbyid(r.id.button1);                          if ((integer)getitem(position) == 4) {                 btcurrent.settext("number " + getitem(position) + " sticky");             } else {                 btcurrent.settext("" + getitem(position));             }              return convertview;         }     } } 

activity_layout.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >      <listview         android:id="@+id/listview1"         android:layout_width="match_parent"         android:layout_height="wrap_content" >     </listview>      <!-- linearlayout's visibility toggled -->      <!-- improvement suggested user 'ar34z'           (see comment section below) -->      <include layout="@layout/list_item_layout" />  </relativelayout> 

list_item_layout

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/llheader"     android:layout_width="match_parent"     android:layout_height="50dp"     android:background="@color/white"     android:orientation="vertical" >      <button         android:id="@+id/button1"         android:layout_gravity="center"         android:layout_width="match_parent"         android:layout_height="wrap_content" />  </linearlayout> 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -