您现在的位置是:首页 > 文章详情

Android标题栏随着滑动显示隐藏

日期:2018-08-14点击:392

这次实现标题栏随着上滑下滑显示隐藏

img_08c1af5a54b1074960d1a9a0871aeb5c.gif

实现这个效果需要,Support Design库中的CoordinatorLayout和AppBarLayout进行配合才行。

dependencies { ... implementation 'com.android.support:design:27.1.1' //必须添加 implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22' } 

xml布局代码

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" app:title="我是标题" app:titleTextColor="#fff" android:id="@+id/toolbar" app:layout_scrollFlags="scroll|enterAlways" //这个属性实现随着页面滚动标题栏(toolbar)显示隐藏 android:background="@color/colorPrimary" app:navigationIcon="@drawable/ic_back" android:layout_height="50dp"> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:id="@+id/recycle" app:layout_behavior="@string/appbar_scrolling_view_behavior"//这个属性只有在布局是CoordinatorLayout 时才有,是让让控件在AppBarLayout之下的 android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.design.widget.CoordinatorLayout> 

Activity代码

public class MainActivity extends AppCompatActivity { Toolbar toolbar; RecyclerView recyclerView; private BaseQuickAdapter<String, BaseViewHolder> baseQuickAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar=findViewById(R.id.toolbar); recyclerView=findViewById(R.id.recycle); setSupportActionBar(toolbar); recyclerView.setLayoutManager(new LinearLayoutManager(this)); final ArrayList<String> strings = new ArrayList<>(); for(int x=0;x<10;x++){ strings.add("我是条目"+x); } baseQuickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item, strings) { @Override protected void convert(BaseViewHolder helper, String item) { helper.setText(R.id.tv_title, item); } }; baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT); // baseQuickAdapter.isFirstOnly(false); baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { } }); recyclerView.setAdapter(baseQuickAdapter); } } 

还需要在主题中将标题去掉

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> //去掉ActionBar <item name="windowNoTitle">true</item> //去掉标题 </style> 

扩展一:将Toolbar换成ImageView和TabLayout实现图片显示隐藏

img_6415eab29fc8919c992035ea65f98c07.gif

xml布局代码

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="match_parent" android:src="@mipmap/bg" android:scaleType="fitXY" app:layout_scrollFlags="scroll|exitUntilCollapsed" //将layout_scrollFlags属性加在ImageView上 android:layout_height="200dp" /> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:background="#fff" app:tabMode="fixed" app:tabGravity="fill" android:layout_height="wrap_content"> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="分组一" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="分组二" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:text="分组三" android:layout_height="wrap_content" /> </android.support.design.widget.TabLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:id="@+id/recycle" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.design.widget.CoordinatorLayout> 
原文链接:https://yq.aliyun.com/articles/665559
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章