2

我有以下文件和类:

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 吗?

先感谢您!

4

1 回答 1

0

刚刚做到了!

解决方案非常简单。需要补充addOnGlobalLayoutListenergetListView().getViewTreeObserver()

PreferenceActivity中的finallyonCreate方法如下所示:

protected void onCreate(Bundle bundle) {
     super.onCreate(bundle);

     setContentView(R.layout.settings);

     final ListView list = getListView();
     list.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // make sure it is not called anymore
            list.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            View view = list.getChildAt(1);

            ExpandableHeightGridView grid = (ExpandableHeightGridView) view.findViewById(R.id.characters_gridview);
            grid.setExpanded(true);
            grid.setAdapter(new CharacterAdapter(getCharacters()));
        }
    });
}
于 2013-01-18T01:45:45.873 回答