The ViewFlipper onTouch event DOES NOT fire while its onClick child button

I am trying to find a solution in StackOverflow or elsewhere, but I could not find a direct answer to this question, perhaps, with the possible exception of the closest, which I can no longer find. However, sorry in advance if this is simply because I'm missing something, or I'm just such a big noob.

In any case, I'm trying to set OnTouchListener to ViewFlipper (parents) and setOnClickListener to Button (child), which fills the parent layout. I need to raise the ViewFlipper onTouch () event first. Thought if ViewFlipper onTouch () returns false, the onClick () button has been started. However, only the onClick () button was called. What for? Is there a specific priority?

Alternatively, I can set the onTouchListener to Button, because Button has a match_parent attribute, so touching this viewflipper was almost the same as touching this button, even if I did this, it just makes the Button onClick () event not working ... .

Below is my simplified activity;

public class TestActivity extends Activity implements OnTouchListener, View.OnClickListener {
@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.viewFlipper1);
    Button btn = (Button) this.findViewById(R.id.btnTest1);
    vf.setOnTouchListener(this);
    btn.setOnClickListener(this);

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <include
        android:id="@+id/button"
        layout="@layout/test" />
</ViewFlipper>
</LinearLayout>

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<Button
    android:id="@+id/btnTest1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Button" />
</LinearLayout>
+3
source share
1 answer

I still don't understand why the ViewFlipper onTouch event was not triggered first, but I found a way to avoid getting out of this problem. We can set the onClickListener and onTouchListener buttons.

btn.setOnClickListener(this);
btn.setOnTouchListener(this);

As the original post said, this prevents the onClick () event, but whatever it is? If it is not called by the Android system, we must call it manually.

@Override public boolean onTouch (View v, MotionEvent event) {

// walls of codes 

this.onClick(v);

, . 'Click', 0,5 , .

0

All Articles