Issue
My goal is to have a sticky header, a clear button, and then a ListView (in that order).
+-----------------+
| Header | (Sticky)
+-----------------+
| "Clear" button | (NOT sticky)
+-----------------+
| ListView | (Also NOT sticky)
| |
| |
| |
| |
| |
| |
+-----------------+
This following approach almost does what I want. The header is sticky, and the ListView functions correctly. But the button is sticky (it tags along right below the sticky header).
<RelativeLayout>
<!-- Used for Sticky Header -->
<RelativeLayout
android:id="@+id/top_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<Button/>
<TextView/>
</RelativeLayout> <!-- End XML for sticky hader -->
<!-- Button to clear bookmarks -->
<Button
android:id="@+id/clearButton"
android:text="CLEAR"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/top_control_bar"
android:onClick="clearBookmarks"/>
<!-- ListView for displaying questions -->
<ListView
android:id="@+id/listViewBrowse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/clearButton" >
</ListView>
</RelativeLayout>
I think I need to stop using this property
android:layout_below="@+id/top_control_bar"
for the "clear" button. But it doesn't work if I just get rid of it (the button overlays the header if I do that). What should I adjust? Or what should I try instead of this approach?
* **SOLUTION ***
Following these steps solved my problem:
- Create a new layout file: non_sticky_header.xml (which contains my clear button).
- Remove Clear button from the main layout.
- Dynamically create a View object by inflating my 'non_sticky_header' resource file. This View object is the header.
- Add header object to my ListView, with addHeaderView().
Code for Steps 3 and 4:
View header = getLayoutInflater().inflate(R.layout.non_sticky_header, null);
list.addHeaderView(header);
Sources
-See third answer to this post from @nDroidDev; my solution varies slightly with steps 3 and 4.
-(See this S.O. Post about using addHeaderView(), which explains steps 3 and 4 (from @user370305).
Solution
I think , instead to add ing the clear button into the Realtive layout, you should add it in your listview's header like this..
1) Create a new layout file say non_sticky_header.xml and add the following lines..
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Button to clear bookmarks -->
<Button
android:id="@+id/clearButton"
android:text="CLEAR"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/top_control_bar"
android:onClick="clearBookmarks"/>
</RelativeLayout>
2) Now remove this clear button from your main layout and update it like this... Just keep the stick headre and listview in your main layout..
<RelativeLayout>
<!-- Used for Sticky Header -->
<RelativeLayout
android:id="@+id/top_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<Button/>
<TextView/>
</RelativeLayout> <!-- End XML for sticky hader -->
<!-- ListView for displaying questions -->
<ListView
android:id="@+id/listViewBrowse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/clearButton" >
</ListView>
</RelativeLayout>
3) At last, Add the non_sticky_header.xml as header of the list in you java file, like this...
//code to add header and footer to listview
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.non_sticky_header, listView, false);
listView.addHeaderView(header, null, false);
Hope it helps... :)
Answered By - iMDroid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.