FileDocCategorySizeDatePackage
PreferenceGroupAdapter.javaAPI DocAndroid 5.1 API10982Thu Mar 12 22:22:10 GMT 2015android.preference

PreferenceGroupAdapter

public class PreferenceGroupAdapter extends android.widget.BaseAdapter implements android.preference.Preference.OnPreferenceChangeInternalListener
An adapter that returns the {@link Preference} contained in this group. In most cases, this adapter should be the base class for any custom adapters from {@link Preference#getAdapter()}.

This adapter obeys the {@link Preference}'s adapter rule (the {@link Adapter#getView(int, View, ViewGroup)} should be used instead of {@link Preference#getView(ViewGroup)} if a {@link Preference} has an adapter via {@link Preference#getAdapter()}).

This adapter also propagates data change/invalidated notifications upward.

This adapter does not include this {@link PreferenceGroup} in the returned adapter, use {@link PreferenceCategoryAdapter} instead.

see
PreferenceCategoryAdapter
hide

Fields Summary
private static final String
TAG
private PreferenceGroup
mPreferenceGroup
The group that we are providing data from.
private List
mPreferenceList
Maps a position into this adapter -> {@link Preference}. These {@link Preference}s don't have to be direct children of this {@link PreferenceGroup}, they can be grand children or younger)
private ArrayList
mPreferenceLayouts
List of unique Preference and its subclasses' names. This is used to find out how many types of views this adapter can return. Once the count is returned, this cannot be modified (since the ListView only checks the count once--when the adapter is being set). We will not recycle views for Preference subclasses seen after the count has been returned.
private PreferenceLayout
mTempPreferenceLayout
private boolean
mHasReturnedViewTypeCount
Blocks the mPreferenceClassNames from being changed anymore.
private volatile boolean
mIsSyncing
private android.os.Handler
mHandler
private Runnable
mSyncRunnable
private int
mHighlightedPosition
private android.graphics.drawable.Drawable
mHighlightedDrawable
private static ViewGroup.LayoutParams
sWrapperLayoutParams
Constructors Summary
public PreferenceGroupAdapter(PreferenceGroup preferenceGroup)

        mPreferenceGroup = preferenceGroup;
        // If this group gets or loses any children, let us know
        mPreferenceGroup.setOnPreferenceChangeInternalListener(this);

        mPreferenceList = new ArrayList<Preference>();
        mPreferenceLayouts = new ArrayList<PreferenceLayout>();

        syncMyPreferences();
    
Methods Summary
private voidaddPreferenceClassName(Preference preference)

        final PreferenceLayout pl = createPreferenceLayout(preference, null);
        int insertPos = Collections.binarySearch(mPreferenceLayouts, pl);

        // Only insert if it doesn't exist (when it is negative).
        if (insertPos < 0) {
            // Convert to insert index
            insertPos = insertPos * -1 - 1;
            mPreferenceLayouts.add(insertPos, pl);
        }
    
public booleanareAllItemsEnabled()

        // There should always be a preference group, and these groups are always
        // disabled
        return false;
    
private android.preference.PreferenceGroupAdapter$PreferenceLayoutcreatePreferenceLayout(Preference preference, android.preference.PreferenceGroupAdapter$PreferenceLayout in)
Creates a string that includes the preference name, layout id and widget layout id. If a particular preference type uses 2 different resources, they will be treated as different view types.

        PreferenceLayout pl = in != null? in : new PreferenceLayout();
        pl.name = preference.getClass().getName();
        pl.resId = preference.getLayoutResource();
        pl.widgetResId = preference.getWidgetLayoutResource();
        return pl;
    
private voidflattenPreferenceGroup(java.util.List preferences, PreferenceGroup group)

        // TODO: shouldn't always?
        group.sortPreferences();

        final int groupSize = group.getPreferenceCount();
        for (int i = 0; i < groupSize; i++) {
            final Preference preference = group.getPreference(i);
            
            preferences.add(preference);
            
            if (!mHasReturnedViewTypeCount && preference.canRecycleLayout()) {
                addPreferenceClassName(preference);
            }
            
            if (preference instanceof PreferenceGroup) {
                final PreferenceGroup preferenceAsGroup = (PreferenceGroup) preference;
                if (preferenceAsGroup.isOnSameScreenAsChildren()) {
                    flattenPreferenceGroup(preferences, preferenceAsGroup);
                }
            }

            preference.setOnPreferenceChangeInternalListener(this);
        }
    
public intgetCount()

        return mPreferenceList.size();
    
private intgetHighlightItemViewType()

        return getViewTypeCount() - 1;
    
public PreferencegetItem(int position)

        if (position < 0 || position >= getCount()) return null;
        return mPreferenceList.get(position);
    
public longgetItemId(int position)

        if (position < 0 || position >= getCount()) return ListView.INVALID_ROW_ID;
        return this.getItem(position).getId();
    
public intgetItemViewType(int position)

        if (position == mHighlightedPosition) {
            return getHighlightItemViewType();
        }

        if (!mHasReturnedViewTypeCount) {
            mHasReturnedViewTypeCount = true;
        }
        
        final Preference preference = this.getItem(position);
        if (!preference.canRecycleLayout()) {
            return IGNORE_ITEM_VIEW_TYPE;
        }

        mTempPreferenceLayout = createPreferenceLayout(preference, mTempPreferenceLayout);

        int viewType = Collections.binarySearch(mPreferenceLayouts, mTempPreferenceLayout);
        if (viewType < 0) {
            // This is a class that was seen after we returned the count, so
            // don't recycle it.
            return IGNORE_ITEM_VIEW_TYPE;
        } else {
            return viewType;
        }
    
public android.view.ViewgetView(int position, android.view.View convertView, android.view.ViewGroup parent)

        final Preference preference = this.getItem(position);
        // Build a PreferenceLayout to compare with known ones that are cacheable.
        mTempPreferenceLayout = createPreferenceLayout(preference, mTempPreferenceLayout);

        // If it's not one of the cached ones, set the convertView to null so that 
        // the layout gets re-created by the Preference.
        if (Collections.binarySearch(mPreferenceLayouts, mTempPreferenceLayout) < 0 ||
                (getItemViewType(position) == getHighlightItemViewType())) {
            convertView = null;
        }
        View result = preference.getView(convertView, parent);
        if (position == mHighlightedPosition && mHighlightedDrawable != null) {
            ViewGroup wrapper = new FrameLayout(parent.getContext());
            wrapper.setLayoutParams(sWrapperLayoutParams);
            wrapper.setBackgroundDrawable(mHighlightedDrawable);
            wrapper.addView(result);
            result = wrapper;
        }
        return result;
    
public intgetViewTypeCount()

        if (!mHasReturnedViewTypeCount) {
            mHasReturnedViewTypeCount = true;
        }
        
        return Math.max(1, mPreferenceLayouts.size()) + 1;
    
public booleanhasStableIds()

        return true;
    
public booleanisEnabled(int position)

        if (position < 0 || position >= getCount()) return true;
        return this.getItem(position).isSelectable();
    
public voidonPreferenceChange(Preference preference)

        notifyDataSetChanged();
    
public voidonPreferenceHierarchyChange(Preference preference)

        mHandler.removeCallbacks(mSyncRunnable);
        mHandler.post(mSyncRunnable);
    
public voidsetHighlighted(int position)

hide

        mHighlightedPosition = position;
    
public voidsetHighlightedDrawable(android.graphics.drawable.Drawable drawable)

hide

        mHighlightedDrawable = drawable;
    
private voidsyncMyPreferences()

        synchronized(this) {
            if (mIsSyncing) {
                return;
            }

            mIsSyncing = true;
        }

        List<Preference> newPreferenceList = new ArrayList<Preference>(mPreferenceList.size());
        flattenPreferenceGroup(newPreferenceList, mPreferenceGroup);
        mPreferenceList = newPreferenceList;
        
        notifyDataSetChanged();

        synchronized(this) {
            mIsSyncing = false;
            notifyAll();
        }