android - 焦点在 ListView 中无法正常工作

标签 android android-listview

我正在开发一款无障碍应用。为此,重要的是界面的所有元素都可以正确获得焦点,以便它们可以被 Talckback 读取。 我构建了一个 PreferenceActivity,里面有一个 ListView,就像这个问题 Preference List only shows first element

它在触摸模式下工作完美,但是当我尝试访问激活了 Talckback 的 ListView 时(这就像我尝试聚焦 ListView),它不像我预期的那样工作。我希望列表的子项获得焦点,而不是整个列表获得焦点。

enter image description here enter image description here


我还有一个问题,我的 PreferenceActivity 中的两个 ListViews 有滚动条,而且滚动条无法正常工作。我可以使 ListView 不可滚动吗?

谢谢

代码:

ListPreferences.java(自定义 ListView )

import com.battleship.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ListPreferences extends Preference implements
        OnCheckedChangeListener, OnHierarchyChangeListener {

    private ListView listView;
    private View thisView;
    private int listHeight = 0;

    public ListPreferences(Context context) {
        super(context);
    }

    public ListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onClick() {

        super.onClick();
        Toast t = Toast.makeText(getContext(), "HOLA!", 3);
        t.show();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        this.setLayoutResource(R.layout.listview_preference_layout);
        thisView = super.onCreateView(parent);
        listView = (ListView) thisView.findViewById(android.R.id.list);
        listView.setOnHierarchyChangeListener(this);

        String[] contentString = new String[3];
        if (getKey().equals("theme")) {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_theme_default)),
                    (getContext().getString(R.string.settings_theme_black)),
                    (getContext().getString(R.string.settings_theme_white)) };
        } else {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_font_big)),
                    (getContext().getString(R.string.settings_font_medium)),
                    (getContext().getString(R.string.settings_font_little)) };
        }

        ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, contentString);
            listView.setAdapter(array);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setFocusable(false);
            listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            return thisView;    

    }





    private void updatePreference(int intRadio) {
        SharedPreferences.Editor editor = getEditor();
        editor.putInt(getKey(), intRadio);
        editor.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        updatePreference(checkedId);
        notifyChanged();
    }

    @Override
    public void onChildViewAdded(View parent, View child) {
        int childHeight = child.getMeasuredHeight();
        if(childHeight > 0)
        {
            listHeight = listView.getAdapter().getCount() * childHeight;
            thisView.setMinimumHeight(listHeight);
            Log.i("LISTA","onChildViewAdded, done: "+listHeight+" "+childHeight);
        }
    }

    public void onChildViewRemoved(View parent, View child) {
    }

}

preference.xml(PreferenceActivity的xml)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="player_settings"
        android:title="@string/settings_player_config" >
        <EditTextPreference
            android:defaultValue="@string/settings_player_default_name"
            android:dialogMessage="@string/settings_player_summary"
            android:dialogTitle="@string/settings_playersname"
            android:key="player_name"
            android:summary="@string/settings_player_summary"
            android:title="@string/settings_playersname" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="volume"
        android:title="@string/settings_volume" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="volume"
            android:title="@string/settings_volume" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="shine"
        android:title="@string/settings_shine" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="shine"
            android:title="@string/settings_shine" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="themeTitle"
        android:title="@string/settings_group_themes" >
        <com.battleship.preferences.ListPreferences android:key="theme" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="fontsTitle"
        android:title="@string/settings_group_font_size" >
        <com.battleship.preferences.ListPreferences android:key="font" />
    </PreferenceCategory>

</PreferenceScreen>

SettingsActivity.java (PreferenceActivity)

package com.battleship;
import com.battleship.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class SettingsActivity extends PreferenceActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
    @Override
    public View getCurrentFocus() {
        Log.d("FOCO", "" + super.getCurrentFocus());
        return super.getCurrentFocus();
    }
    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        Log.d("TRACKBALL", "" + event);
        return super.onTrackballEvent(event);
    }

}

最佳答案

import com.battleship.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ListPreferences extends PreferenceCategory implements OnPreferenceClickListener, OnPreferenceChangeListener{

private final String TAG = getClass().getSimpleName();
private final Context mContext;
private PreferenceScreen parentScreen;
private View thisView;

public FilterPreferenceCategory(Context context) {
    super(context);
    mContext = context;
}

public FilterPreferenceCategory(Context context, PreferenceScreen preferenceScreen) {
    super(context);
    mContext = context;
    parentScreen = preferenceScreen;
}

@Override
protected View onCreateView(ViewGroup parent) {
    this.setLayoutResource(R.layout.rb_group_layout);
    Log.i(TAG, "onCreateView");
    if(thisView == null)
    {
        thisView = super.onCreateView(parent);
        RadioGroup radioGroup = (RadioGroup) thisView.findViewById(R.id.radioGroup);
        final String[] contentString = getKey().equals("theme") ? mContext.getResources().getStringArray(R.array.theme_stuff) : mContext.getResources().getStringArray(R.array.font_stuff);
        int i;
        for(i = 0; i < contentString.length; i++)
        {
                RadioButton radioButton = new RadioButton(mContext);
                radioButton.layout(0, 0, 0, 0);
                radioButton.setFocusable(true);
                radioButton.setFocusableInTouchMode(true);
                radioButton.setOnClickListener(this);
                radioButton.setText(contentString[i]);
                radioGroup.addView(radioButton);
        }
    }
    return thisView;
}

public boolean onPreferenceChange(Preference preference, Object newValue) {
    Log.i(TAG, "onPreferenceChange");
    return false;
}

public boolean onPreferenceClick(Preference preference) {
    Log.i(TAG,"onPreferenceClick");
    return false;
}

@Override
protected void onAttachedToActivity() {
    Log.i(TAG,"onAttachedToActivity");
    super.onAttachedToActivity();
}

public void onClick(View v) {
    v.requestFocus();
    v.requestFocusFromTouch();
}

 }

res/layout/rb_group_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#000000"
    android:weightSum="2"
     >
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        android:orientation="vertical"
        android:gravity="center"
        android:paddingRight="10dp" >
        <TextView
            android:id="@android:id/summary"
            style="@android:style/TextAppearance.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingBottom="5dp"
            />
        <TextView 
            android:id="@android:id/summary"
            style="@android:style/TextAppearance.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />
    </LinearLayout>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
         >
    </RadioGroup>
</LinearLayout>

res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="theme_stuff">
        <item>@string/settings_theme_default</item>
        <item>@string/settings_theme_black</item>
        <item>@string/settings_theme_white</item>
    </string-array>
    <string-array name="font_stuff">
        <item>@string/settings_font_big</item>
        <item>@string/settings_font_medium</item>
        <item>@string/settings_font_little</item>
    </string-array>
</resources>

关于android - 焦点在 ListView 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904890/

相关文章:

android - 在微信/微信上关注某人的链接

android - 如何使用 FileInputStream 设置文件名?

java - Android,关于服务绑定(bind)的快速问题

firebase - 如何在 Flutter 中手动分配 Listview.builder 的第一个元素?

android - 带有单选按钮的自定义拖动排序 ListView

android - Android 中的子菜单

android - SurfaceView onDraw,有时在绘制之前不会刷新自己......为什么?

android - 如何获取 ListView 中所有 EditText 的值

android - 在 Android 中将项目添加到顶部时保持列表的滚动位置

java - 根据Main Activity类的条件在Adapter类的listview中插入图片