2011-12-02 53 views
3

我需要在GoogleTV中使用D-PAD處理PreferenceFragment導航。爲此,我需要訪問PreferenceFragment的ListView(第一個和最後一個元素)。從PreferenceFragment訪問列表視圖

在下面的代碼中,我能夠訪問listview的孩子,但它是空的。然而,如果我調試,我可以看到正在創建的12個孩子,但他們都爲空,因此指望回報「0」

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState){ 
    //Log.d(DEBUG_TAG, "onCreateView"); 
    View v = inflater.inflate(R.layout.settings_tile, null); 
    closeBtn = (ImageView) v.findViewById(R.id.settingsCloseButton); 
    ListView lv = (ListView) v.findViewById(android.R.id.list); 
    lv.getChildCount(); 
    return v; 
} 

下面是對PreferenceScreen的XML ...注意到它是縣屏幕中縣屏:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="main_pref"> 
     <PreferenceCategory 
      android:title="@string/units"> 
      <ListPreference 
       android:key="pref_unit_temp" 
       android:title="@string/temperature" 
       android:defaultValue="@string/default_metric" 
       android:entries="@array/pref_temp_units" 
       android:entryValues="@array/pref_temp_units_values" 
       android:dialogTitle="@string/units" 
       android:layout="@layout/settings_item" 
       /> 
    </PreferenceCategory> 
    <PreferenceCategory 
     android:title="@string/advanced"> 
     <ListPreference 
      android:key="pref_speed" 
      android:title="@string/speed" 
      android:entries="@array/pref_speed" 
      android:entryValues="@array/pref_speed_values" 
      android:defaultValue="@string/default_metric" 
      android:layout="@layout/settings_item"/> 
     <ListPreference 
      android:key="pref_measurement" 
      android:title="@string/measurement" 
      android:entries="@array/pref_measurement" 
      android:entryValues="@array/pref_measurement_values" 
      android:defaultValue="@string/default_metric" 
      android:layout="@layout/settings_item"/> 
     <ListPreference 
      android:key="pref_time" 
      android:title="@string/time_format" 
      android:entries="@array/pref_time" 
      android:entryValues="@array/pref_time_values" 
      android:defaultValue="@string/default_metric" 
      android:layout="@layout/settings_item"/> 
     <ListPreference 
      android:key="pref_date" 
      android:title="@string/date_format" 
      android:entries="@array/pref_date" 
      android:entryValues="@array/pref_date_values" 
      android:defaultValue="@string/default_metric" 
      android:layout="@layout/settings_item"/> 
    </PreferenceCategory> 
     <PreferenceScreen 
       android:title="@string/terms_conditions" 
       android:summary="" 
       android:layout="@layout/settings_item"> 
      <intent android:action="android.intent.action.VIEW" 
        android:data="http://www.accuweather.com/m/EULA.aspx" /> 
     </PreferenceScreen>   

    </PreferenceCategory> 
</PreferenceScreen> 

我可以接觸到至少這是「溫度」和「條款&條件」,如下圖所示的圖像的第一和最後一個元素:

enter image description here

回答

6

這裏是解決訪問列表視圖,並與DPAD控制:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState){ 
     //Log.d(DEBUG_TAG, "onCreateView"); 
     View v = inflater.inflate(R.layout.settings_tile, null); 
     closeBtn = (ImageView) v.findViewById(R.id.settingsCloseButton); 
     final ListView lv = (ListView) v.findViewById(android.R.id.list); 
     lv.setOnKeyListener(new OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       // TODO Auto-generated method stub 
       if (event.getAction() == KeyEvent.ACTION_DOWN) { 
        switch(keyCode) { 
         case KeyEvent.KEYCODE_DPAD_DOWN: 
          return !(lv.getSelectedItemPosition() < lv.getCount() - 1); 
         case KeyEvent.KEYCODE_DPAD_UP: 
          return lv.getSelectedItemPosition() == 1; 
         case KeyEvent.KEYCODE_DPAD_RIGHT: 
          return true;  
        } 
        return false; 
       } 
       return false; 
      } 
     }); 
     return v; 
    } 
+1

好知道,謝謝你讓別人知道你的解決方案。 – Knossos

+1

嗨,你在哪裏設置addPreferencesFromResource(R.xml.settings); – Naruto