CollapsingToolbarLayout 滑动效果

fragment_store.xml



    

        

            

                

                    

                        


                        
                    
                

                

                    

                        

                    
                
            
        


        

    


 

StoreFragment.java
package com.zhoujian.mvpprojectdemo.amodule.fragment;


import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhoujian.mvpprojectdemo.amodule.R;

public class StoreFragment extends Fragment {


    private AppBarLayout mAppBarLayout;
    private RelativeLayout mContent;
    private TextView mTitle;
    private TextView mDesc;
    private TextView mCommonTitle;
    private float mToolbarHeight = 60;
    private float mTitleHeight = 0;
    private float mContentHeight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_store, container, false);
        initView(view);


        return view;
    }

    private void initView(View view) {
        mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appbar);

        mContent = (RelativeLayout) view.findViewById(R.id.content);

        mTitle = (TextView) view.findViewById(R.id.tv_title);
        mCommonTitle = (TextView) view.findViewById(R.id.common_title);
        mDesc = (TextView) view.findViewById(R.id.tv_desc);


        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (mTitleHeight == 0) {
                    mContentHeight = mContent.getHeight();
                    mTitleHeight = mTitle.getHeight();
                }

                float alpha = 1 - (-verticalOffset) / (mToolbarHeight + mContentHeight);
                mDesc.setAlpha(alpha);
                mTitle.setAlpha(alpha);

                mCommonTitle.setAlpha(1 - alpha);

            }
        });


    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}

 

你可能感兴趣的:(Android5.0)