ensure visible on android listview?

后端 未结 5 1856
灰色年华
灰色年华 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

    I have a shorter and, in my opinion, better solution to do this : ListView requestChildRectangleOnScreen method is designed for it.

    The answer above ensures that the item will be displayed, but sometimes it will be displayed partly (ie. when it is at the bottom of the screen). The code below ensures that the whole item will be displayed and that the view will scroll only the necessary zone :

        private void ensureVisible(ListView parent, View view) {
        Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        parent.requestChildRectangleOnScreen(view, rect, false);
    }
    

提交回复
热议问题