I've created a custom ImageView
class, and I'm trying to change the imageResource
when user click on it, but I'm able to call setImageResource()
from that class.
Also I'd like to store like a second imageView
I mean, my custom ImageView
has the same starter imageView
resource, but when click on it it have to be dynamic ImageView
for instance:
ImageView1 ic_launcher (user has not clicked on it)
ImageView1 ic_user (user has clicked on it)
Can you guide how to achieve this?
This is my custom ImageView
class :
public class CustomImageView extends android.support.v7.widget.AppCompatImageView implements View.OnClickListener {
private View.OnClickListener clickListener;
public CustomImageView(Context context) {
super(context);
setOnClickListener(this);
}
public CustomImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(this);
}
@Override
public void setOnClickListener(OnClickListener l) {
if (l == this) {
super.setOnClickListener(l);
} else {
clickListener = l;
}
}
@Override
public void onClick(View v) {
//Should change the imageResource here but also I should have to change it again if user wants (to the initial one)
if (clickListener != null) {
clickListener.onClick(this);
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…