Android视差效果

这次带来的是结合CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout和FloatingActionButton打造炫酷的视觉差效果。

实现步骤

一:需要在主题文件中将Activity的标题栏去掉

Android视差效果_第1张图片

二:编写Activity的布局文件




    
        
            
            
            
        
    
    

    
    

style/ToolbarTextStyle和style/CollapseexpandedTextStyle

 
    

三:编写Activity代码

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    RecyclerView recyclerView;
    AppBarLayout appBarLayout;
    private BaseQuickAdapter baseQuickAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        appBarLayout=findViewById(R.id.appbar);
        toolbar.setTitle("我是标题");
        setSupportActionBar(toolbar);
//        设置条目(可以省略)========================
        recyclerView=findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final ArrayList strings = new ArrayList<>();
        for(int x=0;x<10;x++){
            strings.add("我是条目"+x);
        }
        baseQuickAdapter = new BaseQuickAdapter(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.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    
            }
        });
        recyclerView.setAdapter(baseQuickAdapter);
//        ==================================================
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {
                    //展开状态
                    toolbar.setNavigationIcon(R.drawable.ic_backfour);
                }else if(state == State.COLLAPSED){
                    //折叠状态
                    toolbar.setNavigationIcon(R.drawable.ic_back);
                }else {
                    //中间状态
                    toolbar.setNavigationIcon(R.drawable.ic_back);
                }
            }
        });

    }
    private abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
        public enum State {
            EXPANDED,
            COLLAPSED,
            IDLE
        }

        private State mCurrentState = State.IDLE;
        @Override
        public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
            if (i == 0) {
                if (mCurrentState != State.EXPANDED) {
                    onStateChanged(appBarLayout, State.EXPANDED);
                }
                mCurrentState = State.EXPANDED;
            } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
                if (mCurrentState != State.COLLAPSED) {
                    onStateChanged(appBarLayout, State.COLLAPSED);
                }
                mCurrentState = State.COLLAPSED;
            } else {
                if (mCurrentState != State.IDLE) {
                    onStateChanged(appBarLayout, State.IDLE);
                }
                mCurrentState = State.IDLE;
            }
        }
        public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
    }
  }

你可能感兴趣的:(Android视差效果)