我已经发布了几次相同的问题,但尚未解决.我有一个 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"中的选定项目?
发布评论