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.

+3


source to share


9 replies


I think this is hardcoded and therefore not possible without scrolling



+1


source


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 :))

+1


source


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:

setSelectionFromTop

+1


source


setSelection()

could be your job.

0


source


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);
    }
};

      

0


source


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);
        }
    };
    
          

0


source


You can override requestChildRectangleOnScreen

like this to get rid of scrolling:

 @Override
    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
        return false;
    }

      

0


source


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:

0


source


Use setSelection(4)

to set the selection without scrolling if the new selection is visible.

-1


source







All Articles