I have a fragment lets call it EventFragment
, and I have an RecyclerView.Adapter
called EventAdapter
, inside that adapter is a viewholder class called EventViewHolder
. I want access an animation method inside the viewholder class from the Fragment
.
How would I accomplish this? I was thinking to define another interface to accomplish this.
This is what I have so far:
OnAnimationListener
public interface OnAnimationListener {
void onAnimation();
}
Adapter
public EventAdapter extends RecyclerView.Adapter<RecycleView.ViewHolder> {
// Boilerplate initialization stuff here
public class EventViewHolder extends RecyclerView.ViewHolder implements OnAnimationListener {
// Initialization code
@Override
public void onAnimation() {
// Do an animation
}
}
}
EventFragment
public class EventFragment extends Fragment {
// Boilerplate initialization code
}
I am thinking I should implement an interface inside the Fragment
like so:
public class EventFragment extends Fragment implements onAnimationListener {
private void initAdapter() {
mAdapter.setOnAnimationListener(this);
}
@Override
public void onAnimation(Data data) {
// pass any data
}
}
Then I'm sort of stuck at this point. Normally you would call this once the listener is passed through:
OnAnimationListener.onAnimation(...)
But this doesn't make sense. The code flow goes like this:
EventFragment --> Adapter --> ViewHolder
I need to implement the following code flow:
EventFragment (get access to specific viewholder and do animation) <--> Viewholder
How should I accomplish this? Maybe pass the interface from the Viewholder to the Fragment instead, and call mOnAnimationListener.onAnimation()
from the fragment right?
I want to have specific control of when the animation occurs on the EventViewHolder
and I want to have this control from the EventFragment
how should I go about tackling this?
If you simply want to animate the RecyclerView
's items you can take a look at RecyclerView.ItemAnimator
.
If not (or if you still want to handle this in the fragment) you can attach click listeners for your views inside the ViewHolder
object and handle the clicks by passing the event up ViewHolder->Adapter->Fragment
via the mechanism you already have (and passing the view received in onClick
as a parameter). Personally I'd stay away from this pattern. One reason being that the RecyclerView
can be scrolled in the meantime and I don't have enough knowledge right now on what happens to that particular view once it's off position or maybe even off screen. Or maybe pointing to other data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.