今天状态不好 感冒了 直接先看效果效果吧
布局
自定义SrollView
/**
* Created by Administrator on 2017/6/7.
*
* 悬浮 顶部的 scrollView
*/
public class SuspensionTopScrollView extends ScrollView {
private OnScrollListener onScrollListener;
boolean isAnimRun = false;
public SuspensionTopScrollView(Context context) {
this(context, null);
}
public SuspensionTopScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SuspensionTopScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 设置滚动接口
*
* @param onScrollListener
*/
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
@Override
public int computeVerticalScrollRange() {
return super.computeVerticalScrollRange();
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollListener != null) {
onScrollListener.onScroll(t);
}
}
public void smoothScrollTo(final int dis) {
// if (isAnimRun) {
// return;
// }
@SuppressLint("ObjectAnimatorBinding")
ObjectAnimator anim = ObjectAnimator.ofFloat(this, "ypx", 0.0f, 1.0f);
anim.setDuration(100);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
isAnimRun = true;
int mdis = (int) ((dis - getScrollY()) * (Float) animation.getAnimatedValue());
scrollBy(0, mdis);
}
});
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
isAnimRun = false;
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
anim.start();
}
/**
* 滚动的回调接口
*
* @author xiaanming
*/
public interface OnScrollListener {
/**
* 回调方法, 返回MyScrollView滑动的Y方向距离
*
* @param scrollY 、
*/
public void onScroll(int scrollY);
}
}
代码
/**
* 初始化头部悬停的 效果
*/
private void initTopSuspension() {
bindingView.scrollView.setOnScrollListener(this);
bindingView.rlContent.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
onScroll(bindingView.scrollView.getScrollY());
}
});
}
@Override
public void onScroll(int scrollY) {
// setTopVisibleOrGone(scrollY);
int top1 = bindingView.llSalaryContent.getTop() - bindingView.llTb.getHeight()
- bindingView.tbFactory.getHeight()
- bindingView.viewTop.getHeight();
int top2 = bindingView.llJobContent.getTop() - bindingView.llTb.getHeight()
- bindingView.tbFactory.getHeight()
- bindingView.viewTop.getHeight();
int top3 = bindingView.llEnterpriseContent.getTop() - bindingView.llTb.getHeight()
- bindingView.tbFactory.getHeight()
- bindingView.viewTop.getHeight();
int top4 = bindingView.llRecommendedContent.getTop() - bindingView.llTb.getHeight()
- bindingView.tbFactory.getHeight()
- bindingView.viewTop.getHeight();
if (scrollY > (top1 - 10)) {
bindingView.llTb.setVisibility(View.VISIBLE);
} else {
bindingView.llTb.setVisibility(View.GONE);
}
if (scrollY >= top1 && scrollY < top2) {
bindingView.llTb.setVisibility(View.VISIBLE);
initSelectTextView(bindingView.tvSalary, bindingView.viewSalary, bindingView.ivSalary);
} else if (scrollY >= top2 && scrollY < top3) {
bindingView.llTb.setVisibility(View.VISIBLE);
initSelectTextView(bindingView.tvJobs, bindingView.viewJobs, bindingView.ivJobs);
} else if (scrollY >= top3 && scrollY < top4) {
bindingView.llTb.setVisibility(View.VISIBLE);
initSelectTextView(bindingView.tvEnterprise, bindingView.viewEnterprise, bindingView.ivEnterprise);
} else if (scrollY >= top4) {
bindingView.llTb.setVisibility(View.VISIBLE);
initSelectTextView(bindingView.tvRecommended, bindingView.viewRecommended, bindingView.ivRecommended);
}
}
private void initSelectTextView(TextView tv, View view, ImageView iv) {
if (tv == null) {
return;
}
initFixationTextView();
tv.setTextColor(getResources().getColor(R.color.colorPrimary));
view.setVisibility(View.VISIBLE);
iv.setVisibility(View.VISIBLE);
}
private void initFixationTextView() {
bindingView.tvSalary.setTextColor(getResources().getColor(R.color.black));
bindingView.tvJobs.setTextColor(getResources().getColor(R.color.black));
bindingView.tvEnterprise.setTextColor(getResources().getColor(R.color.black));
bindingView.tvRecommended.setTextColor(getResources().getColor(R.color.black));
bindingView.viewSalary.setVisibility(View.INVISIBLE);
bindingView.viewJobs.setVisibility(View.INVISIBLE);
bindingView.viewEnterprise.setVisibility(View.INVISIBLE);
bindingView.viewRecommended.setVisibility(View.INVISIBLE);
bindingView.ivSalary.setVisibility(View.INVISIBLE);
bindingView.ivJobs.setVisibility(View.INVISIBLE);
bindingView.ivEnterprise.setVisibility(View.INVISIBLE);
bindingView.ivRecommended.setVisibility(View.INVISIBLE);
}
直接看代码吧 不想说话