Android开发之CoordinatorLayoutAppBarLayoutViewPagerTabLayout实现顶部伸缩效果

Android开发之CoordinatorLayoutAppBarLayoutViewPagerTabLayout实现顶部伸缩效果_第1张图片

源码已上传到码云上https://git.oschina.net/osczaizai/AndroidDemo

前面学习了coordinatorLayout,

连接:Android开发之CoordinatorLayout使用详解一.php

Android开发之CoordinatorLayout使用详解二.php

现在我们来个小demo 其实代码都很简单,只要你看了前面的两个使用介绍,基本久OK了

1、主布局文件


xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_recycler_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="guanaj.com.coordinatorlayoutdemo.recyclerdemo.RecyclerViewActivity">

android:id="@+id/appbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="120dp"

android:background="#cccccc"

app:layout_scrollFlags="scroll|enterAlways">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="top content"

android:textColor="#000" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:id="@+id/tablayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:tabTextColor="#000000" />

android:id="@+id/viewpager"

android:layout_width="match_parent"

android:layout_height="match_parent">

重点看这两句:

app:layout_scrollFlags="scroll|enterAlways"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

其实

layout_behavior 只要设置在有子view 是可以滚动的就可以了,

主activity代码:

package guanaj.com.coordinatorlayoutdemo.recyclerdemo;

import android.net.Uri;

import android.support.design.widget.TabLayout;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;

import java.util.List;

import guanaj.com.coordinatorlayoutdemo.R;

public class RecyclerViewActivity extends AppCompatActivity {

private TabLayout tabLayout;

private ViewPager viewPager;

private List fragmentList = new ArrayList<>();

private MyFragmentAdapter adapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_recycler_view);

initViews();

initEvent();

initData();

}

private void initViews() {

tabLayout = (TabLayout) findViewById(R.id.tablayout);

viewPager = (ViewPager) findViewById(R.id.viewpager);

tabLayout.setupWithViewPager(viewPager);

adapter = new MyFragmentAdapter(getSupportFragmentManager());

viewPager.setAdapter(adapter);

}

private void initEvent() {

}

private void initData() {

for (int i = 0;i<5;i++){

Fragment fragment = new ContentFragment();

fragmentList.add(fragment);

}

adapter.notifyDataSetChanged();

}

class MyFragmentAdapter extends FragmentPagerAdapter {

public MyFragmentAdapter(FragmentManager fm) {

super(fm);

}

@Override

public CharSequence getPageTitle(int position) {

return "标题:"+position;

}

@Override

public Fragment getItem(int position) {

return fragmentList.get(position);

}

@Override

public int getCount() {

return fragmentList == null ? 0 : fragmentList.size();

}

}

}

Android开发之CoordinatorLayoutAppBarLayoutViewPagerTabLayout实现.php

你可能感兴趣的:(Android开发之CoordinatorLayoutAppBarLayoutViewPagerTabLayout实现顶部伸缩效果)