android 收缩导航栏,Android_实现可收缩扩展的导航栏 CoordinatorLayout+AppbarLayout+NestedScrollView...

学习自项目: https://github.com/nanchen2251/AiYaGirl

通过上滑把背景图片隐藏起来

看一看

导航栏扩展状态         上滑后导航栏收缩状态

实现思路

要实现这样的效果, 需要用到:CoordinatorLayout和AppbarLayout的配合, 以及实现了NestedScrollView的布局或控。

AppbarLayout是一种支持响应滚动手势的app bar布局,

CollapsingToolbarLayout则是专门用来实现子布局内不同元素响应滚动细节的布局。

与AppbarLayout组合的滚动布局(RecyclerView, NestedScrollView等),需要设置 app:layout_behavior = "@string/appbar_scrolling_view_behavior" 。没有设置的话, AppbarLayout将不会响应滚动布局的滚动事件。

这个布局行为Layout_Behavior:`app:layout_behavior="@string/appbar_scrolling_view_behavior"`

等同于`app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"`

这是一个系统behavior,是为appbar设置滚动动作的一个behavior。 没有这个属性的话,Appbar就是死的,有了它就有了灵魂。

主布局 layout\activity_nav_home.xml

主页面代码 NavHomeActivity.java

public class NavHomeActivity extendsAppCompatActivity {

@BindView(R.id.nav_home_toolbar)

Toolbar mToolBar;

@BindView(R.id.nav_home_fab)

FloatingActionButton mFab;

@Overrideprotected voidonCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_nav_home);

ButterKnife.bind(this);

initView();

}private voidinitView() {//背景与状态栏融为一体(StatusBarUtil类在'轮播图片Banner有介绍')

StatusBarUtil.setTranslucentForImageView(this, 0, mToolBar);//导航栏设置

setSupportActionBar(mToolBar);

getSupportActionBar().setHomeButtonEnabled(true);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);//导航栏的导航按钮:返回

mToolBar.setNavigationOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

finish();

}

});

}//分享按钮

@OnClick(R.id.nav_home_fab)public voidonViewClicked() {

ShareUtil.share(this, R.string.string_share_text);

}

}

分享工具类 ShareUtil.java

importandroid.content.Context;importandroid.content.Intent;importandroid.net.Uri;importcom.zss.pp.R;/*** 专用于分享的工具类*/

public classShareUtil {public static void share(Context context, intstringRes) {

share(context, context.getString(stringRes));

}public static voidshareImage(Context context, Uri uri, String title) {

Intent shareIntent= newIntent();

shareIntent.setAction(Intent.ACTION_SEND);

shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

shareIntent.setType("image/jpeg");

context.startActivity(Intent.createChooser(shareIntent, title));

}public static voidshare(Context context, String extraText) {

Intent intent= newIntent(Intent.ACTION_SEND);

intent.setType("text/plain");

intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.action_share));

intent.putExtra(Intent.EXTRA_TEXT, extraText);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(Intent.createChooser(intent, context.getString(R.string.action_share)));

}

}

所需资源

分享图标 actionbar_share

返回图标 icon_back

显示的文字

分享

分享一下费曼的思维:https://www.sohu.com/a/197127933_464074[分享自爱小酥酥]

省略……通过\n\n给大段文字分段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值