CoordinatorLayout + AppBarLayout+ToolBar 实现向上滚动隐藏 ToolBar

dependencies {
    ......
    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:support-v4:24.2.0'
}



    

        

        
    

    


package com.ttt.toolbartest;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        for (int i = 0; i < 10; i++)
            tabLayout.addTab(tabLayout.newTab().setText("选项卡槽" + i));
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayout.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        RecyclerView.Adapter mAdapter = new MyRecyclerViewAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter {

        private LayoutInflater mLayoutInflater;

        public MyRecyclerViewAdapter(Activity activity) {
            super();
            mLayoutInflater=activity.getLayoutInflater();
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

            View v = mLayoutInflater.inflate(android.R.layout.simple_list_item_1, null);
            MyViewHolder holder = new MyViewHolder(v);
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder viewHolder, int pos) {
            viewHolder.text.setText("数据" + pos);
        }

        @Override
        public int getItemCount() {
            return 100;
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);
            text=(TextView) itemView.findViewById(android.R.id.text1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
}

1. Toolbar 设置 app:layout_scrollFlags="scroll|enterAlways"。

  • scroll: 所有想滚动出屏幕的 view 都需要设置这个 flag,没有设置这个 flag 的 view 将被固定在屏幕顶部。
  • enterAlways: 让任意向下的滚动都会导致该 view 变为可见。
  • enterAlwaysCollapsed: 当视图已经设置 minHeight 属性又使用此标志时,视图只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。
  • exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端。

2. 给有滚动嵌套的控件设置触发 app:layout_behavior ="@string/appbar_scrolling_view_behavior"

  • RecyclerView、Nestedscrollview 等。也可以设置在上一层 FrameLayout、LinearLayout 中。
  • ListView、ScrollView 似乎不能用。

3. 把活动的 theme 设置成 NoActionBar 的。

4. 在活动中用 Toolbar 代替 Actionbar。onCreateOptionsMenu 等方法依然有效。

toolbar = (Toolbar) findViewById(R.id.toolBar); 
setSupportActionBar(toolbar);

你可能感兴趣的:(CoordinatorLayout + AppBarLayout+ToolBar 实现向上滚动隐藏 ToolBar)