我有以下文件和类:
xml/preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/characters_list">
<Preference
android:enabled="true"
android:key="characters_view"
android:layout="@layout/characters_view"
android:selectable="false"/>
</PreferenceCategory>
</PreferenceScreen>
布局/character_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.xxx.widget.ExpandableHeightGridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/characters_gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="380dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"/>
</LinearLayout>
CharacterAdapter类
class CharacterAdapter extends BaseAdapter {
@Override
public int getCount() {
return characters.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HashMap<String, String> character = characters.get(position);
View view;
if (convertView == null) {
view = inflater.inflate(R.layout.characters_view_item, null);
} else {
view = convertView;
}
// ...
return view;
}
}
在PreferenceActivity我试图用项目填充网格视图:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.settings);
Preference characters = findPreference("characters_view");
View view = characters.getView(null, null);
ExpandableHeightGridView grid = (ExpandableHeightGridView) view.findViewById(R.id.characters_gridview);
grid.setExpanded(true);
grid.setAdapter(new CharacterAdapter(getCharacters()));
}
但是什么也没发生,只看到空的首选项:(
有人知道如何在 Preference 项中呈现 GridView 吗?
先感谢您!