1

I'm making a listview with custom adapter / custom item layout (2x TextViews and 4x ImageButtons) and i want to be able to long press the list item to do something and still be able to press the ImageButtons on the layout.

The problem is that if i just have TextViews it works and catches the Long press event, as soon as i add an ImageButton the Long press event stops working. Any idea why this is happening?

4

1 回答 1

0

当您在项目布局中添加可点击视图(如按钮)时,它们会捕获点击事件并且它不会冒泡到底层ListView,要解决此问题,您可以将长点击侦听器添加到项目的根布局中。

因此,不要在您的活动中这样做:

ListView lv = (ListView) findViewById(R.id.lv);
lv.setOnItemLongClickListener(listener);

您在适配器中执行此操作:

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.item, parent, false);

    view.setLongClickable(true);
    view.setOnLongClickListener(listener);
}
于 2012-12-16T11:26:11.643 回答