Add support for saving and using the Preference's key value

- make public and hide the PreferenceGroupAdapter class
- add setActivated(int position) to be able to set the activated item
- udpate getView(int position, View convertView, ViewGroup parent) to set/unset
the activated item

Change-Id: I8a653ad6541bb6b7a043ba7c3d95e5ecad924a0d
This commit is contained in:
Fabrice Di Meglio
2014-04-07 19:12:41 -07:00
parent 19de4ffff3
commit 53c2cf799f
2 changed files with 15 additions and 4 deletions

View File

@@ -45,8 +45,11 @@ import android.widget.ListView;
* adapter, use {@link PreferenceCategoryAdapter} instead.
*
* @see PreferenceCategoryAdapter
*
* @hide
*/
class PreferenceGroupAdapter extends BaseAdapter implements OnPreferenceChangeInternalListener {
public class PreferenceGroupAdapter extends BaseAdapter
implements OnPreferenceChangeInternalListener {
private static final String TAG = "PreferenceGroupAdapter";
@@ -88,6 +91,8 @@ class PreferenceGroupAdapter extends BaseAdapter implements OnPreferenceChangeIn
}
};
private int mActivatedPosition = -1;
private static class PreferenceLayout implements Comparable<PreferenceLayout> {
private int resId;
private int widgetResId;
@@ -207,6 +212,10 @@ class PreferenceGroupAdapter extends BaseAdapter implements OnPreferenceChangeIn
return this.getItem(position).getId();
}
public void setActivated(int position) {
mActivatedPosition = position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final Preference preference = this.getItem(position);
// Build a PreferenceLayout to compare with known ones that are cacheable.
@@ -217,8 +226,9 @@ class PreferenceGroupAdapter extends BaseAdapter implements OnPreferenceChangeIn
if (Collections.binarySearch(mPreferenceLayouts, mTempPreferenceLayout) < 0) {
convertView = null;
}
return preference.getView(convertView, parent);
View result = preference.getView(convertView, parent);
result.setActivated(position == mActivatedPosition);
return result;
}
@Override

View File

@@ -23,7 +23,8 @@
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="@dimen/preference_item_padding_side"
android:paddingEnd="?android:attr/scrollbarSize">
android:paddingEnd="?android:attr/scrollbarSize"
android:background="?android:attr/activatedBackgroundIndicator">
<LinearLayout
android:layout_width="wrap_content"