gpt4 book ai didi

android - listView pointToPosition 不正确 'half the time'

转载 作者:行者123 更新时间:2023-11-30 03:29:13 26 4
gpt4 key购买 nike

我正在尝试使用 list.pointToPosition 来确定用户手指触摸的是哪个列表项位置。我的列表包含 TextView,后跟 RelativeLayout 中的 ListView。我发现的问题是 pointToPosition 似乎忽略了我有一个 TextView 并返回错误的项目位置这一事实。

让我用一些图片和一些被退回的样本进一步解释。我已经启用了让我跟踪手指触摸的调试功能。您将在下图中的蓝线中看到它。

在下图中,您会注意到屏幕顶部是一个标有“LIGHTS”的 TextView。正下方是我的 ListView。查看跟踪我的手指触摸和滑动的蓝线 - 您会注意到它开始于标有“Front Porch”的行的顶部附近。在该位置执行“pointToPosition”返回正确的值 4。

Correct pointToPosition

但是现在,如果我开始触摸同一行的一半,pointToPosition 返回值 5。您可以在下面的屏幕截图中看到我的触摸/拖动。

Wrong position

当我将 TextView 的大小更改为 1dp 的高度时,问题就消失了;因此我相信 pointToPosition 方法假定我的 ListView 是页面上唯一的东西。

我可以通过计算由 TextView 引起的偏移来实现 hack 并处理这个问题,但我的感觉是当这个错误(假设它是一个错误)被修复时,这最终会回来咬我。我想知道是否有适当的方法来修复它。

源代码示例:

layout.xml

<com.mls.util.RelativeLayoutTouchInterceptor xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/app_bg"
android:id="@+id/rlRoot" android:clickable="true"
tools:context=".DeviceListActivity" >

<TextView android:id="@+id/list_header"
style="?android:listSeparatorTextViewStyle"
android:text="Lights"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>

<ListView
android:id="@+id/dlList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="65dp"
android:layout_below="@+id/list_header"
android:layout_alignParentLeft="true">
</ListView>

</com.mls.util.RelativeLayoutTouchInterceptor>

RelativeLayoutTouchInterceptor.java

public class RelativeLayoutTouchInterceptor extends RelativeLayout {
...

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {

switch(event.getAction()) {
case MotionEvent.ACTION_DOWN: {

downStart = MotionEvent.obtain(event);

ListView list = (ListView)this.findViewById(R.id.dlList);
int position = list.pointToPosition((int)downStart.getX(), (int)downStart.getY());
}

...

}

最佳答案

发生这种情况的原因是因为您覆盖了 RelativeLayoutonInterceptTouchEvent,而不是 ListView。它报告布局本身内的 y 坐标,其中包括 TextView

如果您只想获取相对于 ListView 的坐标,您可以为 ListView 本身覆盖它。它是 ViewGroup 的间接子类,因此可以访问该方法。

没有需要修复的 bug,它正常工作。

关于android - listView pointToPosition 不正确 'half the time',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com