如何检测单击列表视图项目的位置(列)(android)?

How to detect the place (column) where a list-view item has been clicked (android)?

我的 android 应用程序中有一个列表视图,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <TextView
        android:id="@+id/listname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="@+id/label"
        android:textSize="50px" >
    </TextView>
    <TextView
        android:id="@+id/listcurrent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="@+id/label"
        android:textSize="50px" >
    </TextView>
    <TextView
        android:id="@+id/listoffset"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="@+id/label"
        android:textSize="50px" >
    </TextView>
    <ImageView
        android:id="@+id/logo"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:layout_weight="0.1"
        android:src="@drawable/icon4" >
    </ImageView>


</LinearLayout>

其中有一些 ArrayAdapter。同样在主要 activity 中,我设置了一个监听器来检测是否有一个列表被点击:

 this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                                    long id) {

                Log.i("MainActivity", "test");

                // We know the View is a TextView so we can cast it
                TextView clickedView = (TextView) view.findViewById(R.id.listname);

                Toast.makeText(MainActivity.this, "Item with id [" + id + "] - Position [" + position + "] - Planet [" + clickedView.getText() + "]", Toast.LENGTH_SHORT).show();

            }
        });

其中 this.list 是列表。到目前为止一切正常,我看到包含 4 列的行; 3 个文本和一个带有图像。当我连续点击任何位置时,我会得到吐司,告诉我我刚刚点击了哪一行。到目前为止一切顺利!

但是如何确定我点击的呢?我点击了第一个 TextView 吗?我点击图片了吗?如何找出?

But how to determine the column where I clicked? Did I click on the first TextView? Did I click on the image? How to find out?

如果您对此类信息感兴趣,则需要在您感兴趣的每个组件上设置一个 View.OnClickListener。您可能需要一种方法来检索行中包含的信息,当 View.OnClickListener 调用回调。如果是这种情况,您将必须在 getView 中用位置标记单个视图。这样,您始终可以通过 ListView 检索该位置

的数据集项

前段时间我通过继承 ListView 来挂接触摸事件并存储触摸坐标以便稍后在 OnItemClickListener 中使用它们来解决这个问题:

private class MyListView extends ListView {
    public int lastDonwX;
    public int lastDownY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        lastDownX = ev.getX();
        lastDownY = ev.getY();
        return super.onInterceptTouchEvent(ev);
    }
}
...
this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {
        ...
        int clickX = ((MyListView)parentAdapter).lastDownX - view.getLeft();
        ViewGroup vg = (ViewGroup) view;
        for (int i = 0, count = vg.getChildCount(); i < count; i++){
            View v = vg.getChildAt(i);
            if (v.getLeft() <= clickX && v.getRight() > clickX){
                //This is the item clicked, do something with it
                ...
                break;
            }
        }
    }
}

鉴于仅当触摸标记仅移动一点时才会注册点击这一事实,您可以使用起始触摸手势位置来接收 OnItemClickListener 中的触摸坐标。虽然上面的解决方案很简单 - 它不考虑多次触摸 - 但我相信你理解这个概念。

这肯定比注册大量点击侦听器更好,而且没有任何副作用,比如破坏标准处理代码。