Add Preference highlighting
- use a specific drawable for highlighting a Preference
at a given position
- also add PreferenceFragment.hasListView() as hidden API
Change-Id: If69854cf6c4852d0f45e2c3a9734b1f63b352f9d
diff --git a/core/java/android/preference/PreferenceFragment.java b/core/java/android/preference/PreferenceFragment.java
index 11d8878..325b2e6 100644
--- a/core/java/android/preference/PreferenceFragment.java
+++ b/core/java/android/preference/PreferenceFragment.java
@@ -337,6 +337,26 @@
return mList;
}
+ /** @hide */
+ public boolean hasListView() {
+ if (mList != null) {
+ return true;
+ }
+ View root = getView();
+ if (root == null) {
+ return false;
+ }
+ View rawListView = root.findViewById(android.R.id.list);
+ if (!(rawListView instanceof ListView)) {
+ return false;
+ }
+ mList = (ListView)rawListView;
+ if (mList == null) {
+ return false;
+ }
+ return true;
+ }
+
private void ensureList() {
if (mList != null) {
return;
diff --git a/core/java/android/preference/PreferenceGroupAdapter.java b/core/java/android/preference/PreferenceGroupAdapter.java
index c2e1f51..9b41ff0 100644
--- a/core/java/android/preference/PreferenceGroupAdapter.java
+++ b/core/java/android/preference/PreferenceGroupAdapter.java
@@ -20,6 +20,8 @@
import java.util.Collections;
import java.util.List;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.preference.Preference.OnPreferenceChangeInternalListener;
import android.view.View;
@@ -91,7 +93,8 @@
}
};
- private int mActivatedPosition = -1;
+ private int mHighlightedPosition = -1;
+ private Drawable mHighlightedDrawable;
private static class PreferenceLayout implements Comparable<PreferenceLayout> {
private int resId;
@@ -212,8 +215,18 @@
return this.getItem(position).getId();
}
- public void setActivated(int position) {
- mActivatedPosition = position;
+ /**
+ * @hide
+ */
+ public void setHighlighted(int position) {
+ mHighlightedPosition = position;
+ }
+
+ /**
+ * @hide
+ */
+ public void setHighlightedDrawable(Drawable drawable) {
+ mHighlightedDrawable = drawable;
}
public View getView(int position, View convertView, ViewGroup parent) {
@@ -227,7 +240,9 @@
convertView = null;
}
View result = preference.getView(convertView, parent);
- result.setActivated(position == mActivatedPosition);
+ if (position == mHighlightedPosition && mHighlightedDrawable != null) {
+ result.setBackgroundDrawable(mHighlightedDrawable);
+ }
return result;
}