@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setDivider(ActivityCompat.getDrawable(getActivity(), R.drawable.list_divider)); setDividerHeight(1); PreferenceGroupAdapter adapter = (PreferenceGroupAdapter) getListView().getAdapter(); for (int i = 0; i < getListView().getAdapter().getItemCount(); i++) {//lazy global setOnPreferenceClickListener Preference preference = adapter.getItem(i); if (preference != null && !InputHelper.isEmpty(preference.getKey())) { if (preference.getKey().equalsIgnoreCase("version")) { preference.setSummary(BuildConfig.VERSION_NAME); } else if (!(preference instanceof SwitchPreference) && !(preference instanceof ListPreference)) { preference.setOnPreferenceClickListener(this); } } } }
public static int getPreferencePosition(PreferenceGroupAdapter adapter, Class<? extends Preference> clazz) { for (int i = 0; i < adapter.getItemCount(); i++) { if (clazz.isInstance(adapter.getItem(i))) { return i; } } throw new Resources.NotFoundException(); }
@Before public void setUp() { controller = Robolectric.buildActivity(PreferencesActivity.class); activity = controller.withIntent(new Intent() .putExtra(PreferencesActivity.EXTRA_TITLE, R.string.display) .putExtra(PreferencesActivity.EXTRA_PREFERENCES, R.xml.preferences_display)) .create().start().resume().visible().get(); RecyclerView list = (RecyclerView) activity.findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(activity)); RecyclerView.Adapter adapter = list.getAdapter(); int position = ShadowSupportPreferenceManager .getPreferencePosition((PreferenceGroupAdapter) adapter, ThemePreference.class); RecyclerView.ViewHolder holder = CustomShadows.customShadowOf(adapter).getViewHolder(position); preferenceView = holder.itemView; }
@Before public void setUp() { controller = Robolectric.buildActivity(PreferencesActivity.class); activity = controller.withIntent(new Intent() .putExtra(PreferencesActivity.EXTRA_TITLE, R.string.display) .putExtra(PreferencesActivity.EXTRA_PREFERENCES, R.xml.preferences_display)) .create().postCreate(null).start().resume().visible().get(); RecyclerView list = (RecyclerView) activity.findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(activity)); RecyclerView.Adapter adapter = list.getAdapter(); int position = ShadowSupportPreferenceManager .getPreferencePosition((PreferenceGroupAdapter) adapter, FontPreference.class); RecyclerView.ViewHolder holder = CustomShadows.customShadowOf(adapter).getViewHolder(position); preferenceView = holder.itemView; }
@Before public void setUp() { controller = Robolectric.buildActivity(PreferencesActivity.class); activity = controller.withIntent(new Intent() .putExtra(PreferencesActivity.EXTRA_TITLE, R.string.display) .putExtra(PreferencesActivity.EXTRA_PREFERENCES, R.xml.preferences_display)) .create().postCreate(null).start().resume().visible().get(); RecyclerView list = (RecyclerView) activity.findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(activity)); RecyclerView.Adapter adapter = list.getAdapter(); int position = ShadowSupportPreferenceManager .getPreferencePosition((PreferenceGroupAdapter) adapter, FontSizePreference.class); RecyclerView.ViewHolder holder = CustomShadows.customShadowOf(adapter).getViewHolder(position); preferenceView = holder.itemView; }
@Override protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { return new PreferenceGroupAdapter(preferenceScreen) { @Override public void onBindViewHolder(PreferenceViewHolder holder, int position) { super.onBindViewHolder(holder, position); // Override Equalizer preference attachment to add a long click listener // and to change the detail text at runtime String fragment = getItem(position).getFragment(); if ("com.marverenic.music.ui.settings.EqualizerFragment".equals(fragment)) { ViewGroup itemView = (ViewGroup) holder.itemView; TextView title = (TextView) itemView.findViewById(android.R.id.title); TextView detail = (TextView) itemView.findViewById(android.R.id.summary); boolean hasSystemEq = Util.getSystemEqIntent(getContext()) != null; if (hasSystemEq && Util.hasEqualizer()) { // If we have Jockey's Equalizer and another Equalizer itemView.setOnLongClickListener(PreferenceFragment.this); detail.setText(R.string.equalizer_more_options_detail); detail.setVisibility(View.VISIBLE); } else if (hasSystemEq && !Util.hasEqualizer()) { // If we don't have any equalizers detail.setText(R.string.equalizer_unsupported); detail.setVisibility(View.VISIBLE); itemView.setEnabled(false); title.setEnabled(false); detail.setEnabled(false); } } } }; }
/** * Return a valid ListView position or -1 if none is found */ private int canUseListViewForHighLighting(String key) { if (getListView() == null) { return -1; } RecyclerView listView = getListView(); RecyclerView.Adapter adapter = listView.getAdapter(); if (adapter != null && adapter instanceof PreferenceGroupAdapter) { return findListPositionFromKey((PreferenceGroupAdapter) adapter, key); } return -1; }
private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) { final int count = adapter.getItemCount(); for (int n = 0; n < count; n++) { final Preference preference = adapter.getItem(n); final String preferenceKey = preference.getKey(); if (preferenceKey != null && preferenceKey.equals(key)) { return n; } } return -1; }
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); final PreferenceGroupAdapter adapter = (PreferenceGroupAdapter) parent.getAdapter(); final int adapterCount = adapter.getItemCount(); boolean wasLastPreferenceGroup = false; for (int i = 0, childCount = parent.getChildCount(); i < childCount; i++) { final View child = parent.getChildAt(i); final int adapterPosition = parent.getChildAdapterPosition(child); Preference preference = adapter.getItem(adapterPosition); boolean skipNextAboveDivider = false; if (adapterPosition == 0) { if (mDrawTop) { drawAbove(c, left, right, child); } skipNextAboveDivider = true; } if (preference instanceof PreferenceGroup && !(preference instanceof PreferenceScreen)) { if (mDrawBetweenCategories) { if (!skipNextAboveDivider) { drawAbove(c, left, right, child); skipNextAboveDivider = true; } } wasLastPreferenceGroup = true; } else { if (mDrawBetweenItems && !wasLastPreferenceGroup) { if (!skipNextAboveDivider) { drawAbove(c, left, right, child); skipNextAboveDivider = true; } } wasLastPreferenceGroup = false; } if (adapterPosition == adapterCount - 1) { if (mDrawBottom) { drawBottom(c, left, right, child); } } } }