美文网首页Android UI控件
CoordinatorLayout AppBarLayout 标

CoordinatorLayout AppBarLayout 标

作者: 小尤学习 | 来源:发表于2017-09-16 22:50 被阅读0次

    先直接上图看效果:

    WeChat_20170916214217_20170916214334.gif

    项目需求:

    滑动的时候标题渐变颜色,并且左右两边的图片渐变切换。并且Toolbar能够悬浮置顶,查阅了一些资料有用ScrollView实现 有用CoordinatorLayout +AppBarLayout +CollapsingToolbarLayout +Toolbar 实现,没错我用的就是后者,其实实现了也就是这么回事,蛮简单的。

    所以写下博客,一个是帮助其他小伙伴,另外一个是记录下自己的菜鸟成长之路吧。也决定之后有空了,把自己过去在项目中遇到的问题都记录下来,做成笔记。

    实现原理:

    为什么要用CoordinatorLayout CollapsingToolbarLayout是用来对Toolbar进行再次包装的ViewGroup,主要是用于实现折叠(其实就是看起来像伸缩~)的App Bar效果。它需要放在AppBarLayout布局里面,并且作为AppBarLayout的直接子View。就不做过多解释了不懂的可以看下这边文章 玩转AppBarLayout更酷炫的顶部栏

    贴上代码:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
    
            <variable
                name="ShopDetailsAcitvity"
                type="com.example.youjia.shopping.MainActivity" />
        </data>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
    
                <android.support.design.widget.AppBarLayout
                    android:id="@+id/app_bar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <android.support.design.widget.CollapsingToolbarLayout
                        android:id="@+id/toolbar_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#f7f7f7"
                        app:contentScrim="?attr/colorPrimary"
                        app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
    
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/view"
                            android:orientation="vertical">
    
    
                            <RelativeLayout
                                android:id="@+id/banner_layout"
                                android:layout_width="match_parent"
                                android:layout_height="264dp"
                                android:orientation="vertical">
    
                                <com.youth.banner.Banner
                                    android:id="@+id/banner"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    app:image_scale_type="center_crop"
                                    app:indicator_drawable_selected="@mipmap/banner_red"
                                    app:indicator_drawable_unselected="@mipmap/banner_grey"
                                    app:indicator_height="9dp"
                                    app:indicator_margin="3dp"
                                    app:indicator_width="9dp"
                                    app:is_auto_play="true" />
    
                                <View
                                    style="@style/View"
                                    android:layout_alignParentBottom="true" />
                            </RelativeLayout>
    
                            <RelativeLayout
                                android:id="@+id/relativelayout_goods"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/banner_layout"
                                android:background="@color/white"
                                android:gravity="center_vertical"
                                android:paddingBottom="12dp"
                                android:paddingLeft="13.3dp"
                                android:paddingRight="13.3dp"
                                android:paddingTop="12dp">
    
                                <TextView
                                    android:text="禄福来精品翡翠"
                                    android:id="@+id/shop_details_1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:lineSpacingExtra="5dp"
                                    android:textColor="#282828"
                                    android:textSize="16sp" />
    
                                <TextView
                                    android:text="¥399.00"
                                    android:id="@+id/shop_details_2"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/shop_details_1"
                                    android:layout_marginTop="10dp"
                                    android:textColor="#e75446"
                                    android:textSize="16sp" />
    
                                <TextView
                                    android:text="原价¥500.00"
                                    android:id="@+id/shop_details_3"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/shop_details_2"
                                    android:layout_marginTop="10dp"
                                    android:textColor="#999999"
                                    android:textSize="12sp" />
                                <!--android:layout_alignBottom="@+id/shop_details_2"-->
                                <TextView
                                    android:text="月销8238笔"
                                    android:id="@+id/sumSotre"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentBottom="true"
                                    android:layout_alignParentRight="true"
                                    android:textColor="#656565"
                                    android:textSize="14sp" />
    
    
                            </RelativeLayout>
    
    
    
                            <LinearLayout
                                android:id="@+id/linearLayout_goods"
                                android:layout_width="match_parent"
                                android:layout_height="46.3dp"
                                android:layout_alignParentStart="true"
                                android:layout_below="@+id/relativelayout_goods"
                                android:layout_marginTop="19dp"
                                android:background="@drawable/ripple_item_white_bg"
                                android:gravity="center_vertical"
                                android:orientation="vertical">
    
                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="13dp"
                                    android:layout_weight="1"
                                    android:gravity="center_vertical"
                                    android:text="选择:商品规格"
                                    android:textColor="#282828"
                                    android:textSize="15.3sp" />
    
                                <View style="@style/View" />
    
                            </LinearLayout>
    
    
                        </RelativeLayout>
                        <android.support.v7.widget.Toolbar
                            android:id="@+id/toolbaretail"
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            app:layout_collapseMode="pin"
                            android:fitsSystemWindows="true"
                            android:paddingTop="15dp"
                            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" >
    
                            <ImageView
                                android:src="@mipmap/share_shop"
                                android:id="@+id/share_img"
                                android:layout_gravity="center|right"
                                android:layout_marginRight="15dp"
                                android:layout_marginTop="5dp"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent" />
                        </android.support.v7.widget.Toolbar>
                    </android.support.design.widget.CollapsingToolbarLayout>
    
                    <android.support.design.widget.TabLayout
                        android:id="@+id/infoPhoneTabLayou"
                        app:tabTextColor="#989898"
                        app:tabSelectedTextColor="#e91b04"
                        app:tabIndicatorColor="@color/tablayout"
                        app:tabBackground="@drawable/ripple_tab_bg"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"  >
    
                    </android.support.design.widget.TabLayout>
    
                    <View
                        android:background="@color/view"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"/>
                </android.support.design.widget.AppBarLayout>
    
    
                <android.support.v4.widget.NestedScrollView
                    android:id="@+id/NestedScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:overScrollMode="always"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    
    
                        <com.example.youjia.shopping.WrapContentHeightViewPager
                            android:id="@+id/infoPhone_Fragment_pager"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            />
    
    
                </android.support.v4.widget.NestedScrollView>
    
    
            </android.support.design.widget.CoordinatorLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="@color/white"
                android:gravity="center_vertical"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.2"
                    android:background="@drawable/ripple_item_white_bg"
                    android:drawablePadding="2dp"
                    android:drawableTop="@mipmap/shop_details_3"
                    android:gravity="center_horizontal"
                    android:text="店铺"
                    android:textColor="#656565"
                    android:textSize="10sp" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.2"
                    android:background="@drawable/ripple_item_white_bg"
                    android:drawablePadding="2dp"
                    android:drawableTop="@mipmap/shop_details_4"
                    android:gravity="center_horizontal"
                    android:text="进货单"
                    android:textColor="#656565"
                    android:textSize="10sp" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:background="#ff6554"
                    android:drawablePadding="5dp"
                    android:gravity="center"
                    android:text="加入进货单"
                    android:textColor="#ffffff" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:background="#ff334c"
                    android:drawablePadding="5dp"
                    android:gravity="center"
                    android:text="立即采购"
                    android:textColor="#ffffff" />
            </LinearLayout>
        </LinearLayout>
    </layout>
    
    
    
    
    package com.example.youjia.shopping;
    
    import android.databinding.DataBindingUtil;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.support.design.widget.AppBarLayout;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    
    
    import com.example.youjia.shopping.databinding.ActivityMainBinding;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class MainActivity extends AppCompatActivity {
        private ActivityMainBinding mBinding;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             mBinding  = DataBindingUtil.setContentView(this,R.layout.activity_main);
            mBinding.setShopDetailsAcitvity(this);
            mBinding.shopDetails3.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
            TabLayout();
            initBanner();
            setToolBar();
        }
    
        /**
         * 初始化轮播图
         */
        private void initBanner() {
            List<Integer> integers = new ArrayList<>();
            integers.add(R.drawable.img1);
            integers.add(R.drawable.img2);
            integers.add(R.drawable.img3);
    //        integers.add(R.drawable.img4);
    //        integers.add(R.drawable.img5);
    //        integers.add(R.drawable.img6);
    //        integers.add(R.drawable.img7);
    
            mBinding.banner.startAutoPlay();
            mBinding.banner.setDelayTime(2000);
            mBinding.banner.setImages(integers).setImageLoader(new GlideImageLoader()).start();
        }
    
        /**
         * 初始化setToolBar
         */
        private void setToolBar(){
            setSupportActionBar(mBinding.toolbaretail);
            mBinding.toolbaretail.setTitleTextColor(Color.WHITE);
            mBinding.toolbarLayout.setTitleEnabled(false);
            mBinding.toolbarLayout.setExpandedTitleGravity(Gravity.CENTER);//设置展开后标题的位置
            mBinding.toolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);//设置收缩后标题的位置
            mBinding.toolbarLayout.setExpandedTitleColor(Color.WHITE);//设置展开后标题的颜色
            mBinding.toolbarLayout.setCollapsedTitleTextColor(Color.WHITE);//设置收缩后标题的颜色
            mBinding.appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    //verticalOffset  当前偏移量 appBarLayout.getTotalScrollRange() 最大高度 便宜值
                    int Offset = Math.abs(verticalOffset); //目的是将负数转换为绝对正数;
                    //标题栏的渐变
                    mBinding.toolbaretail.setBackgroundColor(changeAlpha(getResources().getColor(R.color.redcustom)
                            , Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange()));
    
                    /**
                     * 当前最大高度便宜值除以2 在减去已偏移值 获取浮动 先显示在隐藏
                     */
                    if (Offset < appBarLayout.getTotalScrollRange() / 2) {
                        mBinding.toolbaretail.setTitle("");
                        mBinding.toolbaretail.setAlpha((appBarLayout.getTotalScrollRange() / 2 - Offset * 1.0f) / (appBarLayout.getTotalScrollRange() / 2));
                        mBinding.shareImg.setAlpha((appBarLayout.getTotalScrollRange() / 2 - Offset * 1.0f) / (appBarLayout.getTotalScrollRange() / 2));
                        mBinding.shareImg.setImageDrawable(getResources().getDrawable(R.mipmap.share_shop));
                        mBinding.toolbaretail.setNavigationIcon(R.mipmap.shop_details_2);
                        /**
                         * 从最低浮动开始渐显 当前 Offset就是  appBarLayout.getTotalScrollRange() / 2
                         * 所以 Offset - appBarLayout.getTotalScrollRange() / 2
                         */
                    } else if (Offset > appBarLayout.getTotalScrollRange() / 2) {
                        float floate = (Offset - appBarLayout.getTotalScrollRange() / 2) * 1.0f / (appBarLayout.getTotalScrollRange() / 2);
                        mBinding.toolbaretail.setAlpha(floate);
                        mBinding.shareImg.setAlpha(floate);
                        mBinding.toolbaretail.setNavigationIcon(R.mipmap.image_left);
                        mBinding.shareImg.setImageDrawable(getResources().getDrawable(R.mipmap.img_share));
                        mBinding.toolbaretail.setTitle("禄福来精品翡翠");
                        mBinding.toolbaretail.setAlpha(floate);
                    }
                }
            });
        }
    
        /**
         * 根据百分比改变颜色透明度
         */
        public int changeAlpha(int color, float fraction) {
            int alpha = (int) (Color.alpha(color) * fraction);
            return Color.argb(alpha, 0, 128, 0);
        }
    
        /**TabLayout
         * 初始化
         */
        private void TabLayout(){
            List<String>list_Title = new ArrayList<>();
            list_Title.add("图文详情");
            list_Title.add("规格参数");
    
            List<Fragment> fragmentList = new ArrayList<>();
            fragmentList.add(new Fragment1());
            fragmentList.add(new Fragment2());
    
            mBinding.infoPhoneFragmentPager.setAdapter(new HomeFragmentPageAdapter(getSupportFragmentManager(),fragmentList,list_Title,2));
            mBinding.infoPhoneTabLayou.setTabMode(TabLayout.MODE_FIXED);
            mBinding.infoPhoneTabLayou.setupWithViewPager(mBinding.infoPhoneFragmentPager);
        }
    }
    

    渐变核心实现AppBarLayout.addOnOffsetChangedListener监听
    通过verticalOffset 拿到当前的当前偏移量,appBarLayout.getTotalScrollRange() 是最大高度 偏移值 有了这两个参数我们就可以计算 浮度

    是不是感觉很简单呢到此就结束了,在此奉上源码点击下载
    有更好的方式或者需要改进的地方请给我留言,大家共同学习进步。
    图片是一个做微商朋友提供的,要是需要玉的话可以给我留言。就当给他推广推广啦

    相关文章

      网友评论

        本文标题:CoordinatorLayout AppBarLayout 标

        本文链接:https://www.haomeiwen.com/subject/iivusxtx.html