学习自项目: 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给大段文字分段