常用的Toolbar+viewpager汇总(二)

本文基于常用的Toolbar+viewpager汇总(一)进行修改

GIF1.gif

下载链接:https://share.weiyun.com/5IHvE0K

public class MainActivity extends AppCompatActivity {

    private TabLayout tableLayout;
    private ViewPager viewPager;
    List list_fragment = new ArrayList<>();
    private TabAdapter tabAdapter;
    String[] favourites = {"页面一", "页面二", "页面三"};
    private Toolbar toolbar;
    private AppBarLayout appBarLayout;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        list_fragment.add(new DemoFragment());
        list_fragment.add(new DemoFragment());
        list_fragment.add(new DemoFragment());
        tabAdapter = new TabAdapter(getSupportFragmentManager());
        tableLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(tabAdapter);

        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {
                    //展开状态
                    toolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                }else if(state == State.COLLAPSED){
                    //折叠状态
                    toolbar.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                }else {
                    //中间状态
                    toolbar.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                }
                collapsingToolbarLayout.setTitle("标题");
                collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);
            }
        });
    }

    class TabAdapter extends FragmentPagerAdapter {
        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            return list_fragment.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return favourites[position];
        }
    }

    private void initView() {
        tableLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        imageView = (ImageView) findViewById(R.id.iv);

    }
}

新增类

public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    public enum State {
        EXPANDED,
        COLLAPSED,
        IDLE
    }

    private State mCurrentState = State.IDLE;

    @Override
    public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (i == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }

    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}



    

        

            

                

                    

                

                

                
            

            

            

        

        

            
        

    




你可能感兴趣的:(常用的Toolbar+viewpager汇总(二))