gpt4 book ai didi

android - 从 PreferenceFragment 访问 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:44 25 4
gpt4 key购买 nike

我需要在 GoogleTV 中使用方向键来处理 PreferenceFragment 导航。为此,我需要访问 PreferenceFragment 的 ListView(第一个和最后一个元素)。

在下面的代码中,我能够访问 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...注意它是 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>

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

enter image description here

最佳答案

这是访问 ListView 并使用方向键控制的解决方案:

@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 - 从 PreferenceFragment 访问 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com