Frage

I first inflate a Layout and later add a View to it using addView(). The layout has an OnLongClickListener attachted to it.

If I now add an OnClickListener to the inner view, the OnLongClickListener does no longer fire. How can I fix this?

Sample Code:

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

Layout

<?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>

Any Ideas? This really seems rather odd.

Update: The same thing happens when I use an onTouchListener instead of an onLongClickListener, the OnClickListener in the inner view consumes all touch events.

Is this by design? What I want basically is the same functionality that the ContextMenu provides to a ListView.

War es hilfreich?

Lösung

A clickable View consumes all touch events and prevents them from being returned to the parent. Therefore the only way to register for long clicks on the whole area of a layout containing clickable Views is to also register the listener on every clickable View inside the layout.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top