首先来个效果图
布局文件代码
在布局文件中,CoordinatorLayout作为布局文件根节点,AppbarLayout包含在CoordinatorLayout中,toolbar在AppbarLayout节点下include进来。
<?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="cn.manchester.androIDsupportlibrary.CoordinatorLayoutActivity"> <androID.support.design.Widget.AppbarLayout androID:ID="@+ID/appbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:fitsSystemwindows="true"> <include layout="@layout/toolbar" /> </androID.support.design.Widget.AppbarLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:scrollbars="none" app:layout_behavior="@string/appbar_scrolling_vIEw_behavior"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclerVIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> </linearLayout></androID.support.design.Widget.CoordinatorLayout>
toolbar.xml
toolbar是需要响应vIEw的滚动事件的,所以需要为toolbar指定layout_scrollFlags属性,scroll表示toobar会随着vIEw的向下滚动而隐藏,enteralways表示toolbar会随着vIEw的向上滚动而恢复到原来的位置。
<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.Toolbar xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#304696" androID:popuptheme="@style/themeOverlay.AppCompat.light" app:layout_scrollFlags="scroll|enteralways" app:theme="@style/themeOverlay.AppCompat.Actionbar"></androID.support.v7.Widget.Toolbar>
Java代码
public class CoordinatorLayoutActivity extends AppCompatActivity { Toolbar mToolbar; RecyclerVIEw mRecyclerVIEw; String[] mArrays = new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14"}; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_coordinator_layout); mToolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(mToolbar); getSupportActionbar().setdisplayHomeAsUpEnabled(true); mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclerVIEw); mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(this)); mRecyclerVIEw.setAdapter(new MyAdapter()); } class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.MyVIEwHolder> { @OverrIDe public MyAdapter.MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { MyVIEwHolder holder = new MyVIEwHolder(LayoutInflater.from(CoordinatorLayoutActivity.this) .inflate(androID.R.layout.simple_List_item_1,parent,false)); return holder; } @OverrIDe public voID onBindVIEwHolder(MyAdapter.MyVIEwHolder holder,int position) { holder.tv.setText(mArrays[position]); } @OverrIDe public int getItemCount() { return mArrays.length; } class MyVIEwHolder extends RecyclerVIEw.VIEwHolder { TextVIEw tv; public MyVIEwHolder(VIEw itemVIEw) { super(itemVIEw); tv = (TextVIEw) itemVIEw.findVIEwByID(androID.R.ID.text1); } } }}
以上这篇AndroID Support library 标题栏(Toolbar)滚动效果实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android Support Library 标题栏(Toolbar)滚动效果实现方法全部内容,希望文章能够帮你解决Android Support Library 标题栏(Toolbar)滚动效果实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)