Android - OnClickListener和OnLongClickListener的问题

Phi*_* E. 1 layout android onlongclicklistener onclicklistener

我首先给布局充气,然后使用addView()向它添加一个View.该布局附加了一个OnLongClickListener.

如果我现在向内部视图添加OnClickListener,OnLongClickListener不再触发.我怎样才能解决这个问题?

示例代码:

View someLayout =  getLayoutInflater().inflate(R.layout.some_layout, null);
someLayout.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View arg0) {
            ...
        return true;
        }
});

TextView innerView = new TextView(this);

innerView.setText("JustSomeTextThatDoesNotMatter");
innerView.setOnClickListener(new OnClickListener() {    
    @Override
        public void onClick(View arg0) {
            ...
    );
}
});

((FrameLayout) view.findViewById(R.id.framelayout)).addView(innerView);
Run Code Online (Sandbox Code Playgroud)

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="300dp"
        android:layout_height="wrap_content">
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?这看起来真的很奇怪.

更新:当我使用onTouchListener而不是onLongClickListener时会发生同样的事情,内部视图中的OnClickListener会消耗所有触摸事件.

这是设计的吗?我想要的基本上是ContextMenu为ListView提供的功能.

cor*_*992 6

可点击View消耗所有触摸事件并阻止它们返回到父级.因此,在包含可点击View的布局的整个区域上注册长按的唯一方法是View在布局内的每个可点击内注册监听器.