CoordinatorLayout+AppBarLayout的使用

来源:互联网 发布:网络公开课的好处 编辑:程序博客网 时间:2024/05/08 08:12

AppBarLayout主要实现上划下滑的时候隐藏控件并让一个控件悬浮,一些酷炫的写法见博客:http://blog.csdn.net/huachao1001/article/details/51558835
这是我在网上看见别人写的,我这里主要记录一下简单的应用。是一个图片+TabLaout,上划的时候隐藏图片,下拉的时候显示

代码如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.design.widget.AppBarLayout        android:id="@+id/appbar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:minHeight="?android:attr/actionBarSize"        android:background="@mipmap/dog"        >        <ImageView            android:layout_width="match_parent"            android:layout_height="150dp"            app:layout_scrollFlags="scroll"            />        <android.support.design.widget.TabLayout            android:id="@+id/tablayout"            android:layout_width="match_parent"            android:layout_height="wrap_content"            app:tabIndicatorColor="#f00"            app:tabSelectedTextColor="#f00"            app:tabTextColor="#000" />    </android.support.design.widget.AppBarLayout>    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/appbar"        app:layout_behavior="@string/appbar_scrolling_view_behavior"        /></android.support.design.widget.CoordinatorLayout>

下面是java代码的一部分:

 //tabLayout标题    private TabLayout tablayout;    //tabLayout的内容    private String[] titles={"仿listview","仿gridview","瀑布流"} @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        this.init(); //控件初始化        this.setDataToTabLayout();//向tablayout填充数据        this.setFragmentToViewpager();    //转填fragment        this.BindViewpager();//tablayout绑定viewpager    }    //控件初始化    private void init(){        viewPager= (ViewPager) findViewById(R.id.viewpager);        tablayout= (TabLayout) findViewById(R.id.tablayout);        appbar= (AppBarLayout) findViewById(R.id.appbar);    }    //向tablayout填充数据    private void setDataToTabLayout(){        Log.e("asd",tablayout+"");        Log.e("asd",titles+"");        for (int i = 0; i < titles.length; i++) {            // tablayout.addTab(tablayout.newTab().setText(titles[i]));        }    }    //转填fragment    private void setFragmentToViewpager(){       //略。。。。        MyFragmentAdapter adapter=new MyFragmentAdapter(getSupportFragmentManager(),list);        viewPager.setAdapter(adapter);    }    //tablayout绑定viewpager    private void BindViewpager(){        /**         * 执行setupWithViewPager后,tablayout还在,         * 但是数据会清空,所以要重新赋值         */        tablayout.setupWithViewPager(viewPager);        for (int i = 0; i < titles.length; i++) {            tablayout.getTabAt(i).setText(titles[i]);        }    }

注意:1、
scroll 所有想要滚动出屏幕的view都需要设置flags 没有设置flags的view都会固定在屏幕的顶部
例如:TabLayout没有设置该属性 滚动时会停留在屏幕顶部

  enterAlways 设置该falgs时 每次向下滚动该view变为可见,如果不设置,只有当下面的滚动布局,比如AppBarLayout下面是个listview,那么不设置这个flag,当listview向上滑的时候控件隐藏,只有当listview滑到头部的时候在向下滑控件才会出现,如果设置了此属性,每次listview向下滑的时候控件都会出现

注意2、TabLaout绑定viewpager的时候会清空里面的数据,需要
tablayout.getTabAt(0).setText(titles[0]);这样重新赋值

0 0
原创粉丝点击