美文网首页
CoordinatorLayout + AppBarLayout

CoordinatorLayout + AppBarLayout

作者: TTTqiu | 来源:发表于2016-09-09 01:48 被阅读1207次
    dependencies {
        ......
        compile 'com.android.support:design:24.2.0'
        compile 'com.android.support:support-v4:24.2.0'
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true" >
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e0e0e0"
                app:tabIndicatorColor="#ef5350"
                app:tabSelectedTextColor="#1976d2"
                app:tabTextColor="#90caf9" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    
    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<MyViewHolder> {
    
            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

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