突出显示“ListFragment"中的选定项目?

编程入门 行业动态 更新时间:2024-10-24 23:26:45
本文介绍了突出显示“ListFragment"中的选定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经发布了几次相同的问题,但尚未解决.我有一个 ListFragment 并且我想突出显示列表中的所选项目.我得到了使用选择器"的建议.我不明白如何使用这个选择器.我的 ListFragment 类是:

I have posted the same problem a couple of times but it hasn't yet been resolved. I have a ListFragment and I want to highlight the selected item in the list. I have been given suggestions to use a "selector". I don't understand how to use this selector. My ListFragment class is:

// 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 中使用选择器,但在我的情况下,如果我没有为 ListView 定义任何 xml,那么我将如何使用选择器"?我正在使用预定义的 android.R.layout.simple_list_item_1.

Using setBackground sets the color permanently, but I want it to go away when another item is selected. I understand how to use a selector in a ListView but in my case if I haven't defined any xml for the Listview, then how would I use a "selector"? I am using android.R.layout.simple_list_item_1 which is predefined.

推荐答案

我没有得到我想要的,所以我继续挖掘并想出了一个便宜"的解决方案,这可能不是最佳实践,但可以解决问题.我希望在选择时突出显示该项目,并且当从 listFragment 中选择其他项目时颜色应该消失.这对我有用 - 我定义了一个静态的 View V;并初始化它 V = new View(getActivity());然后在我的

I wasn't getting what I wanted so I kept on digging and came up with a "cheap" solution which might not be the best practice but does the job. I wanted the item to be highlighted when selected and the color should go away when other item is selected from the listFragment. This is what worked for me- I defined a static View V; and initialized it V = new View(getActivity()); Then inside my

onListItemClick(ListView l, View v, int position, long id)

onListItemClick(ListView l, View v, int position, long id)

V.setBackgroundResource(0); v.setBackgroundResource(R.color.BLUE); V = v;

更多推荐

突出显示“ListFragment"中的选定项目?

本文发布于:2023-10-31 01:45:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   ListFragment   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!