分众没有发挥预期的ListView [英] Focus not working as expected in ListView

查看:207
本文介绍了分众没有发挥预期的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个可访问的应用程序。为了这个目的,重要的是该接口的所有元件可以正确地获取聚焦,以便它们可以由Talckback读取。 我建立一个 preferenceActivity 在这个问题的ListView 里面,像href="http://stackoverflow.com/questions/9604242/$p$pference-list-only-shows-first-element/9885268#comment12620104_9885268">$p$pference列表只显示第一个元素

它的工作原理完美的触控模式,但是当我尝试访问的的ListView 与Talckback激活(这就像我试图让专注于的ListView ),它不工作,就像我的预期。我想列表的孩子得到的焦点,而不是整个列表获得焦点。


我有另外一个问题,这两个列表视图在我的 preferenceActivity 有滚动,和滚动不工作的权利。我可以使的ListView 没有滚动?

感谢

code:

列表preferences.java(自定义的ListView)

 进口com.battleship.R;

进口android.content.Context;
进口android.content.Shared preferences;
。进口的Andr​​oid preference preference;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.ViewGroup.OnHierarchyChangeListener;
进口android.widget.ArrayAdapter;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.RadioGroup.OnCheckedChangeListener;
进口android.widget.Toast;

公共类目录preferences扩展preference工具
        OnCheckedChangeListener,OnHierarchyChangeListener {

    私人的ListView ListView的;
    私人查看thisView;
    私人诠释listHeight = 0;

    公开名单preferences(上下文的背景下){
        超(上下文);
    }

    公开名单preferences(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公开名单preferences(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    @覆盖
    保护无效的onClick(){

        super.onClick();
        吐司T = Toast.makeText(的getContext(),HOLA!,3);
        t.show();
    }

    @覆盖
    受保护的视图onCreateView(ViewGroup中父){

        this.setLayoutResource(R.layout.listview_ preference_layout);
        thisView = super.onCreateView(父);
        ListView控件=(ListView控件)thisView.findViewById(android.R.id.list);
        listView.setOnHierarchyChangeListener(本);

        的String [] contentString =新的String [3];
        如果(getKey()。等于(主题)){
            contentString =新的String [] {
                    (的getContext()的getString(R.string.settings_theme_default)),
                    (的getContext()的getString(R.string.settings_theme_black)),
                    (的getContext()的getString(R.string.settings_theme_white)。)};
        } 其他 {
            contentString =新的String [] {
                    (的getContext()的getString(R.string.settings_font_big)),
                    (的getContext()的getString(R.string.settings_font_medium)),
                    (的getContext()的getString(R.string.settings_font_little)。)};
        }

        ArrayAdapter<字符串>阵列=新的ArrayAdapter<字符串>(的getContext()
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1,contentString);
            listView.setAdapter(阵列);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setFocusable(假);
            listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            返回thisView;

    }





    私人无效更新preference(INT intRadio){
        共享preferences.Editor编辑器= getEditor();
        editor.putInt(getKey(),intRadio);
        editor.commit();
    }

    @覆盖
    公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
        更新preference(checkedId);
        notifyChanged();
    }

    @覆盖
    公共无效onChildViewAdded(查看父,子视图){
        INT childHeight = child.getMeasuredHeight();
        如果(childHeight大于0)
        {
            listHeight = listView.getAdapter()getCount将()* childHeight。
            thisView.setMinimumHeight(listHeight);
            Log.i(LISTA,onChildViewAdded,做到:+ listHeight ++ childHeight);
        }
    }

    公共无效onChildViewRemoved(查看父,子视图){
    }

}
 

preference.xml(的preferenceActivity XML)

 < XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    < preferenceCategory
        机器人:关键=player_settings
        机器人:标题=@字符串/ settings_player_config>
        < EditText上preference
            安卓:设置defaultValue =@字符串/ settings_player_default_name
            机器人:dialogMessage =@字符串/ settings_player_summary
            机器人:dialogTitle =@字符串/ settings_playersname
            机器人:关键=player_name
            机器人:总结=@字符串/ settings_player_summary
            机器人:标题=@字符串/ settings_playersname/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:关键=量
        机器人:标题=@字符串/ settings_volume>
        < com.battleship preferences.SeekBar preferences
            安卓:设置defaultValue =50
            机器人:关键=量
            机器人:标题=@字符串/ settings_volume/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:关键=闪耀
        机器人:标题=@字符串/ settings_shine>
        < com.battleship preferences.SeekBar preferences
            安卓:设置defaultValue =50
            机器人:关键=闪耀
            机器人:标题=@字符串/ settings_shine/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:关键=themeTitle
        机器人:标题=@字符串/ settings_group_themes>
        < com.battleship preferences.List preferences安卓键=主题为/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:关键=fontsTitle
        机器人:标题=@字符串/ settings_group_font_size>
        < com.battleship preferences.List preferences安卓键=字体/>
    < / preferenceCategory>

< / preferenceScreen>
 

SettingsActivity.java(preferenceActivity)

 包com.battleship;
进口com.battleship.R;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceActivity;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;

公共类SettingsActivity扩展preferenceActivity {
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preferences。);
    }
    @覆盖
    公共查看getCurrentFocus(){
        Log.d(FOCO,+ super.getCurrentFocus());
        返回super.getCurrentFocus();
    }
    @覆盖
    公共布尔onTrackballEvent(MotionEvent事件){
        Log.d(轨迹球,+活动);
        返回super.onTrackballEvent(事件);
    }

}
 

解决方案

 进口com.battleship.R;

进口android.content.Context;
进口android.content.Shared preferences;
。进口的Andr​​oid preference preference;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.ViewGroup.OnHierarchyChangeListener;
进口android.widget.ArrayAdapter;
进口android.widget.LinearLayout;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.RadioGroup.OnCheckedChangeListener;
进口android.widget.Toast;

公共类目录preferences扩展preferenceCategory实现在preferenceClickListener,在preferenceChangeListener {

私人最终字符串变量=的getClass()getSimpleName()。
私人最终语境mContext;
私人preferenceScreen parentScreen;
私人查看thisView;

公共过滤器preferenceCategory(上下文的背景下){
    超(上下文);
    mContext =背景;
}

公共过滤器preferenceCategory(上下文的背景下,preferenceScreen preferenceScreen){
    超(上下文);
    mContext =背景;
    parentScreen = preferenceScreen;
}

@覆盖
受保护的视图onCreateView(ViewGroup中父){
    this.setLayoutResource(R.layout.rb_group_layout);
    Log.i(TAG,onCreateView);
    如果(thisView == NULL)
    {
        thisView = super.onCreateView(父);
        RadioGroup中RadioGroup中=(RadioGroup中)thisView.findViewById(R.id.radioGroup);
        最终的String [] contentString = getKey()。等于(主题)? 。mContext.getResources()getStringArray(R.array.theme_stuff):mContext.getResources()getStringArray(R.array.font_stuff);
        INT I;
        对于(i = 0; I< contentString.length;我++)
        {
                单选按钮单选按钮=新的单选按钮(mContext);
                radioButton.layout(0,0,0,0);
                radioButton.setFocusable(真正的);
                radioButton.setFocusableInTouchMode(真正的);
                radioButton.setOnClickListener(本);
                radioButton.setText(contentString [I]);
                radioGroup.addView(单选按钮);
        }
    }
    返回thisView;
}

在preferenceChange公共布尔(preference preference,对象为newValue){
    Log.i(TAG,在preferenceChange);
    返回false;
}

公共布尔在preferenceClick(preference preference){
    Log.i(TAG,在preferenceClick);
    返回false;
}

@覆盖
保护无效onAttachedToActivity(){
    Log.i(TAG,onAttachedToActivity);
    super.onAttachedToActivity();
}

公共无效的onClick(视图v){
    v.requestFocus();
    v.requestFocusFromTouch();
}

 }
 

RES /布局/ rb_group_layout.xml:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:填充=10dp
    机器人:后台=#000000
    机器人:weightSum =2
     >
    <的LinearLayout
        机器人:layout_weight =1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_gravity =顶
        机器人:方向=垂直
        机器人:重力=中心
        机器人:paddingRight =10dp>
        <的TextView
            机器人:ID =@机器人:ID /摘要
            风格=@安卓风格/ TextAppearance.Medium
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =center_vertical
            机器人:paddingBottom会=5DP
            />
        <的TextView
            机器人:ID =@机器人:ID /摘要
            风格=@安卓风格/ TextAppearance.Small
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =center_vertical/>
    < / LinearLayout中>
    < RadioGroup中
        机器人:ID =@ + ID / RadioGroup中
        机器人:layout_weight =1
        机器人:layout_width =match_parent
        机器人:layout_height =FILL_PARENT
         >
    < / RadioGroup中>
< / LinearLayout中>
 

RES /价值/ arrays.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <字符串数组名=theme_stuff>
        <项目> @字符串/ settings_theme_default< /项目>
        <项目> @字符串/ settings_theme_black< /项目>
        <项目> @字符串/ settings_theme_white< /项目>
    < /字符串数组>
    <字符串数组名=font_stuff>
        <项目> @字符串/ settings_font_big< /项目>
        <项目> @字符串/ settings_font_medium< /项目>
        <项目> @字符串/ settings_font_little< /项目>
    < /字符串数组>
< /资源>
 

I am developing an accessible app. For this purpose it is important that all elements of the interface can obtain the focus correctly, so that they can be read by Talckback. I build a PreferenceActivity with a ListView inside, like in this question Preference List only shows first element

It works perfect in touch mode, but when I try to access to the ListView with the Talckback activated (It's like I try to get Focused the ListView), It doesn't work like I expected. I want the children of the list get the focus, not the entire list gets the focus.


I have an additional problem, the two ListViews inside my PreferenceActivity has scroll, and the scroll is not working right. Can I make the ListView no scrollable?

Thanks

Code:

ListPreferences.java (Custom 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 (xml of PreferenceActivity)

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

这篇关于分众没有发挥预期的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆