DO NOT MERGE: ListView transient state fix

ListView child views with transientState (setHasTransientState(true)) are not
handled correctly when the data set changes, such as when an item is added
or removed. The problem is that the transient views are cached by their
position, but this position is out of sync between the ListView and the adapter
until the ListView layout process is complete.

A better way, which unfortunately only works on ListViews with stable IDs, is
to cache the views by their itemID instead, and to use that ID to determine when
and where to reuse/retrieve a transient view during the ListView layout.

Issue #8254775 View.setHasTransient state has side-effects when deleting content in ListView

Change-Id: I2fc25e71ed6655af30b9c3f47fdf014e9b667616
This commit is contained in:
Chet Haase
2013-02-26 16:12:13 -07:00
parent 952e4805f7
commit 728713258f
2 changed files with 58 additions and 17 deletions

View File

@@ -6075,8 +6075,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
mTransientStateCount = 0;
Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
"unmatched pair of setHasTransientState calls");
}
if ((hasTransientState && mTransientStateCount == 1) ||
} else if ((hasTransientState && mTransientStateCount == 1) ||
(!hasTransientState && mTransientStateCount == 0)) {
// update flag if we've just incremented up from 0 or decremented down to 0
mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |

View File

@@ -6167,6 +6167,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
private ArrayList<View> mSkippedScrap;
private SparseArray<View> mTransientStateViews;
private LongSparseArray<View> mTransientStateViewsById;
public void setViewTypeCount(int viewTypeCount) {
if (viewTypeCount < 1) {
@@ -6205,6 +6206,12 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
mTransientStateViews.valueAt(i).forceLayout();
}
}
if (mTransientStateViewsById != null) {
final int count = mTransientStateViewsById.size();
for (int i = 0; i < count; i++) {
mTransientStateViewsById.valueAt(i).forceLayout();
}
}
}
public boolean shouldRecycleViewType(int viewType) {
@@ -6234,6 +6241,9 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
if (mTransientStateViews != null) {
mTransientStateViews.clear();
}
if (mTransientStateViewsById != null) {
mTransientStateViewsById.clear();
}
}
/**
@@ -6281,16 +6291,21 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
}
View getTransientStateView(int position) {
if (mTransientStateViews == null) {
return null;
if (mAdapter != null && mAdapterHasStableIds && mTransientStateViewsById != null) {
long id = mAdapter.getItemId(position);
View result = mTransientStateViewsById.get(id);
mTransientStateViewsById.remove(id);
return result;
}
final int index = mTransientStateViews.indexOfKey(position);
if (index < 0) {
return null;
if (mTransientStateViews != null) {
final int index = mTransientStateViews.indexOfKey(position);
if (index >= 0) {
View result = mTransientStateViews.valueAt(index);
mTransientStateViews.removeAt(index);
return result;
}
}
final View result = mTransientStateViews.valueAt(index);
mTransientStateViews.removeAt(index);
return result;
return null;
}
/**
@@ -6300,6 +6315,9 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
if (mTransientStateViews != null) {
mTransientStateViews.clear();
}
if (mTransientStateViewsById != null) {
mTransientStateViewsById.clear();
}
}
/**
@@ -6342,11 +6360,18 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
mSkippedScrap.add(scrap);
}
if (scrapHasTransientState) {
if (mTransientStateViews == null) {
mTransientStateViews = new SparseArray<View>();
}
scrap.dispatchStartTemporaryDetach();
mTransientStateViews.put(position, scrap);
if (mAdapter != null && mAdapterHasStableIds) {
if (mTransientStateViewsById == null) {
mTransientStateViewsById = new LongSparseArray<View>();
}
mTransientStateViewsById.put(lp.itemId, scrap);
} else {
if (mTransientStateViews == null) {
mTransientStateViews = new SparseArray<View>();
}
mTransientStateViews.put(position, scrap);
}
}
return;
}
@@ -6405,10 +6430,18 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
removeDetachedView(victim, false);
}
if (scrapHasTransientState) {
if (mTransientStateViews == null) {
mTransientStateViews = new SparseArray<View>();
if (mAdapter != null && mAdapterHasStableIds) {
if (mTransientStateViewsById == null) {
mTransientStateViewsById = new LongSparseArray<View>();
}
long id = mAdapter.getItemId(mFirstActivePosition + i);
mTransientStateViewsById.put(id, victim);
} else {
if (mTransientStateViews == null) {
mTransientStateViews = new SparseArray<View>();
}
mTransientStateViews.put(mFirstActivePosition + i, victim);
}
mTransientStateViews.put(mFirstActivePosition + i, victim);
}
continue;
}
@@ -6457,6 +6490,15 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
}
}
}
if (mTransientStateViewsById != null) {
for (int i = 0; i < mTransientStateViewsById.size(); i++) {
final View v = mTransientStateViewsById.valueAt(i);
if (!v.hasTransientState()) {
mTransientStateViewsById.removeAt(i);
i--;
}
}
}
}
/**