CoordinatorLayout + AppBarLayout

2016-09-09  本文已影响1207人  TTTqiu
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"。

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

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

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

toolbar = (Toolbar) findViewById(R.id.toolBar); 
setSupportActionBar(toolbar);
上一篇下一篇

猜你喜欢

热点阅读