ensure visible on android listview?

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

    Recently I met the same problem, paste my solution here in case someone need it (I was trying to make the entire last visible item visible):

        if (mListView != null) {
            int firstVisible = mListView.getFirstVisiblePosition()
                    - mListView.getHeaderViewsCount();
            int lastVisible = mListView.getLastVisiblePosition()
                    - mListView.getHeaderViewsCount();
    
            View child = mListView.getChildAt(lastVisible
                    - firstVisible);
            int offset = child.getTop() + child.getMeasuredHeight()
                    - mListView.getMeasuredHeight();
            if (offset > 0) {
                mListView.smoothScrollBy(offset, 200);
            }
        }
    

提交回复
热议问题