Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Position? #12

Closed
erfannf opened this issue Mar 4, 2018 · 1 comment
Closed

How to get Position? #12

erfannf opened this issue Mar 4, 2018 · 1 comment
Labels

Comments

@erfannf
Copy link

erfannf commented Mar 4, 2018

For animating items in ViewBinder method, I need to check if RecyclerView is scrolling up or down. In custom adapters, I usually add a global variable LASTPOSITION and compare it to current position for detecting up or down scroll for proper animation. How can we do so in RendererRecyclerViewAdapter? Also, how can access adapterPosition or position in ViewBinder?

FYI:

  @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
           Animation animation = AnimationUtils.loadAnimation(mActivity, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
         viewHolder.itemView.startAnimation(animation);
         lastPosition = position;
}
@vivchar
Copy link
Owner

vivchar commented Mar 5, 2018

You should extend ViewBinder and put new argument to constructor:

public class AnimatedViewBinder <M extends ViewModel> extends ViewBinder<M> {

	@NonNull
	private final Delegate mDelegate;

	public AnimatedViewBinder(final int layoutID,
	                         @NonNull final Class<M> type,
	                         @NonNull final Delegate delegate,
	                         @NonNull final Binder<M> binder) {
		super(layoutID, type, binder);
 		mDelegate = delegate;
	}

	@Override
	public void bindView(@NonNull final M model, @NonNull final ViewHolder viewHolder) {
		final boolean isUp = viewHolder.getAdapterPosition() > mDelegate.getLastPosition(); 
		Animation animation = AnimationUtils.loadAnimation(getContext(), isUp ? R.anim.up_from_bottom : R.anim.down_from_top);
		viewHolder.itemView.startAnimation(animation);

		mDelegate.setPosition(viewHolder.getAdapterPosition());
		super.bindView(model, viewHolder);
	}

	public interface Delegate {
		int getLastPosition();
		void setPosition(int position);
	}
}

Usages:

rendererDelegate = new AnimatedViewBinder.Delegate() { /* implementation */ }

adapter.registerRenderer(new AnimatedViewBinder(
	R.layout.your_layout,
	YourModel.class,
	rendererDelegate,
	(model, finder, payload) -> { /* ... */ }
));
adapter.registerRenderer(new AnimatedViewBinder(
	R.layout.your_layout2,
	YourModel2.class,
	rendererDelegate,
	(model, finder, payload) -> { /* ... */ }
));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants