2011-11-18 275 views
0

我喜歡從XML和preferenceFragment和左側導航創建的偏好。當我選擇「設置」時,我的pref片段出來了......我可以使用DPad上下移動,當底部沒有更多選擇時,光標停止(所需的行爲),但是當我向上移動列表時,光標繼續到左側的導航欄。我希望它停在頂端。以下是我的pref_screen xml。我的左手導航是創建代碼的一面。我不知道如何將DPad邏輯工作到此。如果在設置片段中,我還想限制DPad將光標移到左側導航菜單。DPad導航首選項屏幕Google TV

<?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><!-- 
    <PreferenceCategory 
      android:title="Weather Notification Bar"> 
      <CheckBoxPreference 
       android:key="pref_notification_enable" 
       android:title="Enable Notifications" 
       android:summary="Turn on/off status bar weather notifications" 
       android:defaultValue="true" 
       android:enabled="false"/> 
      <ListPreference 
       android:key="pref_notification_update_method" 
       android:dependency="pref_notification_enable" 
       android:title="Choose notification update method" 
       android:summary="Manual, Automatic, Interval" 
       android:entries="@array/pref_notification_update_method" 
       android:entryValues="@array/pref_notification_update_method_values" 
       android:dialogTitle="Update Method" /> 
      <ListPreference 
       android:key="pref_notification_interval" 
       android:title="Interval to update" 
       android:dependency="pref_notification_enable" 
       android:summary="Use this to set the update interval if interval method is selected" 
       android:entries="@array/pref_notification_interval" 
       android:entryValues="@array/pref_notification_interval_values" 
       android:dialogTitle="Update Interval" /> 
    </PreferenceCategory> 
    --> 
    <!--<PreferenceCategory 
      android:title="Background"> 
      <CheckBoxPreference 
       android:key="background_enabled" 
       android:title="Enable Interactive Background" 
       android:summary="Turn off to improve performance" 
       android:defaultValue="true" 
       android:enabled="true"/> 
    </PreferenceCategory> 
    --><PreferenceCategory 
      android:title="AccuWeather"> 
     <!-- <PreferenceScreen 
       android:title="@string/call_us" 
       android:summary="" 
       android:layout="@layout/settings_item"> 
      <intent android:action="android.intent.action.DIAL" 
        android:data="tel:8142358650" /> 
     </PreferenceScreen> 
     <PreferenceScreen 
       android:title="@string/email_us" 
       android:summary="[email protected]" 
       android:layout="@layout/settings_item"> 
       <intent android:action="com.accuweather.android.tablet.EMAIL_ACCUWX" 
       /> 
      <intent android:action="android.intent.action.SENDTO" 
       android:data="[email protected]"/> 
     </PreferenceScreen> --> 
     <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

+0

什麼是「設置」?你如何加載PreferencesFragment?如果沒有在這裏看到實際的UI和代碼,它很難提供幫助。 –

+0

抱歉,我無法重現此問題。我有一個左導航欄的活動,我正在加載偏好在它的右側。但是,當我點擊偏好設置屏幕時,它不會將焦點移至我的左側欄。我想這與如何加載佈局xml和preferencefragment有關。 –

回答

0

我必須想出一個辦法來描繪的位置和兒童辦理DPAD。這裏是我寫的代碼來處理這個問題:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState){ 
    View v = inflater.inflate(R.layout.settings_tile, null); 
    final ImageView closeBtn = (ImageView) v.findViewById(R.id.settingsCloseButton); 
    closeBtn.setFocusable(true); 
    closeBtn.setOnClickListener(this); 
    closeBtn.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       v.setOnKeyListener(new OnKeyListener() { 
        @Override 
        public boolean onKey(View v, int keyCode, KeyEvent event) { 
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { 
          return false; 
         } 
         if (event.getAction() == KeyEvent.ACTION_DOWN) { 
          return !(KeyEvent.KEYCODE_DPAD_DOWN == keyCode); 
         } 
         return false; 
        } 
       }); 
      } else { 
       v.setOnKeyListener(null); 
      } 

     } 
    }); 
    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: 
         if (lv.getSelectedItemPosition() == 1) { 
          closeBtn.requestFocus(); 
         } 
         return lv.getSelectedItemPosition() == 1; 
        case KeyEvent.KEYCODE_DPAD_RIGHT: 
         return true;  
       } 
       return false; 
      } 
      return false; 
     } 
    }); 
    consumeKeyEvent(v); 
    return v; 
} 

private void consumeKeyEvent(Object obj) { 
    if (obj instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup)obj).getChildCount(); i++) { 
      consumeKeyEvent(((ViewGroup)obj).getChildAt(i)); 
     } 
     return; 
    } 
    ((View)obj).setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      Log.i(DEBUG_TAG, "Event consumed, " + keyCode); 
      return false; 
     } 
    }); 
}