如何使用GridView和自定义适配器作为首选项?

时间:2013-01-17 16:43:54

标签: android android-layout android-listview android-preferences android-gridview

我有以下文件和类:

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

刚刚做到了!

解决方案非常简单。需要在addOnGlobalLayoutListener上添加getListView().getViewTreeObserver()

PreferenceActivity 中的最后onCreate方法如下所示:

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()));
        }
    });
}