android – ListView项目未显示为已激活

android – ListView项目未显示为已激活,第1张

概述所以,一切都在智能手机上运行良好.但是,在平板电脑上,我的ListView应显示为左侧导航元素,当前激活项目在详细信息片段中显示为扩展.这也很有效.但是,ListFragment的激活项目不会显示为已选中.我将 android:choiceMode设置为singleChoice.我有正确的选择器激活,按下一个项目时会出现背景drawable.选择器有适合android的项:state_activ 所以,一切都在智能手机上运行良好.但是,在平板电脑上,我的ListVIEw应显示为左侧导航元素,当前激活的项目在详细信息片段中显示为扩展.这也很有效.但是,ListFragment的激活项目不会显示为已选中.我将 android:choiceMode设置为singleChoice.我有正确的选择器激活,按下一个项目时会出现背景drawable.选择器有适合androID的项:state_activated =“true”.我在styles.xml文件中指定了androID:activatedBackgroundindicator.当我在onListItemClick中记录ListVIEw.getCheckedItemCount()的输出时,它会让我回到1,正如我所料.

很久以前,几周前,在开始使用自定义列表视图项布局之前,我开始使用ArrayAdapter完全按照预期工作,然后开始连接我的数据源.然后在某个地方,我做了一些让激活项目显示不正确的东西,但我不确定它是什么.

selectable_background.xml

<selector xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:exitFadeDuration="@androID:integer/config_mediumAnimTime" >    <item androID:state_pressed="false" androID:state_focused="true" androID:drawable="@drawable/List_focused" />    <item androID:state_pressed="true" androID:drawable="@drawable/pressed_background" />    <!-- selected -->    <item androID:state_activated="true" androID:drawable="@drawable/pressed_background" />    <item androID:state_checked="true" androID:drawable="@drawable/pressed_background" /> <!-- paranoia -->    <item androID:state_selected="true" androID:drawable="@drawable/pressed_background" /> <!-- paranoia -->    <!-- default -->    <item androID:drawable="@androID:color/transparent" /></selector>

fragment_List.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:orIEntation="vertical"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent" >    <ListVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:ID="@ID/androID:List"        androID:layout_wIDth="match_parent"        androID:layout_height="0dip"        androID:layout_weight="1"        androID:choiceMode="singleChoice"        androID:ListSelector="@drawable/selectable_background" >    </ListVIEw>    <TextVIEw androID:ID="@ID/androID:empty"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:paddingtop="16dp"        androID:paddingBottom="16dp"        androID:paddingleft="12dp"        androID:paddingRight="12dp"        androID:text="@string/empty_List" /></linearLayout>

List_item.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="horizontal"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content" >    <ImageVIEw androID:ID="@+ID/item_image"        androID:layout_wIDth="63dp"        androID:layout_height="63dp"        androID:contentDescription="@string/List_image"        androID:src="@drawable/ic_thumbnail_placeholder"        androID:background="#f0f0f0" />    <TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:ID="@+ID/item_name"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:textAppearance="?androID:attr/textAppearanceListItemSmall"        androID:gravity="center_vertical"        androID:paddingleft="?androID:attr/ListPreferredItempaddingleft"        androID:paddingRight="?androID:attr/ListPreferredItempaddingRight"        androID:minHeight="?androID:attr/ListPreferredItemHeightSmall" /></linearLayout>

MyListFragment.java

@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    this.getActivity().getLoaderManager().initLoader(ALL_ITEMS_LOADER,null,this);    listadapter = new SimpleCursorAdapter(            this.getActivity(),R.layout.List_item,new String[] { Item.name },new int[] { R.ID.item_name },CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER    );    this.setlistadapter(listadapter);}@OverrIDepublic Loader<Cursor> onCreateLoader(int i,Bundle bundle) {    Uri queryUri;    switch (i) {        case ALL_ITEMS_LOADER:            queryUri = Item.CONTENT_URI;            return new CursorLoader(this.getActivity(),queryUri,null);        default:            throw new IllegalArgumentException("UnkNown loader ID: " + i);    }}@OverrIDepublic voID onLoadFinished(Loader<Cursor> cursorLoader,Cursor cursor) {    if (listadapter == null) {        throw new IllegalStateException("List adapter is missing!");    } else {        Log.d(TAG,"Load finished. Cursor swapped.");        listadapter.swapCursor(cursor);    }}

更新:

我也尝试用之前使用过的ArrayAdapter替换SimpleCursorAdapter,但没有骰子:

listadapter = new ArrayAdapter<String>(        this.getActivity(),R.ID.item_name,new String[] {"1","2","3"});

更新2:

啊啊!线索!

listadapter = new ArrayAdapter<String>(        this.getActivity(),androID.R.layout.simple_List_item_activated_1,androID.R.ID.text1,"3"});

这有效.但由于我实际上需要使用自己的布局,因此它无法解决我自己的问题. R.layout.List_item和androID.R.layout.simple_List_item_activated_1之间有什么不同?

解决方法

Once upon a time,a few weeks ago,I even had this all working exactly as intended … before I started using a custom List vIEw item layout.

我的猜测是你之前使用的是simple_List_item_activated_x布局之一,它在其背景可绘制定义中定义了激活的drawable.框架所做的一件事就是从被激活的逻辑中分离出被按下的逻辑(前者是列表选择器的一部分,而后者是列表项),但它不需要这样做,你可以将它们全部放在列表项背景中并完全禁用选择器.

我建议将自定义drawable作为每个列表项的背景而不是列表选择器; activate是应用于每个单独项目视图的状态.然后你可以设置androID:ListSelector =“@ null”,因为在某些状态下你有一个透明的背景,你不希望显示默认选择器.

总结

以上是内存溢出为你收集整理的android – ListView项目未显示为已激活全部内容,希望文章能够帮你解决android – ListView项目未显示为已激活所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1128676.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存