Android ListView setSelection() does not seem to work

前端 未结 17 1588
情歌与酒
情歌与酒 2020-11-28 06:45

I have a ListActivity that implements onListItemClick() and calls a doSomething() function of the class. The latter contains l.s

相关标签:
17条回答
  • 2020-11-28 06:54

    I found that sometimes setSelection will not work because I set attribute "android:height" of listView to "wrap_content".

    And the times my App won't work is that when listView become scrollable from non-scrollable.

    For example, if my app is "File Browser App". When my list is less than, let's say 6, then it's non-scrollable. Now I return to the parent directory, and it has 11 objects, and I want to set selection to some position, and it won't work here.

    to\from    |    Scrollable  | non-Scrollable
    

    Scrollable | O | O( of course )

    non-Scrollable | X | O( of course )

    I don't want to use post(Runnable), because there will has delay.

    ==================================

    Answer:

    You can try to set "android:height" to "match_parent"

    God, it spends three days.

    0 讨论(0)
  • 2020-11-28 06:55

    When use post to setSelection(), the ListView will see first , then scroll to the position , thank to "魏經軒", then layout actually will effect the setSelection(), because setSelection() call the setSelectionFromTop(int position, int y), there is another way to solve it.

    listView.setAdapter(listView.getAdapter());
    listView.setSelection(123);
    
    0 讨论(0)
  • 2020-11-28 06:56

    In my case smoothScrollToPosition(int position) worked, can you also tell me how to set that scrolled position into center of the list. It appeared at the bottom of visible items.

    0 讨论(0)
  • 2020-11-28 06:58

    If you use an Adapter for your ListView add this code to your adapter:

    public class MyAdapter extends
            ArrayAdapter<MyClass> {
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflator = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflator.inflate(R.layout.my_adapter, null);
            } else {
                rowView = (View) convertView;
            }
    
            //...
    
            // set selected item
            LinearLayout ActiveItem = (LinearLayout) rowView;
            if (position == selectedItem)
            {
                ActiveItem
                        .setBackgroundResource(R.drawable.background_dark_blue);
    
                // for focus on it
                int top = (ActiveItem == null) ? 0 : ActiveItem.getTop();
                ((ListView) parent).setSelectionFromTop(position, top);
            }
            else
            {
                ActiveItem
                        .setBackgroundResource(R.drawable.border02);
            }
    
        }
    
        private int selectedItem;
    
        public void setSelectedItem(int position) {
            selectedItem = position;
        }
    
    }
    

    In your Activity:

    myAdapter.setSelectedItem(1);
    
    0 讨论(0)
  • 2020-11-28 06:59

    For me the solution to this problem was:

    listView.clearChoices();
    
    0 讨论(0)
  • 2020-11-28 07:01

    For me calling

    listView.notifyDataSetChanged();
    listView.requestFocusFromTouch();
    

    and then

     listView.setSelection(position);
    

    solved the issue.

    if you do that in a runnable it works without calling requestFocusFromTouch(), but the old position of the ListView is showen for a sekound.

    0 讨论(0)
提交回复
热议问题