7

I have a ListView with 10 items in it, when i set the 4th item in the ListView as selected then my ListView gets scrolled and 4th item in the ListView gets placed at the top of the screen.

I don't want to scroll the ListView when i call setSelected() for the list item which is not in the view.

I have also tried to scroll the ListView programmatically using scrollTo(0, 0); but it is not working.

2

9 Answers 9

2

An old question, but it may be useful to somebody. You can use setSelectionFromTop. It's rather smart, and you can use it for example like that:

getListView().setSelectionFromTop(selection, getListView().getHeight() / 2);

It begins to scroll only after middle of the list:

setSelectionFromTop

2
  • it will also scroll the list, to make it in the view. Sep 18, 2014 at 12:32
  • You have to add more logic to achieve no scrolling
    – joe1806772
    Mar 15, 2017 at 21:55
2

Here's what I did:

int top = listView.getChildAt(fixedChildIndex).getTop();

//set selected item, keep scroll position
listView.setSelectionFromTop(newIndex, top);

This keeps the ListViews Child with view index (ListView reuses views!) fixedChildIndex in place.

For example if you want the highlighting to stay use 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 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 view of the child you want to select. You can get it by doing this:

1

I think it's hardcoded and thus not possible without scrolling

2
1

you can use listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); option with

mListView.setItemChecked(pos, true);

Setup some selectors may be required. Also look for google example IOSchedule App --great example about listview with multi selection controller (have a few bugs :) )

1
  • Then probably you use custom onClickListener. Have you checked google io example? it's works good Mar 29, 2017 at 8:15
0

setSelection() may be of your work.

1
  • Currently i am using setSelection and it is scrolling the list view. Mar 26, 2012 at 13:17
0

Do NOT use setSelection()!

I do that in another way.

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

This worked for me -

  1. Set CHOICE_MODE_SINGLE when setting up the custom listview -
  2. Use setSelected() inside OnItemClickListener() as under -

    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
0

You can override requestChildRectangleOnScreen like this to get rid of the scrolling:

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

Use setSelection(4) to set the Selection without scrolling if the new selection is visible.

1
  • Currently i am using setSelection and it is scrolling the list view. Mar 26, 2012 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.