项目之TabLayout + AppBarLayout + CoordinatorLayout

项目之TabLayout + AppBarLayout +  CoordinatorLayout,实现页签滑动,并且实现标题滑动隐藏。


CoordinatorLayout:

协调(Coordinate)其他组件,实现联动,使界面达到各式各样的滚动效果。

1、作为顶层布局 
2、调度协调子布局

AppBarLayout

1、AppBarLayout继承自LinearLayout,布局方向为垂直方向。

2、AppBarLayout是在LinearLayou上加了一些材料设计的概念,它可以让你定制当某个可滚动View的滚动手势发生变化时,其内部的子View实现何种动作。

内部的子View通过在布局中加app:layout_scrollFlags设置执行的动作,包括

 NestedScrollView依赖于ActionBar决定执行动作
            layout_scrollFlags设置执行动作
            scroll:当指定的ScrollView发生滚动时,该View也跟随一起滚动,就好像这个View也是属于这个ScrollView一样
            enterAlways:当ScrollView往下滚动时,该View会直接往下滚动。而不用考虑ScrollView是否在滚动
            exitUntilCollapsed:当这个View要往上逐渐“消逝”时,会一直往上滑动,直到剩下的的高度达到它的最小高度后,再响应ScrollView的内部滑动事件
            enterAlwaysCollapsed:首先是enterAlways效果,当View的高度达到最小高度时,View就暂时不去往下滚动,直到ScrollView滑动到顶部不再滑动时,View再继续往下滑动,直到滑到View的顶部结束


需求依赖

compile 'com.android.support:design:25.3.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        <!--
            NestedScrollView依赖于ActionBar决定执行动作
            layout_scrollFlags设置执行动作
            scroll:当指定的ScrollView发生滚动时,该View也跟随一起滚动,就好像这个View也是属于这个ScrollView一样
            enterAlways:当ScrollView往下滚动时,该View会直接往下滚动。而不用考虑ScrollView是否在滚动
            exitUntilCollapsed:当这个View要往上逐渐“消逝”时,会一直往上滑动,直到剩下的的高度达到它的最小高度后,再响应ScrollView的内部滑动事件
            enterAlwaysCollapsed:首先是enterAlways效果,当View的高度达到最小高度时,View就暂时不去往下滚动,直到ScrollView滑动到顶部不再滑动时,View再继续往下滑动,直到滑到View的顶部结束
        -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="新闻资讯"
            app:titleTextColor="@color/white" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabBackground="@color/colorPrimary"
            app:tabIndicatorColor="@color/white"
            app:tabSelectedTextColor="@color/white"
            app:tabTextAppearance="@style/TabLayoutTextSize"
            app:tabTextColor="@color/white" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.huading.myvolley.widget.LoadStateLayout
        android:id="@+id/ls_loadStateLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.huading.myvolley.widget.LoadStateLayout>
</android.support.design.widget.CoordinatorLayout>

TabLyout标题的字体大小,styles.xml

    <style name="TabLayoutTextSize">
        <item name="android:textSize">16sp</item>
    </style>

oneFragment

package com.huading.myvolley.ui;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.huading.myvolley.R;
import com.huading.myvolley.adapter.MyFragmentPagerAdapter;
import com.huading.myvolley.base.BaseFragment;
import com.huading.myvolley.widget.LoadStateLayout;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 作者:LHZ on 2017/5/27 13:50
 * 界面:
 */
public class OneFragment extends BaseFragment {

    @InjectView(R.id.ls_loadStateLayout)
    LoadStateLayout lsLoadStateLayout;
    @InjectView(R.id.toolbar)
    Toolbar mTitleBar;
    @InjectView(R.id.tabs)
    TabLayout mTabs;
    @InjectView(R.id.viewPager)
    ViewPager mViewPager;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.one_fragment;
    }

    @Override
    protected void initViewsAndEvents(View view) {

        mTitleBar.setTitle("新闻资讯");
        ArrayList<String> mTitleList = new ArrayList<>();
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mTitleList.add("头条");
        mTitleList.add("足球");
        mTitleList.add("汽车");
        mTitleList.add("笑话");
        mFragments.add(new Fragment1());
        mFragments.add(new Fragment2());
        mFragments.add(new Fragment3());
        mFragments.add(new Fragment4());
        mTabs.setTabMode(TabLayout.MODE_FIXED);
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitleList);
        mViewPager.setAdapter(pagerAdapter);
        mTabs.setupWithViewPager(mViewPager);
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void DestroyViewAndThing() {
        ButterKnife.reset(this);
    }


}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值