ListView 在 ListView 外的自定义位置显示 ImageView OnClick

ListView show ImageView OnClick on custom position outside ListView

我有一个包含自定义项的列表视图,并且有些项具有 OnClickListener。我想做的是当我点击自定义项目显示和该项目下方的 ImageView 时,但是该图像比列表视图的单元格大,所以我需要将它放在它外面并确定 X 和 Y,然后只显示它在该单元格的项目下方。我尝试了很多东西,但我没有在正确的位置获取图像。有什么想法可以实现吗?

这是我的最后一个代码,但我仍然无法在所有情况下都很好地工作。

请注意,此 "tooltip" 图片位于包含列表视图的布局中。

    tooltipImage.setX(mListView.getX()); 
    tooltipImage.setY(mListView.getY() - getTotalHeightofListView() / mListView.getChildCount());
    tooltipImage.setVisibility(View.VISIBLE);

这是包含列表视图和图像视图的布局:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >


    <ListView
        android:id="@+id/custom_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:scrollbars="none" />


   <ImageView
       android:id="@+id/tootltip"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentEnd="true"
       android:layout_alignParentRight="true"
       android:scaleType="centerInside"
       android:src="@drawable/tootltip"
       android:visibility="gone" />

</RelativeLayout>

您称它为 "tool tip",所以听起来您希望它 "float" 在项目的顶部。但是当用户滚动时会发生什么?

这里有一个建议:您可以使用 ExpandableListView 来实现类似的目的。您的列表项将是第一级。当您单击列表项时,它会展开以在其下方显示您的 ImageView。这样,当用户滚动时,ImageView 将随之移动。

我曾做过类似的事情,我什至隐藏了 expand/collapse 指示器,这样 ListView 看起来就不像是 ExpandableListView。

最后,为了完成这个,我将 "tooltip" 图像放在我的基础 Activity 中,然后将其放置在屏幕上,我从单击的图像中获取 X 和 Y,并将工具提示放置在那里:

int[] listViewCoords = new int[2];
listView.getChildAt(pos).findViewById(R.id.image).getLocationOnScreen(listViewCoords);
getBaseActivity(listViewCoords[0] + imageWidht, [1] + imageHeight);

在我的基地Activity:

toolTip.setVisibility(View.VISIBLE);
toolTip.setX(x);
toolTip.setY(y);