2

我有以下文件和類:如何使用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())); 
} 

但沒有任何反應,只是看空首選項:(


有誰知道如何使GridView的內部偏好項目?

預先感謝您!

+0

我也試圖GridView控件添加到喜好的標題的ListView但當時不可能優先項目的工作,例如檢查/取消選中複選框不工作:( –

回答

0

剛剛做到了!

解決方案非常簡單。需要在getListView().getViewTreeObserver()上添加addOnGlobalLayoutListener

最後onCreate方法PreferenceActivity意志的樣子:

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())); 
     } 
    }); 
} 
相關問題