滚动到视图android时,项目动画在recyclerview中停止
我有一个Recyclerview,在单个列表项中设置了一个动画视图,但是当我滚动recyclerview时,动画正在停止。这是因为recyclerview会从视图中删除项目,因此当我们向后滚动时,它会将其取回!但是现在我希望该动画继续进行,因为只有从服务器获取数据时我才停止动画!
I have a Recyclerview, im animating a view inside individual list item, but when I scroll the recyclerview the animation is stopping. Its because recyclerview removes the items form its view so when we scroll back it fetches it back! But now i want that animation to keep going as I would stop it only when i get data from server!
我想要的是即使在recyclerview滚动并且视图失焦并且出现时,我也不会停止在recylerview内部的单个项目中开始的动画回到焦点!获取服务器数据时,我需要停止代码中的动画!我有在哪里停止动画的代码,如果该项目未从视图中滚动,则它可以工作!
All I want is the animation that I start in the individual items inside the recylerview shouldn't stop even if the recyclerview is scrolled and the view is out of focus and comes back to focus! I need to stop the animation in the code when I get the server data! I have the code where to stop the animation and it works if the item is not scrolled off the view!
btn.onClick-这按钮是recyclerview列表的onClick。
项目1 btn.startAnimation(anim.xml)-开始动画
btn.onClick -- this button is the onClick for the recyclerview list item 1 btn.startAnimation(anim.xml) -- starting the animation
onSuccess-服务器返回成功btn。 clearAnimation();
onSuccess -- server returns success btn.clearAnimation();
但在onSuccess之前,如果我们滚动列表,动画将停止!
but before the onSuccess if we scroll the list the animation is stopped!
请帮助!
从crymson的答案中得到启发,我使用View的标记方法代替了在复杂逻辑中设置boolean的方法,几乎没有任何简单而有用的解决方案
By inspiring from crymson's answer i have made little easy and useful solution using tag method of View instead setting a boolean in complicated logic of your custom adapter.
@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if (holder.getItemViewType() == TYPE_AD)
((ViewHolderForAd) holder).ivStory.setTag(false);
}
public class ViewHolderForAd extends RecyclerView.ViewHolder {
private ImageView ivStory;
TextView tvName;
public ViewHolderForAd(View view) {
super(view);
ivStory = (ImageView) view.findViewById(R.id.ivStoryImage);
tvName = (TextView) view.findViewById(R.id.tvAppName);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos < 0) {
pos = (int) v.getTag();
}
customItemClickListener.onItemClicked(v, pos);
}
});
//ivStory.startAnimation(AnimationUtils.loadAnimation(context, R.anim.pulse_story));
ivStory.setTag(false); //Set default tag false to decrease risk of null
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
//...Your code...
if (!(boolean) holder1.ivStory.getTag()) {
holder1.ivStory.setTag(true);
holder1.ivStory.startAnimation(AnimationUtils.loadAnimation(context, R.anim.pulse_story));
}
//...Your code...//
}
如果您已经在imageView中标记了某些东西(例如位置),则可以使用setTag(key,object)代替setTag(object)。
希望这对某人有帮助。
You can use setTag(key, object) instead of setTag(object) if you already tagged something(like position) in your imageView. Hope this helps someone.