机器人 - 列表视图获取项目视图按位置视图、机器人、位置、项目

2023-09-11 20:32:51 作者:爷穿着校服闯酒吧

我的ListView自定义适配器(底座适配器)。我想通过位置获取视图列表视图从。我试过 mListView.getChildAt(位置),但它不工作。我怎样才能通过位置获取项目的看法?

I have listview with custom adapter (base adapter). I want to get view from listview by position. I tried mListView.getChildAt(position) , but it is not working. How can i get item view by position?

推荐答案

使用这样的:

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}