如何在Android中突出显示ListView中的行? [英] How to highlight row in ListView in Android?
问题描述
我需要突出显示 ListView
中已选择的一行(向用户显示他选择的内容),因此,不是要选择的行,而是他选择的行之前.
我已经有了位置:
ListView.setSelection(position);
现在我想要的是选择这一特定行并突出显示它.
包含ListView
的Activity中onCreate()
的代码:
公共类CountryView扩展Activity{受保护的静态最终字符串LOG_TAG = null;/** 在第一次创建活动时调用.*/字符串[] lv_arr = {};ListAdapter 适配器;文本视图 t;私有 ListView lvUsers;私有 ArrayListmListUsers;字符串响应=空;公共国际d;int selectedListItem = -1;@覆盖public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.country);意图数据 =getIntent();mListUsers = getCoun();lvUsers = (ListView) findViewById(R.id.counlistView);lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers));selectedListItem=data.getExtras().getInt("PositionInList");lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);lvUsers.setOnItemClickListener(new OnItemClickListener(){内部位置项;public void onItemClick(AdapterView> parent, View view,int position, long id){Intent pongIntent = new Intent(getApplicationContext(),Trav.class);int could=mListUsers.get(position).id;pongIntent.putExtra("response",mListUsers.get(position).p);pongIntent.putExtra("responseCounID",counId);//把选择列表的位置放在extra里面位置项=位置;pongIntent.putExtra("PositionInListSet",positionItem);setResult(Activity.RESULT_OK,pongIntent);Log.i("CounID *******************************",""+could);结束();}});}}
由于默认情况下 ListViews
设置为 NONE
选择模式,在触摸模式下 setSelection
方法不会有视觉效果.
为了保持之前的选择/直观地显示一个明确的选择,首先你必须适当地设置你的列表视图的选择模式:
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
阅读这些方法的 API 文档很有用:
- setSelection
void android.widget.AdapterView.setSelection(int position)
设置当前选中的项目.到支持可访问性子类重写此方法必须调用首先覆盖超级方法.
参数:position
要选择的数据项的索引(从 0 开始).
- setChoiceMode
void android.widget.ListView.setChoiceMode(int choiceMode)
定义选择行为列表.默认情况下,列表没有任何选择行为(CHOICE_MODE_NONE
).通过设置choiceMode 到 CHOICE_MODE_SINGLE
,列表最多允许一个项目在一个选择的状态.通过设置选择模式到CHOICE_MODE_MULTIPLE
,该列表允许任意数量的项目被选中.
参数:
choiceMode
CHOICE_MODE_NONE
之一,CHOICE_MODE_SINGLE
或 CHOICE_MODE_MULTIPLE
如果这还不够(假设您希望始终以不同方式显示当前选择之外的最后一个选择),您应该存储最后选择的项目(填充 ListAdapter
的数据)作为 lastSelectedItem
,并且在您的适配器的 getView
方法中,如果它等于这个 lastSelectedItem
,则将不同的背景资源分配给渲染器.
如果您的最后一个选择不会在选择更改时刷新,您应该在您的适配器实例上显式调用 notifyDataSetChanged
方法.
更新
由于包含 ListView
的活动是等待此结果的活动的子项(基于 setResult(Activity.RESULT_OK,pongIntent);
部分),初始想法是正确的,最后一个位置应该在开始activity的时候通过intent传递:
selectedListItem = getIntent().getIntExtra("PositionInList", -1);lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);lvUsers.setSelection(selectedListItem);
ListView.CHOICE_MODE_SINGLE
解决方案如果您保持在同一个活动中将起作用,但是您在每个 itemClick 上完成它(选择更改),这就是为什么应该将额外数据传递到起始 <代码>意图代码>.
您还可以从您的适配器中设置先前选择的项目的背景 - 如上所述-,覆盖其 getView
方法:
lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups){@覆盖public View getView(int position, View convertView, ViewGroup parent){最终视图渲染器 = super.getView(position, convertView, parent);如果(位置 == selectedListItem){//TODO: 在此处设置正确的选择颜色:renderer.setBackgroundResource(android.R.color.darker_gray);}返回渲染器;}});
I need to highlight a row in a ListView
that was selected (to show the user what he chose), so, it's not the one that is going to be chosen, it's the one he chose before.
I already have the location by:
ListView.setSelection(position);
And now what I want is to select this specific row and to highlight it.
The code of the onCreate()
in the activity that contains the ListView
:
public class CountryView extends Activity
{
protected static final String LOG_TAG = null;
/** Called when the activity is first created. */
String[] lv_arr = {};
ListAdapter adapter;
TextView t;
private ListView lvUsers;
private ArrayList<Coun> mListUsers;
String responce=null;
public int d;
int selectedListItem = -1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
Intent data =getIntent();
mListUsers = getCoun();
lvUsers = (ListView) findViewById(R.id.counlistView);
lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers));
selectedListItem=data.getExtras().getInt("PositionInList");
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setOnItemClickListener(new OnItemClickListener()
{
int positionItem;
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent pongIntent = new Intent(getApplicationContext(),Trav.class);
int counId=mListUsers.get(position).id;
pongIntent.putExtra("response",mListUsers.get(position).p);
pongIntent.putExtra("responseCounID",counId);
//Put the position of the choose list inside extra
positionItem=position;
pongIntent.putExtra("PositionInListSet",positionItem);
setResult(Activity.RESULT_OK,pongIntent);
Log.i("CounID *******************************"," "+counId);
finish();
}
});
}
}
Since by default ListViews
are set to a selection mode of NONE
, in touch mode the setSelection
method won't have visual effect.
For keeping the previous selection / visually display an explicit selection, first you must set your listview's choice mode appropriately:
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
It's useful to read the API Docs of these methods:
- setSelection
void android.widget.AdapterView.setSelection(int position)
Sets the currently selected item. To support accessibility subclasses that override this method must invoke the overriden super method first.
Parameters:
position
Index (starting at 0) of the data item to be selected.
- setChoiceMode
void android.widget.ListView.setChoiceMode(int choiceMode)
Defines the choice behavior for the List. By default, Lists do not have any choice behavior (
CHOICE_MODE_NONE
). By setting the choiceMode toCHOICE_MODE_SINGLE
, the List allows up to one item to be in a chosen state. By setting the choiceMode toCHOICE_MODE_MULTIPLE
, the list allows any number of items to be chosen.Parameters:
choiceMode
One ofCHOICE_MODE_NONE
,CHOICE_MODE_SINGLE
, orCHOICE_MODE_MULTIPLE
In case this is not enough (say you'd like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the ListAdapter
) as lastSelectedItem
, and in your adapter's getView
method assign a different background resource to the renderer if it equals this lastSelectedItem
.
If your last selection wouldn't refresh on selection change, you should explicitly call the notifyDataSetChanged
method on your adapter instance.
Update
Since your activity containing the ListView
is a child of an activity which waits for this one's result (based on the setResult(Activity.RESULT_OK,pongIntent);
part), the initial idea is correct, the last position should be passed through the intent when starting the activity:
selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);
The ListView.CHOICE_MODE_SINGLE
solution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that's why the extra data should be passed to the starting Intent
.
You can also set the previously selected item's background from your adapter -as mentioned above-, overriding its getView
method:
lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (position == selectedListItem)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}
return renderer;
}
});
这篇关于如何在Android中突出显示ListView中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!