【问题标题】:ListView show ImageView OnClick on custom position outside ListViewListView 在 ListView 之外的自定义位置上显示 ImageView OnClick
【发布时间】:2015-03-19 01:44:31
【问题描述】:

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

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

请注意,此“工具提示”图像位于包含列表视图的布局中。

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

这是包含 listview 和 ImageView 的布局:

<?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>

【问题讨论】:

  • 那么ImageView和ListView在同一个容器中?也许您可以发布该布局 xml。
  • @krislarson 我添加了布局,以便您可以看到它
  • 点击时尝试获取listView的childView(convertView)。使用这个 childView getX as x 和 getY+getHeight as y 来定位您的 imageView,当然,您应该将 listView x,y 值相加。这可能会有所帮助。
  • @davidleen29 谢谢,我会试试的,你有什么用:当然,你应该把 listView x,y 值加起来。这可能有帮助吗?
  • 你计算的childView的x,y值只是相对于它的父级(listView),而不是相对于relativeLayout。所以必须将 listView x,y 加起来相对于 relativeLayout。(在你的情况下,没有必要)

标签: android xml android-listview


【解决方案1】:

您将其称为“工具提示”,因此听起来您希望它“浮动”在项目顶部。但是当用户滚动时会发生什么?

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

我做过类似的事情,我什至隐藏了展开/折叠指示器,这样 ListView 就不会看起来是 ExpandableListView。

【讨论】:

  • 您好,感谢您的回答,但我需要将其放在列表视图之外,就像您在自定义项目下方说“浮动”仅 2 秒一样,所以当用户滚动时我不会遇到问题。
【解决方案2】:

最后,为了实现这一点,我将“工具提示”图像放在我的基本活动中,并将其放置在屏幕上,我从单击的图像中获取 X 和 Y,并将工具提示放置在那里:

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

在我的 baseActivity 上:

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多