I have a recyclerview which has animation added for each item in onBindViewHolder() which results in multiple calls for single item in recycler view.
My requirement is to zoom in image on selection of an item in recycler-view. I have started an animation in onBindViewHolder() for each item which has resulted in continuous calls of onBindViewHolder() for same item.
#Override
public void onBindViewHolder(#NonNull BaseViewHolder viewHolder, int viewType) {
viewHolder.imageView.animate().scaleX(scaleX).scaleY(scaleY).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
viewHolder.imageView.animate().setListener(null);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
}).setDuration(THUMBNAIL_ZOOMING_DURATION).start();
}
Imageview should zoom in by 1.15f when an item gets selected with animation and should get zoom out if deselected.
onBindViewHolder is not a good place for animations. Try using onViewAttachedToWindow to start and onViewDetachedFromWindow to stop animations
Related
I have a form with a clickeable cardview. Depending on some of the options, I hide the CardView setting its visibility to GONE.
Other form controls below the card come up to occupy the space the cardview was taking. However when clicking in the area the Cardview used to be, the onclick event is fired for the cardview.
I even put a check to verify the visibility before the click action, but the cardview repots VISIBLE (even though its not being shown). I use Butterknife for the onClick event:
#OnClick(R.id.logo_cardview)
public void onLogoClick(){
if (mLogoCardView.getVisibility() == VISIBLE) {
Snackbar.make(this, "Logo clicked", Snackbar.LENGTH_SHORT).show();
showSettingsDialog();
}
}
I hide the Cardview using an animation:
mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
mAlphaAnimation.setDuration(200);
mAlphaAnimation.setFillAfter(true);
mHideListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mMatchInfo.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
};
I'm creating an application for Android, and in this moment i want just create a selectable list using a RecyclerView, for this, when a user clicks on an item, I put its id in an array and I change its icon.
The problem is that, when I scroll down and then I scroll up again or when I change fragment, the array still contains all the id of the items, but the icons comes back as before, they don't mantain the new image that I set.
This is the onBindViewHolder metod, this method is the only in the whole project which change the imageview :
List<Integer> selected = new LinkedList<>();
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
final int pos = position;
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Context context = v.getContext();
Animation to_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.to_middle);
final Animation from_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.from_middle);
to_middle.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
holder.mImageView.setImageResource(R.drawable.ic_action_navigation_check);
holder.mImageView.clearAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
holder.mImageView.setAnimation(to_middle);
holder.mImageView.startAnimation(to_middle);
selected.add(pos);
}
});
if(!selected.contains(position)) {
/* I checked and this method it's not called when
an item is selected, so the following instruction
isn't the cause of my problem, anyway the image
comes back as before */
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_default)
.fitCenter()
.into(holder.mImageView);
}
}
I'm sure that R.drawable.ic_default isn't used elsewhere, so which method is changing the image?
Thank you
if(selected.contains(position)) { // Check if already selected
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_action_navigation_check) // set selected drawable here.
.fitCenter()
.into(holder.mImageView);
}
You are checking that "if not selected set image to default" but you need to check "if selected set image to selected image".
I am trying to have a view animated in my app and am using NineOldAndroid for animations.
The desired effect is to fade the view out and then set it's visibility to gone so that it doesn't get clicked while invisible. Here is How I do it.
ViewPropertyAnimator.animate(view).alpha(0).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
The problem here is that the listener above sticks with the view and when I try to fade it in again the listener gets called again resulting the view to be GONE upon appearing.
ViewPropertyAnimator.animate(enterGallery).alpha(1);
How can I clear the listener after the view visibility is set to GONE in the first piece of code?
I found the solution and it would be to pass null as listener when making the view VISIBLE.
ViewPropertyAnimator.animate(view).alpha(1).setListener(null);
I want to set Animation to images of in image view like in newsstand application and change image after particular interval of time.
Please help its very important for me.
for animating a view (image view included) i recommend this library
set the animation time and when it ends change the image, full code for the code snippet
rope = YoYo.with(Techniques.FadeIn).duration(1000).playOn(mTarget);// after start,just click mTarget view, rope is not init
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Techniques technique = (Techniques)view.getTag();
rope = YoYo.with(technique)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
//change image here
}
#Override
public void onAnimationCancel(Animator animation) {
Toast.makeText(MyActivity.this, "canceled", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animator animation) {
}
})
.playOn(mTarget);
}
});
So when I click a list item in my ListView it gets animated. The problem is that when it's animating I can still click it and this is undesirable. I'm using nineOldAndroids and to disable the ListItem I do:
set.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
v.setClickable(false);
v.setEnabled(false);
}
#Override
public void onAnimationEnd(Animator animator) {
v.setClickable(true);
v.setEnabled(true);
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
setClickable behaves strangely: it does let me click the list item for a brief time after the animation starts and after the animation ends it won't let me click it anymore (even though I set it to true). And setEnabled doesn't work at all.
How can I disable a list Item while an animation is running?
make the adapter isEnabled(int position) method return false while the animation on that item is going on.
add this to your adapter
Set<Integer> disabledItems;
private void setRowEnabled(int position,boolean enabled){
if(enabled){
disabledItems.remove(position);
}else{
disabledItems.add(position);
}
}
#Override
public boolean isEnabled(int position) {
return !disabledItems.contains(position);
}
And call the setRowEnabled from the onAnimationStart onAnimationEnd of your Animation