我已经发布了同样的问题了几次,却一直没有得到解决。我有一个 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
但对我来说,如果我还没有定义任何XML为列表视图
,那么我将如何使用选择?我使用 android.R.layout.simple_list_item_1
这是pdefined $ P $。
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选择。
这是我定义的静态什么工作我 - 视图V;
并初始化它 V =新景(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,视图V ,INT位置,长ID)
onListItemClick(ListView l, View v, int position, long id)
V.setBackgroundResource(0);
v.setBackgroundResource(R.color.BLUE);
V = v;