第一我们要在base/core/res/res/drawable/下定义item背景使用的selector属性xml文件
1.preference_background_bottom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/preference_background_bottom_pressed" />
<item android:state_focused="true" android:drawable="@drawable/preference_background_bottom_pressed" />
<item android:drawable="@drawable/preference_background_bottom_normal" />
</selector>
2.preference_background_center.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/preference_background_center_pressed" />
<item android:state_focused="true" android:drawable="@drawable/preference_background_center_pressed" />
<item android:drawable="@drawable/preference_background_center_normal" />
</selector>
3.preference_background_single.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/preference_background_single_pressed" />
<item android:state_focused="true" android:drawable="@drawable/preference_background_single_pressed" />
<item android:drawable="@drawable/preference_background_single_normal" />
</selector>
4.preference_background_top.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/preference_background_top_pressed" />
<item android:state_focused="true" android:drawable="@drawable/preference_background_top_pressed" />
<item android:drawable="@drawable/preference_background_top_normal" />
</selector>
第二我们在base/core/res/res/values/styles.xml中定义一个style
<style name="Widget.PreferenceListView" parent="Widget.AbsListView">
<item name="android:listSelector">@android:color/transparent</item>
<item name="android:divider">@android:drawable/preference_divider</item>
<item name="android:footerDividersEnabled">false</item>
</style>
第三我们修改base/core/res/res/layout/preference_list_fragment.xml中ListView使用刚才定义的style
<ListView android:id="@android:id/list"
style="@style/Widget.PreferenceListView"
android:layout_width="match_parent"
......
第四修改base/core/java/android/preference/PreferenceGroupAdapter.java类
1.定义两个变量
private List<Integer> mPreferenceCategoryIndex; //记录所有item的index
private int mCurrentIndex; //记录当前item的index
2.在构造函数中对mPreferenceCategoryIndex进行初始化
mPreferenceCategoryIndex = new ArrayList<Integer>();
3.在flattenPreferenceGroup函数中更新mPreferenceCategoryIndex的值,修改如下
private void flattenPreferenceGroup(List<Preference> preferences, PreferenceGroup group) {
// TODO: shouldn't always?
group.sortPreferences();
final int groupSize = group.getPreferenceCount();
for (int i = 0; i < groupSize; i++, mCurrentIndex++) {
final Preference preference = group.getPreference(i);
if (preference instanceof PreferenceCategory) {
mPreferenceCategoryIndex.add(mCurrentIndex + 1);
}
......
4.添加getCurrentCategoryOffset函数,用来获取当前item位置
private int[] mCCOCache = new int[2];
private int[] getCurrentCategoryOffset(int position) {
List<Integer> list = mPreferenceCategoryIndex;
int end = getCount();
int size = list.size();
int offset = 0;
mCCOCache[0] = 0;
mCCOCache[1] = end;
for (int i = size - 1; i >= 0; i--) {
int t = list.get(i);
if (position >= t) {
offset = t;
mCCOCache[0] = offset;
if (i < size - 1) {
mCCOCache[1] = list.get(i + 1) - 1;
} else {
mCCOCache[1] = end;
}
break;
} else {
mCCOCache[1] = t - 1;
}
}
return mCCOCache;
}
5.修改getView方法,处理item的背景,修改原生的return preference.getView(convertView, parent);为以下:
......
View v = preference.getView(convertView, parent);
if (!(preference instanceof PreferenceCategory)) {
int[] offset = getCurrentCategoryOffset(position);
if (offset[1] - offset[0] == 1) {
v.setBackgroundResource(com.android.internal.R.drawable.preference_background_single);
} else if (position == offset[0]) {
v.setBackgroundResource(com.android.internal.R.drawable.preference_background_top);
} else if (offset[1] - position == 1) {
v.setBackgroundResource(com.android.internal.R.drawable.preference_background_bottom);
} else {
v.setBackgroundResource(com.android.internal.R.drawable.preference_background_center);
}
}
return v;
到此大功告成,当然你要把selector文件所需要的图片文件准备好。我用到的背景图片如下:
好了,下次有时间再更新其他学习的心得。