简体   繁体   中英

onclicklistener not working for dynamically created customview

i have class StickerImageView that extand StickerView class and StickerView extend FrameLayout

now i'm creating object of StickerImageView class and setup bitmap into it..

StickerImageView iv_sticker;
iv_sticker = new StickerImageView(this);
iv_sticker.setImageBitmap(result);

and making it visible to screen by adding view

mainscreen_relativelayout.addView(iv_sticker);

now i want to detact toast on that dynamically created view(iv_sticker) so i'm doing this

iv_sticker.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(),"i clicked",Toast.LENGTH_LONG).show();
    }
});

i also tried this for this custom view iv_sticker

iv_sticker.setClickable(true);

when i use OnTouchListener toast appear but view(iv_sticker) not getting movable anymore

iv_sticker.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            Toast.makeText(CropActivity.this,"i clicked",Toast.LENGTH_LONG).show();
            // Do what you want
            return true;
        }
        return false;
    }
});

this is log when i click on sticker

04-07 18:03:23.265 8208-8208/com.package.myappname V/com.knef.stickerView: sticker view action down
04-07 18:03:23.362 8208-8208/com.package.myappname V/PhoneWindow: debug app launch: MotionEvent.ACTION_UP
04-07 18:03:23.362 8208-8208/com.package.myappname V/com.knef.stickerView: sticker view action up

when i try ontouchevent , toast appear but sticker is not movable anymore

This is because you have consumed the touch event. Return false from the onTouch(...) method to make framework handle it for you again.

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