ListView中长按不工作时,一个按钮在列表 [英] ListView long click not working when a button is in list

查看:209
本文介绍了ListView中长按不工作时,一个按钮在列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表适配器一个ListView。它有OnItemClickListener和OnItemLongClickListner它用来做工精细。此后,我不得不把一个按钮,在列表项和项目点击长按听众的布局停止工作。下面是我的示例code:

I have a ListView with custom list adapter. It has OnItemClickListener and OnItemLongClickListner which used to work fine. After then, I had to put a button in the layout of list item and the item click and long click listener stopped working. Here is my sample code:

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
            }
});

这用于正常工作的列表项的布局添加按钮之前:

This used to work fine before adding the button in the layout of list item:

<?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。谢谢你。

I tried changing inline listener to implementing onItemLongClickListener on activity but no success till now. Thanks.

推荐答案

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

Your image button is probably taking focus when you click on list item.

所以收藏此

android:descendantFocusability="blocksDescendants" 

你的根元素

<一个href=\"http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability\">http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

安卓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.

这篇关于ListView中长按不工作时,一个按钮在列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆