Created
November 12, 2015 00:53
-
-
Save NikolaDespotoski/d7882514d582e4a3a79c 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
package productmeister.com.productmeister.view; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import android.support.design.widget.AppBarLayout; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v7.app.ActionBar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
/** | |
* Created by Nikola D. on 11/11/2015. | |
*/ | |
public class ToolbarElevationOffsetListener implements AppBarLayout.OnOffsetChangedListener { | |
private final Toolbar mToolbar; | |
private float mTargetElevation; | |
private final AppCompatActivity mActivity; | |
public ToolbarElevationOffsetListener(AppCompatActivity appCompatActivity, Toolbar toolbar) { | |
mActivity = appCompatActivity; | |
mToolbar = toolbar; | |
mTargetElevation = mToolbar.getContext().getResources().getDimension(R.dimen.appbar_elevation); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) { | |
offset = Math.abs(offset); | |
mTargetElevation = Math.max(mTargetElevation, appBarLayout.getTargetElevation()); | |
if (offset >= appBarLayout.getTotalScrollRange() - mToolbar.getHeight()) { | |
float flexibleSpace = appBarLayout.getTotalScrollRange() - offset; | |
float ratio = 1 - (flexibleSpace / mToolbar.getHeight()); | |
float elevation = ratio * mTargetElevation; | |
setToolbarElevation(elevation); | |
} else { | |
setToolbarElevation(0); | |
} | |
} | |
private void setToolbarElevation(float targetElevation) { | |
ActionBar supportActionBar = mActivity.getSupportActionBar(); | |
if (supportActionBar != null) supportActionBar.setElevation(targetElevation); | |
else if (mToolbar != null) | |
ViewCompat.setElevation(mToolbar, targetElevation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment