Add header and footer to list view without setting adapter - android

I have a simple list view, and I want to add header and footer to this list view without setting an adapter for it.
How do I add a header and footer to a ListView without setting an adapter, if it is possible?
xml code for listView:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
and code for adding header and footer:
ListView listView = (ListView)findViewById(R.id.listView);
Button headerButton = new Button(getApplicationContext());
headerButton.setText("Header");
listView.addHeaderView(headerButton);
Button footerButton = new Button(getApplicationContext());
footerButton.setText("Footer");
listView.addFooterView(footerButton);

You won't be able to achieve that.
The way header and footer layouts work with ListView is the following: Whenever you add a header or footer to your ListView, it will cause your own adapter to be wrapped by a WrapperListAdapter and basically make your header and footer items be treated as list items in their own way(handled by the adapter of course). Thus, your ListView can't really know about these items if it has no adapter.

Related

Android listview inside viewpager not swiping

I have a listview inside of a viewpager. I set an empty view on the listview. If this listview starts empty, when i add something to it then right or left swipes on the listview won't switch between tabs. If the listview starts with something (not empty) when i set the adapter on it, then i can swipe. If i remove the setEmptyView on the listview, it always works whether it starts with data or without it.
How can i set an empty view and still get the listview to swipe between tabs?
This is how i set the empty view:
mListview.setEmptyView(findViewById(R.id.empty_view));
the empty view is a textview directly below the listview in a linearlayout
the empty view xml:
empty_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/empty_list"
android:textColor="#color/lighter_gray"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/no_item"
android:textStyle="italic">
</TextView>
I would suggest removing your empty view from the current layout file, and move it into its own dedicated layout file. Then inflate this new layout in your class when you want to use it as an empty view. The end result would be something like this (not compiled, coding from memory):
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
mListview.setEmptyView(v);
or maybe
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, mListview, false);
mListview.setEmptyView(v);

How to add separator to listview (android)?

Is it possible to add separator view to listview without changing adapter's list of items?
I know that you can inflate different types of items with different layouts but that's not what i'm looking for. I dont want to change list of items which adapter uses. Only want to add separator at desired position.
Something similar to ListView#AddHeader. It adds view at the top of the list but doesn't change list of items.
I want to add view between two items, but don't want to change list of items.
Is this what you're looking for? dividerHeight property.
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:dividerHeight="2dp" >

Animate the switch between not empty listview and empty listview

I have a listview filled with data
at some point the user can wipeout that data and reveal the emptyview that i set up.
I want the listview to vanish from the screen animated, revealing the empty data.
How do that ?
you can use the empty view. Simply add a view with #android:id/empty to your layout and when the dataset is empty it will be automatically show. For instance:
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent" />

ListView with buttons

I have a ListView with few elements, each item contains two buttons with a selector so when I'm pressing the buttons the image change.
My problem is when I tap on the some ListView element the buttons on this specific ListView also changing like I pressed the buttons.
Is this a known issue??
Thank's.
listview xml:
<ListView
android:id="#+id/ViewIndex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:choiceMode="none"
android:fadingEdge="none" >
</ListView>
ListView listView = (ListView) findViewById(R.id.ViewIndex);
listView.setOnItemClickListener(new OnItemClickListener(){
}
I have a custom adapter, that add xml which contain two buttons.
My best guess would be that you are using the onclicklistener of the listview instead of the onclikclisteners of each individual button. If this is the case you should set onclicklisteners for each button and do your actions there.

Android addHeaderView disappears when no items in ListView

I am using addHeaderView to add a view item to the top of a ListView. I also have a TextView to display a message saying there are no items in the list.
Here is the layout:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/list_empty"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
And the Java code:
final ListView listView = getListView();
final View view = getLayoutInflater().inflate(R.layout.list_item_add,
listView, false);
listView.addHeaderView(view, null, true);
When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.
I would like the header view to be visible in the list view whether there are items in the list or not.
Thanks,
Remove the #android:id/empty view from your layout, or override/subclass your adapter to return false from isEmpty()
From my experience (SDK version 10):
Overriding isEmpty() in the adapter makes it work.
Then, it is optional to remove the #android:id/empty view.

Categories

Resources