Summary: An attempt to dynamically add header lines to a ListView using a special adapter shell. ListView has problems synchronizing scroll position. A Runnable demo project is provided.
I would like to dynamically add items to the list based on the values in the CursorAdapter, by several positions that exceed what the user is currently viewing. To do this, I have an adapter that wraps the CursorAdapter and saves the new content indexed in SparseArray. The ListView needs to be updated when items are added to the user adapter, but I met many pitfalls, trying to get them to work, and would like some advice.
Demo project can be downloaded here: DynamicSectionedList.zip
In the demo, headers are added dynamically, looking at 10 places to find the correct position in which the list items switch to the next letter. Each implementation of notifyDataSetChanged has the problems described below:
Demo 1
This demo shows the value of notifyDataSetChanged (). Clicking on any application will crash. This is due to a health check in ... the ListView mItemCount != adapter.getItemCount(). Moral - we need to notify the list that changed the data.
Demo 2 The
natural next step is to notify ListView of changes when changes occur. Unfortunately, by doing this while the ListView scrolls, all touch interaction breaks until the application switches from touch mode. You will need to “scroll the scroll” far enough to generate new headers to notice this. Clicking on the screen will not stop scrolling, and after that none of the list items will be available for click. This is due to some code if (!mDataChanged) { /* do very important stuff */ }in AbsListView.onTouchEvent ().
3
, Demo 3 pendingChanges, Adapter notifyDataSetChangedIfNeeded(), ListView, "" . , , ListView.layoutChildren(), , , , . , .
, , . / . , .
4
Demo 3 , , . notifyDataSetChangedIfNeeded() , , , , .
, , , . , , .
Android , , . , , , !
, , , , , .