当列表中存在一个按钮时,ListView的长按功能无法工作

7

我有一个带有自定义列表适配器的ListView。它有OnItemClickListener和OnItemLongClickListner,之前都能正常工作。但后来,我不得不在列表项的布局中添加了一个按钮,然后项目点击和长按监听器就停止工作了。这是我的示例代码:

ListView lv=(ListView)findViewbyId(R.id.listview); 
lv.setAdapter(listviewadapter);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
            // My code
            }
});

在列表项布局中添加按钮之前,这个功能运行得很好:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:padding="2dp"
    >
    <TextView
        android:id="@+id/symbol_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2.5"
        android:layout_gravity="left"
        />
    <TextView
        android:id="@+id/ltp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/change_in_perc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/volume"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />

    <ImageButton
        android:id="@+id/chart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0.5"
        android:src="@drawable/charts"
        android:contentDescription="Chart Link"
        />
</LinearLayout>

我尝试将内联监听器更改为在活动上实现onItemLongClickListener,但到目前为止还没有成功。谢谢。

这可能是情况。我正在尝试。但如果它会夺取焦点,那么当我点击列表视图时,按钮的单击事件不应该被触发吗? - Sourabh
2
这个有效。请把它作为您的答案添加,以便我可以接受它。 - Sourabh
2个回答

13

当您单击列表项时,您的图像按钮可能会获取焦点。

因此,请添加以下内容:

android:descendantFocusability="blocksDescendants" 

到您的根元素

http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

Constant    Value   Description
beforeDescendants   0    The ViewGroup will get focus before any of its descendants.
afterDescendants    1    The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants   2    The ViewGroup will block its descendants from receiving focus.
This corresponds to the global attribute resource symbol descendantFocusability.

1
非常感谢。一旦stackoverflow允许,我会接受这个答案。 :) - Sourabh
请帮帮我。我现在已经卡在这个问题上7个小时了,而且这是一个非常简单的问题。http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 - Ruchir Baronia

0

你在XML中设置了android:longClickable="true",并且在代码中使用了listview.setLongClickable(true)吗?


请帮帮我。我已经卡在这个问题上7个小时了,而且它是一个非常简单的问题。http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 - Ruchir Baronia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接