列表视图中的第一个项目不会获得焦点第一个、视图、焦点、项目

2023-09-06 22:49:19 作者:男人乃生外之物;

我有一个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);
                    }
                });
            }
        }
    });