How to set selected item of ListView without scrolling view [Android]
I have ListView
with 10 items in it, when I set the 4th item in the ListView as selected, then mine ListView
gets scrolled and the fourth item is ListView
placed at the top of the screen.
I don't want to scroll ListView
when I call setSelected()
on a list item that is not in the view.
I also tried to scroll ListView
programmatically using scrollTo(0, 0);
, but it doesn't work.
source to share
you can use listview.setChoiceMode (ListView.CHOICE_MODE_SINGLE); from
mListView.setItemChecked(pos, true);
Some selectors may need to be installed. Also see Google example IOSchedule app - great example about listview with multiple select controllers (there are a few bugs :))
source to share
Old question, but it might be helpful to someone. You can use setSelectionFromTop . It's pretty smart and you can use it like this:
getListView().setSelectionFromTop(selection, getListView().getHeight() / 2);
It only starts scrolling after the middle of the list:
source to share
DO NOT use setSelection ()!
I do it differently.
private AdapterView.OnItemClickListener oiclFolders = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int posi, long id) {
// clear original selected item
if ( mViewOrg!=null ) mViewOrg.setBackgroundDrawable(null);
if ( view!=null ) {
// define your own drawable to set as background
view.setBackgroundResource(R.drawable.item_bg);
mViewOrg = view;
}
curSel = posi; // to store which one is selected
// Do NOT setSelection -> will auto scroll to top
// mlvFolders.setSelection(posi);
}
};
source to share
This worked for me -
- Set CHOICE_MODE_SINGLE when setting up a custom list -
-
Use setSelected () inside OnItemClickListener () as below -
private void setup_view(View rootView) { listview = (ListView) rootView.findViewById(R.id.list); if(listview != null) { listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listview.setAdapter(new MyAdapter(getActivity().getApplicationContext())); listview.setOnItemClickListener(onitemclicklistener); } } private OnItemClickListener onitemclicklistener = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { v.setSelected(true); } };
source to share
Here's what I did:
int top = listView.getChildAt(fixedChildIndex).getTop();
//set selected item, keep scroll position
listView.setSelectionFromTop(newIndex, top);
This keeps the child ListViews with the view index (ListView reuses views!) FixedChildIndex in place.
For example, if you want the highlight to keep using this:
int first = listView.getFirstVisiblePosition();
int fixedChildIndex = listView.getSelectedItemPosition() - first;
View newSelectedChild = listView.getChildAt(selectedViewIndex);
If you want to make sure the selected item is fully visible then use this line
//set selected item, keep scroll position and make sure it is completely visible
listView.setSelectionFromTop(newIndex, Math.min(Math.max(0,top), listView.getHeight()-newSelectedChild.getHeight()));
Where newSelectedChild is the kind of child you want to select. You can get it by following these steps:
source to share