Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 35d7b89b authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

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
parent 8b53023a
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -337,6 +337,26 @@ public abstract class PreferenceFragment extends Fragment implements
        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;
+19 −4
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import java.util.ArrayList;
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 @@ public class PreferenceGroupAdapter extends BaseAdapter
        }
    };

    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 @@ public class PreferenceGroupAdapter extends BaseAdapter
        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 @@ public class PreferenceGroupAdapter extends BaseAdapter
            convertView = null;
        }
        View result = preference.getView(convertView, parent);
        result.setActivated(position == mActivatedPosition);
        if (position == mHighlightedPosition && mHighlightedDrawable != null) {
            result.setBackgroundDrawable(mHighlightedDrawable);
        }
        return result;
    }