简体   繁体   English

启动活动Android时将焦点放在列表视图中的第一项

[英]Focus 1st item in a listview when starting an activity android

I am developing an app that when I press a button ,It starts a new activity (ContactList.java) that contains a listview called mContactList. 我正在开发一个应用程序,当我按下按钮时,它会启动一个新活动(ContactList.java),其中包含一个名为mContactList的列表视图。 When the activity starts I want the first item of the listview to be automatically focused. 活动开始时,我希望列表视图的第一项自动聚焦。

I tried something like that: 我尝试过这样的事情:

ContactList.java ContactList.java

mContactsList.requestFocus(mContactsList.getFirstVisiblePosition());

I have also created this two xml resources to ensure that when the view is focused a rectangle is displayed around the item of the listview: 我还创建了这两个xml资源,以确保在聚焦视图时在listview的项目周围显示一个矩形:

selected_rectangle.xml selected_rectangle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:color="@color/blue"
        android:width="5dp"/>
</shape>

selector_contact_list.xml selector_contact_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
            android:drawable="@drawable/selected_rectangle"/> </selector>

For some reason this code doesn´t work and I am not able to set focus on the first item of the list (since no rectangle appears around the first item of the list). 由于某种原因,此代码无法正常工作,我无法将注意力集中在列表的第一项上(因为列表的第一项周围没有矩形出现)。

Can someone give me clues what is happening? 有人可以告诉我发生了什么事吗?

Please note that setting focus and setting selection are two different things.. and I think you mean the second one, if so try this: 请注意,设置焦点和设置选择是两件事。.我想您的意思是第二个,如果是这样,请尝试以下操作:

listViewName.setSelection(positionOfItem);

if that didn't work, try requestFocusFromTouch() before calling setSelection() method. 如果不起作用,请在调用setSelection()方法之前尝试requestFocusFromTouch()

and if that didn't work, try this: 如果不起作用,请尝试以下操作:

listViewName.setItemChecked(positionOfItem, true);

Or this: 或这个:

listViewName.smoothScrollToPosition(positionOfItem);

For other solutions try the answers in this link: Android ListView setSelection() does not seem to work 对于其他解决方案,请尝试以下链接中的答案: Android ListView setSelection()似乎不起作用

采用

listview.setselection(thenumber/position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击第一个项目时,Android CarouselPicker - Android CarouselPicker when 1st item is click ANDROID:保存第一个项目后,将列表视图中的下一个项目获取到editText - ANDROID: Get next item from listview to editText after 1st item is saved 当我在列表视图中搜索项目时。 它总是打开第一个活动 - When i search items on my listview. it always open the 1st activity 没有运行第一个活动 - not running 1st activity 在 android 中将数据从第一个选项卡传递到第三个选项卡活动时获取 null object 引用 - Getting null object reference when passing data from 1st tab to 3rd tab activity in android 使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了 - visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android 将值从1st传递到android中的第三个活动 - pass value from 1st to third activity in android 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 内含Spinner的ListView:listview的第一项影响其最后一项 - ListView with Spinner Inside: 1st Item of the listview affects its last item JTable JComboBox默认在列表扩展时显示第一项 - JTable JComboBox is defaulting to display 1st item when list is expanded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM