diff options
| author | Adam Cohen <adamcohen@google.com> | 2012-01-05 13:58:47 -0800 |
|---|---|---|
| committer | Adam Cohen <adamcohen@google.com> | 2012-01-17 15:21:34 -0800 |
| commit | b967392e0170af8cfd8053fd43fcdf8c46f703e9 (patch) | |
| tree | 7127ae3b8dd9a7fff99f1993016e14535a721c88 /core/java/android/widget/RemoteViewsAdapter.java | |
| parent | d9bb7a8df856d477701158c2cc5c40b86a2c70f5 (diff) | |
Hinting RemoteViewsAdapter as to which views are visible
-> This prevents collection widgets from flashing loading
views when they are updated with new content
Change-Id: I1241ff9a09edfd990ad03f76449d18b9359246b4
Diffstat (limited to 'core/java/android/widget/RemoteViewsAdapter.java')
| -rw-r--r-- | core/java/android/widget/RemoteViewsAdapter.java | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/core/java/android/widget/RemoteViewsAdapter.java b/core/java/android/widget/RemoteViewsAdapter.java index 7b430328ef0f..586fdf4da501 100644 --- a/core/java/android/widget/RemoteViewsAdapter.java +++ b/core/java/android/widget/RemoteViewsAdapter.java @@ -68,6 +68,8 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback private RemoteViewsAdapterServiceConnection mServiceConnection; private WeakReference<RemoteAdapterConnectionCallback> mCallback; private FixedSizeRemoteViewsCache mCache; + private int mVisibleWindowLowerBound; + private int mVisibleWindowUpperBound; // A flag to determine whether we should notify data set changed after we connect private boolean mNotifyDataSetChangedAfterOnServiceConnected = false; @@ -765,7 +767,7 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback } if (position > -1) { // Load the item, and notify any existing RemoteViewsFrameLayouts - updateRemoteViews(position, isRequested); + updateRemoteViews(position, isRequested, true); // Queue up for the next one to load loadNextIndexInBackground(); @@ -827,8 +829,8 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback } } - private void updateRemoteViews(final int position, boolean isRequested) { - if (!mServiceConnection.isConnected()) return; + private void updateRemoteViews(final int position, boolean isRequested, boolean + notifyWhenLoaded) { IRemoteViewsFactory factory = mServiceConnection.getRemoteViewsFactory(); // Load the item information from the remote service @@ -864,12 +866,14 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback // there is new data for it. final RemoteViews rv = remoteViews; final int typeId = mCache.getMetaDataAt(position).typeId; - mMainQueue.post(new Runnable() { - @Override - public void run() { - mRequestedViews.notifyOnRemoteViewsLoaded(position, rv, typeId); - } - }); + if (notifyWhenLoaded) { + mMainQueue.post(new Runnable() { + @Override + public void run() { + mRequestedViews.notifyOnRemoteViewsLoaded(position, rv, typeId); + } + }); + } } } @@ -929,6 +933,16 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback return typeId; } + /** + * This method allows an AdapterView using this Adapter to provide information about which + * views are currently being displayed. This allows for certain optimizations and preloading + * which wouldn't otherwise be possible. + */ + public void setVisibleRangeHint(int lowerBound, int upperBound) { + mVisibleWindowLowerBound = lowerBound; + mVisibleWindowUpperBound = upperBound; + } + public View getView(int position, View convertView, ViewGroup parent) { // "Request" an index so that we can queue it for loading, initiate subsequent // preloading, etc. @@ -1059,6 +1073,13 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback // Re-request the new metadata (only after the notification to the factory) updateTemporaryMetaData(); + // Pre-load (our best guess of) the views which are currently visible in the AdapterView. + // This mitigates flashing and flickering of loading views when a widget notifies that + // its data has changed. + for (int i = mVisibleWindowLowerBound; i <= mVisibleWindowUpperBound; i++) { + updateRemoteViews(i, false, false); + } + // Propagate the notification back to the base adapter mMainQueue.post(new Runnable() { @Override |
