FileDocCategorySizeDatePackage
ViewsStateBundle.javaAPI DocAndroid 5.1 API7080Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

ViewsStateBundle

public class ViewsStateBundle extends Object
Maintains a bundle of states for a group of views. Each view must have a unique id to identify it. There are four different strategies {@link #SAVE_NO_CHILD} {@link #SAVE_ON_SCREEN_CHILD} {@link #SAVE_LIMITED_CHILD} {@link #SAVE_ALL_CHILD}.

This class serves purpose of nested "listview" e.g. a vertical list of horizontal list. Vertical list maintains id->bundle mapping of all it's children (even the children is offscreen and being pruned).

The class is currently used within {@link GridLayoutManager}, but it might be used by other ViewGroup.

Fields Summary
public static final int
LIMIT_DEFAULT
public static final int
UNLIMITED
private int
mSavePolicy
private int
mLimitNumber
private android.support.v4.util.LruCache
mChildStates
Constructors Summary
public ViewsStateBundle()


      
        mSavePolicy = SAVE_NO_CHILD;
        mLimitNumber = LIMIT_DEFAULT;
    
Methods Summary
protected voidapplyPolicyChanges()

        if (mSavePolicy == SAVE_LIMITED_CHILD) {
            if (mLimitNumber <= 0) {
                throw new IllegalArgumentException();
            }
            if (mChildStates == null || mChildStates.maxSize() != mLimitNumber) {
                mChildStates = new LruCache<String, SparseArray<Parcelable>>(mLimitNumber);
            }
        } else if (mSavePolicy == SAVE_ALL_CHILD || mSavePolicy == SAVE_ON_SCREEN_CHILD) {
            if (mChildStates == null || mChildStates.maxSize() != UNLIMITED) {
                mChildStates = new LruCache<String, SparseArray<Parcelable>>(UNLIMITED);
            }
        } else {
            mChildStates = null;
        }
    
public voidclear()

        if (mChildStates != null) {
            mChildStates.evictAll();
        }
    
public final intgetLimitNumber()

return
the limitNumber, only works when {@link #getSavePolicy()} is {@link #SAVE_LIMITED_CHILD}

        return mLimitNumber;
    
public final intgetSavePolicy()

return
the savePolicy, see {@link #SAVE_NO_CHILD} {@link #SAVE_ON_SCREEN_CHILD} {@link #SAVE_LIMITED_CHILD} {@link #SAVE_ALL_CHILD}

        return mSavePolicy;
    
static java.lang.StringgetSaveStatesKey(int id)

        return Integer.toString(id);
    
public final voidloadFromBundle(android.os.Bundle savedBundle)

        if (mChildStates != null && savedBundle != null) {
            mChildStates.evictAll();
            for (Iterator<String> i = savedBundle.keySet().iterator(); i.hasNext(); ) {
                String key = i.next();
                mChildStates.put(key, savedBundle.getSparseParcelableArray(key));
            }
        }
    
public final voidloadView(android.view.View view, int id)
Load view from states, it's none operation if the there is no state associated with the id.

param
view view where loads into
param
id unique id for the view within this ViewsStateBundle

        if (mChildStates != null) {
            String key = getSaveStatesKey(id);
            SparseArray<Parcelable> container = mChildStates.get(key);
            if (container != null) {
                view.restoreHierarchyState(container);
            }
        }
    
public voidremove(int id)

        if (mChildStates != null && mChildStates.size() != 0) {
            mChildStates.remove(getSaveStatesKey(id));
        }
    
public final android.os.BundlesaveAsBundle()

return
the saved views states

        if (mChildStates == null || mChildStates.size() == 0) {
            return null;
        }
        Map<String, SparseArray<Parcelable>> snapshot = mChildStates.snapshot();
        Bundle bundle = new Bundle();
        for (Iterator<Entry<String, SparseArray<Parcelable>>> i =
                snapshot.entrySet().iterator(); i.hasNext(); ) {
            Entry<String, SparseArray<Parcelable>> e = i.next();
            bundle.putSparseParcelableArray(e.getKey(), e.getValue());
        }
        return bundle;
    
public final voidsaveOffscreenView(android.view.View view, int id)
Save off screen views according to policy.

param
view view to save
param
id unique id for the view within this ViewsStateBundle

        switch (mSavePolicy) {
            case SAVE_LIMITED_CHILD:
            case SAVE_ALL_CHILD:
                saveViewUnchecked(view, id);
                break;
            case SAVE_ON_SCREEN_CHILD:
                remove(id);
                break;
            default:
                break;
        }
    
public final voidsaveOnScreenView(android.view.View view, int id)
The on screen view is saved when policy is not {@link #SAVE_NO_CHILD}.

param
view
param
id

        if (mSavePolicy != SAVE_NO_CHILD) {
            saveViewUnchecked(view, id);
        }
    
protected final voidsaveViewUnchecked(android.view.View view, int id)
Save views regardless what's the current policy is.

param
view view to save
param
id unique id for the view within this ViewsStateBundle

        if (mChildStates != null) {
            String key = getSaveStatesKey(id);
            SparseArray<Parcelable> container = new SparseArray<Parcelable>();
            view.saveHierarchyState(container);
            mChildStates.put(key, container);
        }
    
public final voidsetLimitNumber(int limitNumber)

see
ViewsStateBundle#getLimitNumber()

        this.mLimitNumber = limitNumber;
        applyPolicyChanges();
    
public final voidsetSavePolicy(int savePolicy)

see
ViewsStateBundle#getSavePolicy()

        this.mSavePolicy = savePolicy;
        applyPolicyChanges();