drag-sort-listview
drag-sort-listview copied to clipboard
Bug with headers/footers and cursor adapter?
Trying to implement a listview where items are draggable in order to remove them. It works fine unless the following lines are added:
final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View space = inflater.inflate(R.layout.list_header_space, null);
lv.addHeaderView(space, null, false);
lv.addFooterView(space, null, false);
Where lv is my dslv. The issue seems to be that I am using a cursor-adapter (your own wrapper). With headers/footers the list goes crazy. I think this might have something to do with the position mapping.
I could not reproduce a crazy (?) list by adding a header and footer to the CursorAdapter example in the demo project; re-orderings/removals worked just fine.
I found the problem now. It's not a bug, it was entirely my fault. Anyway, here's the reason and way to recreate it in the example app.
The reason was that I added the same view as both header and footer.
Taking CursorDSLV.java as an example, with header/footer code inserted right before the adapter is set on the listview:
adapter = new SimpleDragSortCursorAdapter(this,
R.layout.list_item_click_remove, null, cols, ids, 0);
DragSortListView dslv = (DragSortListView) findViewById(android.R.id.list);
LayoutInflater inflater = getLayoutInflater();
TextView header = (TextView) inflater.inflate(R.layout.header_footer, null);
header.setText("Header here");
dslv.addHeaderView(header, null, false);
TextView footer = (TextView) inflater.inflate(R.layout.header_footer, null);
footer.setText("Footer here");
// Using the second of these lines will not work
//dslv.addFooterView(footer, null, false);
dslv.addFooterView(header, null, false);
dslv.setAdapter(adapter);
Additionally, the behaviour only appears if the list is short enough to fit on one screen, e.g. the header and footer are both visible. In other words, in the case that the same view is present at two different locations in the screen. So delete items until the list is short enough and you'll experience some odd stuff.
For reference, here is the modified cursor_main.xml with drag-to-remove true which I used:
<?xml version="1.0" encoding="utf-8"?>
<com.mobeta.android.dslv.DragSortListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/com.mobeta.android.demodslv"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
android:layout_margin="3dp"
android:dividerHeight="1px"
dslv:drag_enabled="true"
dslv:collapsed_height="1px"
dslv:drag_scroll_start="0.33"
dslv:float_alpha="0.6"
dslv:slide_shuffle_speed="0.3"
dslv:sort_enabled="false"
dslv:drag_start_mode="onDown"
dslv:drag_handle_id="@id/drag_handle"
dslv:remove_enabled="true"
dslv:remove_mode="slideRight" />
So again, not a bug with your library. One can probably expect strange things from the ordinary listview in this case. Good solid library, thanks for the quick response!
Whoa. Okay now I see the crazies. This is no good. I will investigate. Thanks a bunch for the clear follow-up.
Reopened!
Hey, even if i have dslv.addHeaderView(viewOne); dslv.addHeaderView(viewTwo);
will it work?
I encountered the same problem.
Thanks @spacecowboy , I solved this problem when I inserted header/footer code before the adapter is set on the listview.