android – 在“ListFragment”中突出显示所选项目?

android – 在“ListFragment”中突出显示所选项目?,第1张

概述我已经发布了相同的问题几次,但还没有解决.我有一个ListFragment,我想突出显示列表中选定的项目.我已经提出使用“选择器”的建议.我不明白如何使用这个选择器.我的ListFragment类是: // Create an adapter with list of stores and populate the list with // values Array 我已经发布了相同的问题几次,但还没有解决.我有一个ListFragment,我想突出显示列表中选定的项目.我已经提出使用“选择器”的建议.我不明白如何使用这个选择器.我的ListFragment类是:
// Create an adapter with List of stores and populate the List with        // values        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),androID.R.layout.simple_List_item_1,StoreList);        setlistadapter(adapter);        mDbHelper.close();    }    /*     * (non-Javadoc)     *      * Handles the event when an item is clicked on left pane,performs action     * based on the selection in left pane     *       * @see androID.app.ListFragment#onListItemClick(androID.Widget.ListVIEw,* androID.vIEw.VIEw,int,long)     */    @OverrIDe    public voID onListItemClick(ListVIEw l,VIEw v,int position,long ID) {        String selectedStore = (String) getlistadapter().getItem(position);        DetailFragment fragment = (DetailFragment) getFragmentManager()                .findFragmentByID(R.ID.detailFragment);        if (fragment != null && fragment.isInLayout()) {            v.setBackgroundcolor(getResources().getcolor(R.color.BLUE));            // passes selectedStore to detail fragment              fragment.setText(selectedStore);            // getItemList(selectedStore);        }

使用setBackground永久设置颜色,但是当选择另一个项目时,我希望它消失.
我了解如何在ListVIEw中使用选择器,但在我的情况下,如果我没有为lis​​tvIEw定义任何xml,那么我将如何使用“selector”?我正在使用预定义的androID.R.layout.simple_List_item_1.

解决方法 以下为我工作:

RES /颜色/ menu_highlight.xml

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID">  <item     androID:state_pressed="true"    androID:drawable="@color/red"  />  <item    androID:state_selected="true"    androID:drawable="@color/red" />  <item     androID:drawable="@color/white" /></selector>

RES /值/ colors.xml:

<?xml version="1.0" enCoding="utf-8"?><resources> <color name="white">#FFFFFF</color><color name="red">#FF0000</color></resources>

res / layout / menuitem.xml ::(列表中的每个项目的XML)

<?xml version="1.0" enCoding="utf-8"?><linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_height="wrap_content"    androID:layout_wIDth="fill_parent">    <TextVIEw        androID:ID="@+ID/textmenu"        androID:layout_height="wrap_content"        androID:text="text"        androID:textcolor="#FFFFFF"         androID:background="@color/menu_highlight"        androID:visibility="visible"        androID:layout_wIDth="fill_parent" /></linearLayout>

最后,在ListFragment类中,添加VIEw prevIoUs并将以下代码添加到onListitemclick函数中(在ListFragment: highlight selected row中提到)

public class MenuListFragment extends ListFragment{VIEw prevIoUs;@OverrIDe    public voID onListItemClick(ListVIEw l,long ID) {        super.onListItemClick(l,v,position,ID);        //Logic to highlight selected item        prevIoUs.setSelected(false);        v.setSelected(true);        prevIoUs=v;    }}
总结

以上是内存溢出为你收集整理的android – 在“ListFragment”中突出显示所选项目?全部内容,希望文章能够帮你解决android – 在“ListFragment”中突出显示所选项目?所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1133913.html

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

发表评论

登录后才能评论

评论列表(0条)

保存