效果图如下
主布局文件
Activity主要方法都有注释
private void initView() {
StatusBarUtil.setTranslucentForImageView(mActivity, 50, null);
//防止toolbar顶入状态栏
CollapsingToolbarLayout.LayoutParams lp2 = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
lp2.topMargin = PhoneUtil.getStatusBarHeight(mActivity);
toolbar.setLayoutParams(lp2);
myAppBarLayoutBehavoir = (NoScrollBehavior)
((CoordinatorLayout.LayoutParams) appBar.getLayoutParams()).getBehavior();
}
private void initListener() {
appBar.addOnOffsetChangedListener(this);
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//拿到当前fragment
currentFragment = (Demo5ItemFragment) list_fragment.get(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initTab() {
//初始化tab
list_title = new ArrayList<>();
list_title.add("军事");
list_title.add("娱乐");
list_title.add("萌宠");
list_title.add("生活");
list_id = new ArrayList<>();
list_id.add(1);
list_id.add(2);
list_id.add(3);
list_id.add(4);
list_fragment = new ArrayList<>();
for (int i = 0; i < list_id.size(); i++) {
Demo5ItemFragment orderItemFragment = Demo5ItemFragment.newInstance(list_id.get(i));
list_fragment.add(orderItemFragment);
}
currentFragment = (Demo5ItemFragment) list_fragment.get(0);
indicatorAdapter = new TabPageIndicatorAdapter(getSupportFragmentManager(), list_fragment, list_title);
viewpager.setAdapter(indicatorAdapter);
tabLayout.setupWithViewPager(viewpager);
}
@Override
public void onDestroy() {
super.onDestroy();
appBar.removeOnOffsetChangedListener(this);
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//顶部渐变 标题栏处理
float percent = Float.valueOf(Math.abs(verticalOffset)) / Float.valueOf(appBarLayout.getTotalScrollRange());
int alpha = (int) (255 * percent);
titleLayout.setBackgroundColor(Color.argb(alpha, 26, 128, 210));
tvTitle.setAlpha(alpha);
bgImg.setBackgroundColor(Color.argb(alpha, 26, 128, 210));
if (percent < 0.5) {
rlBack.setBackgroundResource(R.drawable.oval_shadow);
} else {
rlBack.setBackground(null);
}
//滑动事件处理
if (percent == 0) {
//当完全展开时 appbar可滑动 禁止refresh(可根据需求不禁止刷新)
myAppBarLayoutBehavoir.setNoScroll(false);
currentFragment.setRefreshState(false);
} else if (percent == 1) {
//当完全折叠时 appbar不可滑动使tab吸顶 允许refresh
currentFragment.setRefreshState(true);
myAppBarLayoutBehavoir.setNoScroll(true);
} else {
//滑动中 appbar可滑动 禁止refresh(建议禁止刷新,否则会appbar影响滑动流畅)
myAppBarLayoutBehavoir.setNoScroll(false);
currentFragment.setRefreshState(false);
}
}
@Override
public void onBackPressed() {
//返回监听 当appBar处于不可滑动(即完全折叠)时,先释放appBar
if (myAppBarLayoutBehavoir.isNoScroll()) {
myAppBarLayoutBehavoir.setNoScroll(false);
appBar.setExpanded(true, true);
} else {
super.onBackPressed();
}
}
@OnClick(R.id.rl_back)
public void onViewClicked() {
//返回监听 当appBar处于不可滑动(即完全折叠)时,先释放appBar
if (myAppBarLayoutBehavoir.isNoScroll()) {
myAppBarLayoutBehavoir.setNoScroll(false);
appBar.setExpanded(true, true);
} else {
mSwipeBackHelper.backward();
}
}
自定义behavior
/**
* created by dalang at 2018/10/23
* 自定义behavior 用于解决CoordinatorLayout布局将imgView顶入状态栏后 下面的content显示不全的问题
* 代替系统的@string/appbar_scrolling_view_behavior
*/
public class FixScrollingFooterBehavior extends AppBarLayout.ScrollingViewBehavior {
private AppBarLayout appBarLayout;
public FixScrollingFooterBehavior() {
super();
}
public FixScrollingFooterBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (appBarLayout == null) {
appBarLayout = (AppBarLayout) dependency;
}
final boolean result = super.onDependentViewChanged(parent, child, dependency);
final int bottomPadding = calculateBottomPadding(appBarLayout);
final boolean paddingChanged = bottomPadding != child.getPaddingBottom();
if (paddingChanged) {
child.setPadding(
child.getPaddingLeft(),
child.getPaddingTop(),
child.getPaddingRight(),
bottomPadding);
child.requestLayout();
}
return paddingChanged || result;
}
private int calculateBottomPadding(AppBarLayout dependency) {
final int totalScrollRange = dependency.getTotalScrollRange();
return totalScrollRange + dependency.getTop();
}
}
/**
* created by dalang at 2018/10/23
* 自定义behavior 用于控制appbar的滑动
* 当tabLayout吸顶后 使下滑不触发appBar的滑动 使下拉刷新更顺畅
*/
public class NoScrollBehavior extends AppBarLayout.Behavior{
public boolean noScroll;
public NoScrollBehavior() {
}
public NoScrollBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean isNoScroll() {
return noScroll;
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
if (noScroll) {
return false;
} else {
return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type);
}
}
}
(一)高斯模糊实现毛玻璃效果丶共享元素动画 丶地址选择器
(二)仿京东顶部伸缩渐变丶自定义viewpager指示器丶viewpager3D回廊丶recyclerview瀑布流
(三)RxJava2常用操作符merge、flatmap、zip--结合MVP架构讲解
(四)仿支付宝首页顶部伸缩滑动/中间层下拉刷新
(六)仿QQ首页drawer/侧滑删除/浮动imgaeView/角标拖拽
(七)仿微信发布朋友圈拖拽删除
将持续更新.. 不喜勿喷,仅个人分享,希望能帮助到你
源码地址:Github传送门