使用cardview将我的代码从listview迁移到Recycler视图。我在textview上应用了闪烁效果。一旦我向下滚动并向上移动,Textview就会停止闪烁。 在onBindViewHolder中应用动画 回收适配器:
@Override
public void onBindViewHolder(final TaskViewHolder holder, final int position) {
final TaskDetails item = list_items.get(position);
if (item.getStatus().equals(Constants.TASK_ACCEPTED)) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.VISIBLE);
holder.onsiteButton.setVisibility(View.GONE);
} else if (item.getStatus().equals(Constants.TASK_ENROUTE_Failed) ||
item.getStatus().equals(Constants.TASK_ONSITE)) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.onsiteButton.setVisibility(View.VISIBLE);
} else if (item.getStatus().equals(Constants.TASK_ONSITE_Failed) ||
item.getStatus().equals(Constants.TASK_COMPLETED)) {
if (item.getFormList() != null && item.getFormList().size() == 0) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.onsiteButton.setVisibility(View.VISIBLE);
}
} else {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.acceptButton.setVisibility(View.VISIBLE);
}
if (item.getStatus().equals(Constants.TASK_ACCEPTED)) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.VISIBLE);
holder.onsiteButton.setVisibility(View.GONE);
holder.completeButton.setVisibility(View.GONE);
} else if (item.getStatus().equals(Constants.TASK_ENROUTE) || item.getStatus().equals(Constants.TASK_ENROUTE_Failed)) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.onsiteButton.setVisibility(View.VISIBLE);
holder.completeButton.setVisibility(View.GONE);
} else if (item.getStatus().equals(Constants.TASK_ONSITE) || item.getStatus().equals(Constants.TASK_ONSITE_Failed)) {
holder.acceptButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.onsiteButton.setVisibility(View.GONE);
if (item.getFormList() != null && item.getFormList().size() == 0) {
holder.completeButton.setVisibility(View.VISIBLE);
}
} else {
holder.acceptButton.setVisibility(View.VISIBLE);
holder.onsiteButton.setVisibility(View.GONE);
holder.enrouteButton.setVisibility(View.GONE);
holder.completeButton.setVisibility(View.GONE);
}
holder.task_subject_name.setText(item.getTask_subject_name());
holder.task_detail.setText(item.getDetails());
holder.task_location.setText(item.getAddress());
holder.task_id.setText(item.getTask_id());
holder.imageViewInfo.setTag(holder);
holder.imageViewTaskInstruction.setTag(holder);
holder.task_distance.setText(item.getTask_distance());
String priority = item.getTask_priority();
if (priority.equalsIgnoreCase(Constants.JSON_VALUE_PRIORITY_HIGH)) {
holder.list_indicator.setBackgroundResource(R.color.high_priority);
Animation anim = new AlphaAnimation(0.3f, 1.0f);
anim.setDuration(200); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
holder.list_indicator.startAnimation(anim);
} else if (priority.equalsIgnoreCase(Constants.JSON_VALUE_PRIORITY_NORMAL)) {
holder.list_indicator.setBackgroundResource(R.color.normal_priority);
} else if (priority.equalsIgnoreCase(Constants.JSON_VALUE_PRIORITY_LOW)) {
holder.list_indicator.setBackgroundResource(R.color.low_priority);
}
holder.acceptButton.setTag(holder);
答案 0 :(得分:0)
您可以在ViewHolder类的onBind方法中为视图设置动画。
public class YourHolder extends RecyclerView.ViewHolder{
private View list_indicator;
private ObjectAnimator anim;
public YourHolder(View itemView) {
super(itemView);
list_indicator = itemView.findViewById(R.id.list_indicator);
anim = ObjectAnimator.ofFloat(list_indicator, "alpha", 0.3f, 1f);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(200);
}
public void bind(DataObject dataObject){
anim.start();
}
}
Recycler adapter
@Override
public YourHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mLayoutInflater.inflate(R.layout.my_view, parent, false);
YourHolder yourholder = new YourHolder(v);
return yourholder;
}
@Override
public void onBindViewHolder(YourHolder holder, int position) {
DataObject dataObject = mDataObjects.get(position);
holder.bind(dataObject);
}