开发者

How to set scroll position for long PreferenceScreen

开发者 https://www.devze.com 2023-03-01 10:38 出处:网络
Android app has some very long preference screens, which always open a开发者_C百科t the top of the preference menu.I have some idea where the user wants to be in the preference menu.How can I force th

Android app has some very long preference screens, which always open a开发者_C百科t the top of the preference menu. I have some idea where the user wants to be in the preference menu. How can I force the preference screen to open scrolled to a specific preference item?


I know this is an old one, so this answer is just for reference.

To auto-select a given screen, all you have to do is setPreferenceScreen() (this is for a pre-Honeycomb non-Fragment PreferenceActivity).

Once you're on the correct PreferenceScreen, you can indeed use getListView().smoothScrollToPosition(position) (but this is a Froyo+ method), or you can use getListView.setSelection(position).

But how to get the position?

First, watch out for the trap: PreferenceActivity.getListAdapter() does not return the actual ListAdapter, but a local instance variable which is disconcertingly not in sync with PreferenceActivity.getListView().getAdapter() (and usually null).

Second, trying to use Preference.getOrder() returns the order of the Preference object within its parent, which is what you want to use for the position only if you're not using PreferenceCategories since what you need is its order within the PreferenceScreen.

If you are using PreferenceCategories, you need to iterate over the items in the adapter (for (int i = 0; i < adapter.getCount(); i++)until you find the right one, and use its position.

Another corner of the Android SDK that is in dire need of some attention…


You can just use scrollToPreference :

https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat#scrollToPreference(androidx.preference.Preference)

Example:

scrollToPreference(preferenceKey)

or:

scrollToPreference(preference)


Add this function to your PreferenceFragment

public void scrollToItem(String preferenceName) {
        ListView listView = ButterKnife.findById(getView(),android.R.id.list);
        Preference preference = findPreference(preferenceName);
        if (preference != null && listView != null) {
            for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                Preference iPref = (Preference) listView.getAdapter().getItem(i);
                if (iPref == preference) {
                    listView.setSelection(i);
                    break;
                }
            }
        }
    }

Lets say you have settings.xml with this

<Preference
    android:icon="@drawable/ic_action_email"
    android:key="emailSupport"
    android:title="@string/email_support" />

You can call

scrollToItem("emailSupport");

Note: You may need to replace listView.setSelection(i) with listView.smoothScrollToPosition(i)


Since PreferenceActivity extends ListActivity, you can call getListView() to get the ListView containing your preferences, and then use listView.smoothScrollToPosition() to scroll to a specific row in the list. I haven't actually tried this before, but it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号