문제

I need to handle PreferenceFragment navigation using D-PAD in GoogleTV. To do that, I need to access the ListView(first and last elements) of the PreferenceFragment.

In the code below I was able to access the children of the listview but it was empty. Yet if I debug I can see 12 children being created but they are null therefore count return is '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;
}

Below is the xml for the PreferenceScreen...notice it is Pref screens within Pref screens:

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

Can I get access to at least the first and last element which is "Temperature" and "Terms & Conditions" as seen in image below:

enter image description here

도움이 되었습니까?

해결책

Here is the solution to accessing listview and controlling with 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;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top