其他分享
首页 > 其他分享> > android – 从PreferenceFragment访问列表视图

android – 从PreferenceFragment访问列表视图

作者:互联网

我需要在GoogleTV中使用D-PAD处理PreferenceFragment导航.为此,我需要访问PreferenceFragment的ListView(第一个和最后一个元素).

在下面的代码中,我能够访问列表视图的子项,但它是空的.然而,如果我调试我可以看到12个孩子被创建,但他们是null因此计数返回为’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 …注意它是Pref屏幕中的Pref屏幕:

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

我是否可以访问至少第一个和最后一个元素,即“温度”和“条款和条件”,如下图所示:

解决方法:

以下是访问listview和使用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;
    }

标签:android,android-fragments,android-preferences
来源: https://codeday.me/bug/20190826/1732185.html