ensure visible on android listview?

后端 未结 5 1848
灰色年华
灰色年华 2020-12-10 10:57

Is there a way that I can makle sure a given item in an android listview is entirely visible?

I\'d like to be able to programmatically scroll to a specific item, lik

5条回答
  •  情话喂你
    2020-12-10 11:05

    Try it:

    public static void ensureVisible(ListView listView, int pos)
    {
        if (listView == null)
        {
            return;
        }
    
        if(pos < 0 || pos >= listView.getCount())
        {
            return;
        }
    
        int first = listView.getFirstVisiblePosition();
        int last = listView.getLastVisiblePosition();
    
        if (pos < first)
        {
            listView.setSelection(pos);
            return;
        }
    
        if (pos >= last)
        {
            listView.setSelection(1 + pos - (last - first));
            return;
        }
    }
    

提交回复
热议问题