Get Absolute View Position in ListView

后端 未结 3 1664
[愿得一人]
[愿得一人] 2021-02-09 14:29

I am trying to make a popup window appear above/below an item that is clicked inside of a ListView. However, the problem is that the View that is coming in from the OnItemClick

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-09 14:49

    Try this and see if it works..

    private  void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }
    

    }

    //here listItem.getMeasuredHeight() will get u the height of each list item...U can get ur y position by height*clickedPosition

提交回复
热议问题