T. Folsomからの回答を詳しく説明するために、ここに私の実装を示します。
res/layout/preference_header_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:baselineAligned="false"
android:gravity="center_vertical"
android:minHeight="48dp"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_width="@dimen/header_icon_width"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<TextView
android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
res/values/dimens.xml
<resources>
<dimen name="header_icon_width">28dp</dimen>
</resources>
あなたの PreferenceActivity クラスで:
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
/*
* the headers must be restored before the super call in order
* to be ready for the call to setListAdapter()
*/
if (savedInstanceState.containsKey("headers")) {
setHeaders((ArrayList<Header>)savedInstanceState.getSerializable("headers"));
}
}
// as suggest by https://stackoverflow.com/questions/15551673/android-headers-categories-in-preferenceactivity-with-preferencefragment
if(onIsMultiPane()) getIntent().putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, PreferencesFragment.class.getName());
super.onCreate(savedInstanceState);
...
}
@Override
protected void onResume() {
super.onResume();
// https://stackoverflow.com/questions/15551673/android-headers-categories-in-preferenceactivity-with-preferencefragment
// Select the displayed fragment in the headers (when using a tablet) :
// This should be done by Android, it is a bug fix
if(getHeaders() != null) {
final String displayedFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
if (displayedFragment != null) {
for (final Header header : getHeaders()) {
if (displayedFragment.equals(header.fragment)) {
switchToHeader(header);
break;
}
}
}
}
...
}
/**
* Populate the activity with the top-level headers.
*/
@Override
public void onBuildHeaders(List<Header> target) {
// we have to save the headers as the API call getHeaders() is hidden.
setHeaders(target);
loadHeadersFromResource(R.xml.settings_headers, target);
}
private List<Header> headers;
private void setHeaders(List<Header> headers) {
this.headers = headers;
}
private List<Header> getHeaders() {
return headers;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("headers", (ArrayList<PreferenceActivity.Header>)headers);
super.onSaveInstanceState(outState);
}
@Override
public void setListAdapter(ListAdapter adapter) {
if (adapter == null) {
super.setListAdapter(null);
} else {
super.setListAdapter(new HeaderAdapter(this, getHeaders()));
}
}
private static class HeaderAdapter extends ArrayAdapter<Header> {
static final int HEADER_TYPE_CATEGORY = 0;
static final int HEADER_TYPE_NORMAL = 1;
private static final int HEADER_TYPE_COUNT = HEADER_TYPE_NORMAL + 1;
private static class HeaderViewHolder {
ImageView icon;
TextView title;
TextView summary;
}
private LayoutInflater mInflater;
static int getHeaderType(Header header) {
if (header.fragment == null && header.intent == null) {
return HEADER_TYPE_CATEGORY;
} else {
return HEADER_TYPE_NORMAL;
}
}
@Override
public int getItemViewType(int position) {
Header header = getItem(position);
return getHeaderType(header);
}
@Override
public boolean areAllItemsEnabled() {
return false; // because of categories
}
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) != HEADER_TYPE_CATEGORY;
}
@Override
public int getViewTypeCount() {
return HEADER_TYPE_COUNT;
}
@Override
public boolean hasStableIds() {
return true;
}
public HeaderAdapter(Context context, List<Header> objects) {
super(context, 0, objects);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
Header header = getItem(position);
int headerType = getHeaderType(header);
View view = null;
if (convertView == null) {
holder = new HeaderViewHolder();
switch (headerType) {
case HEADER_TYPE_CATEGORY:
view = new TextView(getContext(), null,
android.R.attr.listSeparatorTextViewStyle);
holder.title = (TextView) view;
break;
case HEADER_TYPE_NORMAL:
view = mInflater.inflate(R.layout.preference_header_item,
parent, false);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.title = (TextView) view
.findViewById(android.R.id.title);
holder.summary = (TextView) view
.findViewById(android.R.id.summary);
break;
}
view.setTag(holder);
} else {
view = convertView;
holder = (HeaderViewHolder) view.getTag();
}
// All view fields must be updated every time, because the view may
// be recycled
switch (headerType) {
case HEADER_TYPE_CATEGORY:
holder.title.setText(header.getTitle(getContext()
.getResources()));
break;
case HEADER_TYPE_NORMAL:
holder.icon.setImageResource(header.iconRes);
holder.title.setText(header.getTitle(getContext()
.getResources()));
CharSequence summary = header.getSummary(getContext()
.getResources());
if (!TextUtils.isEmpty(summary)) {
holder.summary.setVisibility(View.VISIBLE);
holder.summary.setText(summary);
} else {
holder.summary.setVisibility(View.GONE);
}
break;
}
return view;
}
}
このコードをすべて配置したら、ヘッダーを作成するのは簡単です。
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<header android:title="atitle" />
</preference-headers>
これが誰かに役立つことを願っています。適切に動作するようになるまでに時間がかかったことがわかっています。