Android ListView setSelection() does not seem to work

前端 未结 17 1587
情歌与酒
情歌与酒 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:49

    I know this is an old question but I just had a similar problem that I solved in this way:

    mListView.clearFocus();
    mListView.post(new Runnable() {
        @Override
        public void run() {
            mListView.setSelection(index);
        }
    });
    
    0 讨论(0)
  • 2020-11-28 06:49

    I have an very large Request with Webcontent. When I used the code in onCreateView the Listview wasnt even finished loading. I put it in onPostExecute of my AsyncTask.

                //Get last position in listview
            if (listView != null && scrollPosition != 0) {
                listView.clearFocus();
                listView.requestFocusFromTouch();
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        listView.setItemChecked(scrollPosition, true);
                        listView.setSelection(scrollPosition);
                    }
                });
            }
    

    Dont forget to set the item checked in on Click ;)

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

    You might need to wrap setSelection() in a posted Runnable (reference).

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

    You can try 2 ways like these:
    Solution A:

        mListView.post(new Runnable() {
            @Override
            public void run() {
                if (null != mListView) {
                    mListView.clearFocus();
                    mListView.requestFocusFromTouch();
                    mListView.setSelection(0);
                }
            }
        });
    

    In some complicated situation, this solution will bring some new problems in Android 8.x.
    Besides it may cause unexpected onFocusChange().

    Solution B: Define a custom view extends ListView. Override method handleDataChanged().Then setSelection(0). In CustomListView:

    @Override
    protected void handleDataChanged() {
        super.handleDataChanged();
        if (null != mHandleDataChangedListener){
            mHandleDataChangedListener.onChanged();
        }
    }
    HandleDataChangedListener mHandleDataChangedListener;
    
    public void setHandleDataChangedListener(HandleDataChangedListener handleDataChangedListener) {
        this.mHandleDataChangedListener = handleDataChangedListener;
    }
    
    public interface HandleDataChangedListener{
        void onChanged();
    }
    

    In activity:

        mListView.setHandleDataChangedListener(new CustomListView.HandleDataChangedListener() {
            @Override
            public void onChanged() {
                mListView.setHandleDataChangedListener(null);
                mListView.setSelection(0);
            }
        });
        mAdapter.notifyDataSetChanged();
    

    Ok, That's it.

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

    Maybe you should use the smoothScrollToPosition(int position) method of ListView

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

    Found a solution in my case. I am not using a Runnable since my class is extending ListFragment. What I had to do is make my index a final; final index = 5; mListView.setSelection(index);

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