问题
I have to set focus on listview item. ListView consist of TextView. point where i want to set focus on listview item, at that point i have the position of that item.
I have tried setSelectio(pos), where pos is the position of listview item, but this results in setting the position of item at position on the top of the screen.
please suggest me how to proceed.
builder.setPositiveButton(R.string.save,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("@gaurav", "changeText is not null");
if (null != changeText) {
Log.d("@gaurav", "changeText is not null");
if (!(changeText.getText().toString().equalsIgnoreCase(""))) {
myList
.set(pos, changeText.getText()
.toString());
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
} else {
Log.d("@gaurav", "changeText is null");
}
getListView().setFocusableInTouchMode(true);
getListView().setSelection(pos);
getListView().requestFocus();
}
});
回答1:
I think you want to highlight a particular list item based on your requirement. So you are not able to set the focus if the item is not visible in the screen. Am I right ? If that is your problem you can use following method public void smoothScrollToPosition (int position)
Added in API level 8
to scroll to that particular position and then use setSelection() method
回答2:
If you want to set focus/color on particular index of ListView
, you should try this, it works well.
list.post(new Runnable() {
@Override
public void run() {
list.setSelected(true);
list.getChildAt(0).setBackgroundColor(Color.BLACK);
list.getChildAt(1).setBackgroundColor(Color.BLUE);
firstListItemPosition = firstListItemPosition +1;
Log.v("firstListItemPosition", "firstListItemPosition ");
}
});
firstListItemPosition = list.getFirstVisiblePosition();
来源:https://stackoverflow.com/questions/13657136/how-to-set-focus-in-listview-item