Skip to content

Instantly share code, notes, and snippets.

@aqua30
Last active September 20, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aqua30/18de9dd71a7e4afdee01cec3b052d84b to your computer and use it in GitHub Desktop.
Save aqua30/18de9dd71a7e4afdee01cec3b052d84b to your computer and use it in GitHub Desktop.
/**
* Created by Saurabh(aqua) in 2017.
*/
public class HideShowRecyclerParallaxActivity extends BaseActivity {
/* view binding */
@BindView(R.id.recycler_view)RecyclerView recyclerView;
@BindView(R.id.parallax_image)ImageView parallaxImage;
@BindView(R.id.tv_heading)AppTextView heading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* hide the heading initially */
heading.setAlpha(0f);
ParallaxAdapter adapter = new ParallaxAdapter();
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
/* calcaulate the range we need to cover */
int maxDistance = recyclerView.getPaddingTop();
/* calculate the amount of scroll */
int movement = maxDistance - recyclerView.getChildAt(0).getTop();
/* calculate the alpha value for heading based on scrolling */
float alphaFactor = ((movement * 1.0f) / (maxDistance - heading.getHeight()));
if (movement >= 0 && movement <= maxDistance) {
/* parallaxing the image */
parallaxImage.setTranslationY(-movement/2);
/* hide/show the heading based on alpha */
heading.setAlpha(alphaFactor);
} else {
parallaxImage.setTranslationY(-maxDistance);
}
}
});
}
@Override
protected int getActivityLayout() {
return R.layout.ac_parallax;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment