I'm trying to create a layout that contains a ListView
with a button underneath it, and also make it so that the button 'sticks' to the bottom of the ListView, even when I add or delete a row from the ListView.
With the layout code below, when I add a new row to the list, the button 'moves' to its correct location right beneath the bottom of the ListView. So that works fine.
The problem is when I delete a row from the ListView, the button stays where it is and doesn't 'move up' so that it sticks to the bottom of the ListView. When I rotate the device and it recreates the view, the button does in fact move up, but I'd like it to automatically move up when a row is deleted.
Here is the code I have now:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<ImageButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_button" />
</LinearLayout>
SOLUTION:
The solution was to create a small layout file that contains just the button, then add it as the footer of the ListView programatically.
Inside my fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
...
View footerView = inflater.inflate(R.layout.listview_footer, null, false);
addButton = (ImageButton) footerView.findViewById(R.id.addButton);
listView.addFooterView(footerView);
data = getListViewData();
adapter = new MyListAdapter(getActivity(), data);
listView.setAdapter(adapter);
...
}
listview_footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_button" />
The listview_footer.xml just contains the button, and no layout.
Now, when I delete a row from the ListView
, the button moves up to 'stick' to the bottom of the ListView. And as before, when I add a row to the ListView, the button moves down below it.