简体   繁体   中英

Dynamic TextView onClickListener Android

I'm adding an onClickListener to a dynamic TextView with a CompoundDrawable and it doesnt seem to be working.

CODE

for (Product p: mProductList) {     
    ...     

    TextView textView = new TextView(this);
    if (bmp != null) {
        Drawable d = new BitmapDrawable(getResources(), bmp);
        textView.setText(name);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
        textView.setPadding(5, 0, 5, 0);
        textView.setTag(unique);
        textView.setClickable(true);
        textView.setOnClickListener(mProductListener);
        mProductCarousel.addView(textView, sortOrder);
    }
}

mProductListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.v("TESTING", "CLICKED");
        String productId = String.valueOf(v.getTag());
        Log.v("PRODUCT ID", productId);
    }
};

The TextView with Drawable are added to the layout with no problem, but the click event is not firing. I never receive either Log message. Any help is appreciated.

I'm not sure. This is just a hunch let me know if it pays of:

if (bmp != null) {
    Drawable d = new BitmapDrawable(getResources(), bmp);
    textView.setText(name);
    textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
    textView.setPadding(5, 0, 5, 0);
    textView.setTag(unique);
    textView.setClickable(true);
    textView.setFocusableInTouchMode(true);
    textView.setOnClickListener(mProductListener);
    mProductCarousel.addView(textView, sortOrder);
}

If it doesn't try the alternative:

textView.setFocusable(true);

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