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

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

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

前面学习了coordinatorLayout,

连接:http://cherylgood.cn/c/Android开发之CoordinatorLayout使用详解一.php

http://cherylgood.cn/c/Android开发之CoordinatorLayout使用详解二.php

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

1、主布局文件




    

        

            
        


    

    

        

        

        
    

重点看这两句:

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();
        }
    }
}


http://cherylgood.cn/c/Android开发之CoordinatorLayoutAppBarLayoutViewPagerTabLayout实现.php

你可能感兴趣的:(Android)