我是android studio的新手,正在尝试制作一个recyclerView跌倒动画,但是我目前尚不清楚何时设置layoutAnimation。我正在使用Firebase回收器适配器。以下是我的OnViewCreated函数(public class EventsFragment extends Fragment
):
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final EditText inputSearch = (EditText) view.findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
// Implementing Search
}
ProductsRef = FirebaseDatabase.getInstance().getReference().child("Events");
recyclerView = view.findViewById(R.id.event_recycler_menu);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
以下是OnStart
函数:
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Events> options =
new FirebaseRecyclerOptions.Builder<Events>().setQuery(ProductsRef , Events.class)
.build();
FirebaseRecyclerAdapter<Events, EventViewHolder> adapter =
new FirebaseRecyclerAdapter<Events, EventViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull EventViewHolder holder, int position, @NonNull final Events model) {
holder.event_desc.setText(model.getDescription());
holder.event_loc.setText("Location :"+model.getLocation());
holder.event_time.setText("Time : "+model.getTime());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),EventDetailActivity.class);
intent.putExtra("eid",model.getEid());
startActivity(intent);
}
});
}
@NonNull
@Override
public EventViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.event_user_layout, viewGroup,false);
EventViewHolder holder = new EventViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
runAnimation(recyclerView);
}
runAnimation
函数如下:
private void runAnimation(RecyclerView recyclerView) {
final Context context = recyclerView.getContext();
final LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context,R.anim.layout_falldown);
recyclerView.setLayoutAnimation(controller);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
}
设置适配器后,我试图调用runAnimation
,但是动画根本不起作用。以下是我的 item_animation_fall_down.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/animation_duration_medium">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator"/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"/>
</set>
layout_falldown.xml :
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/item_animation_fall_down"
android:delay="15%"
android:animationOrder="normal">
</layoutAnimation>
请告诉我如何运行动画。运行动画的位置以及原因。