How to fix the place of header in ListView? - android

I have read https://stackoverflow.com/a/2620252/779408 and know how to add a header to listview. But this header is not fixed when the user scroll down the list.
How can I fix the place of the header of ListView when the user scroll the list and make it visible always on top?

The better way is set the header statically in a separated layout (ouside your ListView). An example that illustrates this could be:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="#+id/YOUR_HEADER_VIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You have to replace the View with your header's view.
I hope this solve your problem.

maybe just put your header out of the list. make the view to stick on top of the list and work on the design so it looks like it is inside the list.

Related

Android: I have a fixed header for a listview but the "glow effect" is only for the listview

I'm trying to make a layout similar to the people app, so I have a list with a fixed header at the top. I define the header on top of the listview inside a relative layout but the "glow effect" at the top of the list is only for the listview and not the whole layout.
This is what I have:
and this is what I want:
This is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:minHeight="20dp"
android:orientation="vertical" >
<TextView
android:id="#+id/status_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/last_updated"
android:textSize="12sp"
android:textStyle="bold"
android:textAllCaps="true"
android:gravity="center|left"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:textColor="#color/nice_blue"
/>
<View style="#style/HeaderDivider"/>
</LinearLayout>
<ListView
android:id="#+id/status_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:layout_below="#+id/header_layout" >
</ListView>
The glow effect in both examples are functioning the same.
What is happening within the People application is that it is also a ListView just like yours. The effect happens in the same place. The Me, #, A, etc headers are actually inside the ListView.
Option 1
To do what you are looking to do, put a header in the ListView, you'll also need to create a custom ListView. This means extending ListView.
It sounds more challenging than it is, but you should be able to find a number of examples online. Look for examples that extend a ListView rather than creating a custom ListView.
The general idea is that you'll add in a TextView into the the View of the ListView that will act as your header. This will put the header text Last updated at inside of the ListView view that you've created and since it is inside, it will be included underneath the glow.
Option 2
You may be able to use the addHeaderView(View v, Object data, boolean isSelectable) or addHeaderView(View v) methods on the ListView itself.
I believe this will add a header row inside the ListView, but I've never used this so I can't say for sure how it works. This may be easier, but will be less flexible.

android:headerDividersEnabled isn't working

I want to have a divider at the top of a ListView (which is placed in RelativeLayout), but when I try to use this, I get only the bottom divider:
<ListView
android:id="#+id/last_contacts_picked_list"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:paddingTop="50dp"
android:dividerHeight="5dp"
android:layout_marginTop="50dp"
android:divider="#android:drawable/divider_horizontal_textfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="..."
android:layout_alignLeft="..."
android:layout_alignRight="..."
android:layout_below="..."
/>
I've got something like this:
http://i051.radikal.ru/1211/4f/a408db5e717f.png
Why doesn't it have the header divider too?
That is not what android:headerDividersEnabled is supposed to do. It means that if you are adding a list header whether that header needs to be separated by a divider or not.
For your problem simply use a linearlayout with ImageView having the divider as source followed by ListView. Hope this helps.

Android: Nested List Views and item heights

I am currently trying to create a nested ListView.
An outer ListView contains items, of which each consists of a TextView, as caption, and another ListView with children.
Filling the ListViews works really nice using another nested ListView adapter, yet, when it comes to actually displaying the content on the device, the nested list items are not high enough to display all the children. To be exact, only the first nested list item is displayed.
My current layout approach is the following:
Outer list item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5px"
>
<TextView android:id="#+id/day"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#ff5e00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="false"/>
<ListView android:id="#+id/innerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fadingEdge="none"
/>
</LinearLayout>
and an inner list layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5px"
>
<TextView android:id="#+id/innertextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:singleLine="false"/>
</LinearLayout>
Is there any chance to adapt the size of the outer list item in a way that all the inner list items will be displayed?
I also looked at ExpandableListViews. This approach worked, yet, I do not want to have the list items collapsed.
Thank you very much,
Matthias
I did not really like this solution, so I looked into this issue some more and found another solution:
ExpandableListView inherits from ListView. What it does is overriding the getView method from BaseAdapter and make a distinction between child and parent items. It is not a real nested list, but it flattens the list by using different layout items.
The same can be done for creating a NestedListView. Inherit from ListView and expect a self created adapter, which makes a distinction between child and parent layout items.
For each one return the appropriately filled layout items.
Done.
You should be able to make the items in an ExpandableListView uncollaposable. This is probably you best option. For instructions on how disable collapsing, checkout this SO Post.

Android layout - listview and edittext below

I'm trying to mimic the behaviour of the HTC SMS application (tradional view), where all messages are shown, and an EditTextis shown below. As you can see in the screenshot, when scrolling upwards, the EditText scrolls away at the bottom.
I'm stuck with this, even after reading multiple posts (eg Android Layout with ListView and Buttons and this website: http://www.finalconcept.com.au/article/view/android-keeping-buttons-visible), it's not working as expected.
Thanks to the comments and EditText now showing under ListView, I've managed to have my ListView take all available space and start scrolling once completed. The EditText is showing at the bottom of the screen now - always. I'd like it to disappear at the bottom when I scroll up though - now it remains at the bottom
Current Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TableLayout
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<EditText android:id="#+id/newmessagecontent"
android:layout_height="150dp"
android:singleLine="false"
android:gravity="top"
android:layout_width="250dp"
android:layout_alignParentTop="true"
/>
<Button android:layout_height="wrap_content"
android:id="#+id/sendmessage"
android:text="Send"
android:layout_width="wrap_content"
/>
</TableRow>
</TableLayout>
</LinearLayout>
i think what you need to implement here is some sort of modification of the SeparatedListAdapter from Jeff Sharkey from this Article. In this article he not only manages to add two Adapters to a ListView but also explains how to have Headers to separate them if you want (you can remove that part of the code).
So what i mean, is your first Adapter will be the data with It's rows, and the second Adapter will be a dummy one with no data that just points to a View with your controls or whatever.
this way the ListView and what you want to add at the bottom are gonna be all scrollable.
Hope this helps.
A ListView automatically scrolls if all the items in it take up more space than the view provides. What happens if you remove the ScrollView?

footer View issue in android

In my android app when the no of items in the list is small the footer sticks to end of the list like the one shown below.
but not to the end of screen.
Is there any way that i can stick it to the bottom of the screen.
I am using a seperate footer.xml and am calling it using Inflator service.
Please share your valuable suggestions.
Thanks in advance:)
Use Relative layout and align it to bottom of parent:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<YourFooter
android:id="#+id/SomethingSomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Categories

Resources