Last active
September 20, 2017 09:09
-
-
Save aqua30/18de9dd71a7e4afdee01cec3b052d84b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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