How to manage dividers in a PreferenceFragment?

后端 未结 10 1022
慢半拍i
慢半拍i 2020-12-01 12:29

I started dealing with preferences in a PreferenceFragment. Here\'s what I have:

\"my_preferences\"

10条回答
  •  一整个雨季
    2020-12-01 12:52

    Had totally forgot about this question, will post an answer now to help others. I solved by moving my code in the onResume() method of the activity that hosts my PreferenceFragment. I think there are several other points at which you can recall a non-null ListView using findViewById(android.R.id.list).

    public boolean mListStyled;
    
    @Override
    public void onResume() {
        super.onResume();
        if (!mListStyled) {
            View rootView = getView();
            if (rootView != null) {
                ListView list = (ListView) rootView.findViewById(android.R.id.list);
                list.setPadding(0, 0, 0, 0);
                list.setDivider(null);
                //any other styling call
                mListStyled = true;
            }
        }
    }
    

    You can probably get rid of the rootView check, but at the same time you might even want to check for list != null. I didn't face any NPE this way anyway.

    So, setDivider(null) takes off the item-item dividers. I managed to add section dividers covering the full width of the screen by:

    • Removing padding from list;
    • Adding a custom preference in my XML:

    n

     
    

提交回复
热议问题