Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
Last active August 17, 2020 18:48
Show Gist options
  • Save yuriyskulskiy/9a11795edf06cfb89e311640faa64a0b to your computer and use it in GitHub Desktop.
Save yuriyskulskiy/9a11795edf06cfb89e311640faa64a0b to your computer and use it in GitHub Desktop.
AnimatedLayout part 4: implement CustomLinearLayout
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
RecyclerView.State state) {
ConstraintLayout parentLayout = findAnimatedLayout();
AnimatedLayout animatedLayout = null;
if (parentLayout != null) {
animatedLayout = parentLayout.findViewById(R.id.transformItem);
// todo add parallax in next article
}
if (needToRunAnimation(dy, animatedLayout)) {
animatedLayout.animateBy(dy);
return dy;
} else {
return super.scrollVerticallyBy(dy, recycler, state);
}
}
private ConstraintLayout findAnimatedLayout() {
return (ConstraintLayout) findViewByPosition(INSERT_POSITION);
}
private boolean needToRunAnimation(float dy, AnimatedLayout animatedLayout) {
if (animatedLayout == null) {
// view is out of the screen - position is not laid out
return false;
}
//todo merge nested if
if (dy > 0) {
if (animatedLayout.isForwardAnimationPossible()
&& isItemAboveCenterY((View) animatedLayout.getParent())) {
return true;
}
}
if (dy < 0) {
if (animatedLayout.isReverseAnimationPossible()
&& !isItemAboveCenterY((View) animatedLayout.getParent())) {
return true;
}
}
return false;
}
private boolean isItemAboveCenterY(View animatedItem) {
float centerItemY = computeItemCenterY(animatedItem);
float centerRecyclerViewY = computeRecyclerViewCenterY();
return centerItemY < centerRecyclerViewY;
}
private int computeItemCenterY(View animatedView) {
return (animatedView.getBottom() + animatedView.getTop()) / 2;
}
private int computeRecyclerViewCenterY() {
return getHeight() / 2;
}
boolean canScrollVerticallyFlag = true;
@Override
public boolean canScrollVertically() {
if (canScrollVerticallyFlag) {
return super.canScrollVertically();
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment