简体   繁体   中英

onTouchListener() in Android not called after including ImageView from BaseActivity-XML

The following code shows an abstract Activity (AppActivity), other Activities have to extend from. There is already a layout defined, but inherits nothing more then including a round button (extending ImageView).

MainActivity defines an own layout, which has to include the button mentioned before. Looking at my current results, the Button sucessfully is inherited and shown, can not be null

Nevertheless, the onTouchListener() attached to this ImageView never gets called from perspective "MainActivity". Note, that i return true if MotionEvent.ACTION_DOWN and UP occurs. So this cant be the cause. Any suggestions?

Abstract Activity with CircleButton (extends ImageView) inclusive onTouchListener()

public abstract class AppActivity extends Activity {

    public CircleButton circleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_btn_layout);

        circleButton = (CircleButton) findViewById(R.id.voiceControlBtn);

        circleButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("TOUCH", "");
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ButtonGotPressed();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    circleButton.ButtonGotReleased();
                    return true;
                }
                return false;
            }
        });
}

Activity extending from AppActivity

  public class MainActivity extends AppActivity implements ICadeCommand {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(eu.alfred.batterywarnerapp.R.layout.activity_main);
        }

activity_main.xml (for MainActivity)

<RelativeLayout .../>

    <include layout="@layout/voice_btn_layout" />

</RelativeLayout>

voice_btn_layout.xml (for AppActivity)

    <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:layout_margin="15dip"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true">

            <packagename.CircleButton
                ...
                android:scaleType="fitXY"/>

    </FrameLayout>

You are setting OnTouchListener for circleButton in R.layout.voice_btn_layout .

When you call setContentView() in MainActivity , you replace the layout R.layout.voice_btn_layout with eu.alfred.batterywarnerapp.R.layout.activity_main . Therefore the onTouch() method for circleButton in AppActivity is not called because the circleButton shown on the screen is not the same as the circleButton in AppActivity .

You can test if this is indeed the case by adding an extra line in AppActivity

public abstract class AppActivity extends Activity {

public CircleButton circleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voice_btn_layout);

    circleButton = (CircleButton) findViewById(R.id.voiceControlBtn);
    circleButton.setVisibility(View.GONE);     // ADD THIS LINE 
    circleButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        ...

You should notice that circleButton is still visible thus showing that the circleButton displayed on screen is not the same as the one in AppActivity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM