列表视图中的第一个项目不会获得焦点 [英] The first item in listview does not gets the focus

查看:186
本文介绍了列表视图中的第一个项目不会获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText和一个线性列表视图layout.Suppose有5个结果列表中当前总出10.当我从编辑文本到列表视图,第5项列表视图(向下滚动可见我想它总是最后一个)获得焦点,而不是第一个项目!我应该怎么解决这个问题?

I have an edittext and a listview in a linear layout.Suppose there are 5 results currently visible in the list out of total 10.When i scroll down from the edit text on to the listview, the 5th item in the listview(I suppose its always the last one) gets the focus, instead of the first item! How should i solve this?

推荐答案

1)建立一个处理程序,在您的活动现场。像这样的:

1) Create a Handler, as a field in your Activity. Like this:

private Handler myHandler = new Handler();

2)当您创建ListView中,添加一个OnFocusChangeListener,如下图所示。

2) When you create the ListView, add a OnFocusChangeListener, like shown below.

    myList.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                myHandler.postAtFrontOfQueue(new Runnable() {
                    public void run() {
                        myList.setSelection(0);
                    }
                });
            }
        }
    });

这篇关于列表视图中的第一个项目不会获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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