Skip to content

Instantly share code, notes, and snippets.

@DariusL
Last active August 29, 2015 14:14
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 DariusL/1f311acf1fb922fce77a to your computer and use it in GitHub Desktop.
Save DariusL/1f311acf1fb922fce77a to your computer and use it in GitHub Desktop.
Bits of android
/**
* remeasures view with the same width and returns it's height, the view has to be laid out before calling this
* @param view the view
* @return measured height
*/
public static int measureViewHeight(View view){
view.measure(
View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
return view.getMeasuredHeight();
}
private Rect getViewBounds(View bounds){
Rect ret = new Rect();
int[] location = new int[2];
bounds.getLocationInWindow(location);
ret.set(location[0], location[1], location[0] + bounds.getWidth(), location[1] + bounds.getHeight());
return ret;
}
/**
* Finds the scroll position for this recycler
* @param view obvious
* @return index of the first visible item
*/
public static int getScrollPosition(RecyclerView view){
View childAt = view.getLayoutManager().getChildAt(0);
if(childAt != null){
return view.getLayoutManager().getPosition(childAt);
}else{
return 0;
}
}
//scroll toolbar with recyclerview
list.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
float translation = toolbar.getTranslationY();
translation -= dy;
if(translation > 0)
translation = 0.0f;
else if(translation < -toolbar.getHeight())
translation = -toolbar.getHeight();
toolbar.setTranslationY(translation);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment