美文网首页
常用的Toolbar+viewpager汇总(二)

常用的Toolbar+viewpager汇总(二)

作者: it奔跑在路上 | 来源:发表于2019-04-23 17:49 被阅读0次

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

    GIF1.gif

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

    public class MainActivity extends AppCompatActivity {
    
        private TabLayout tableLayout;
        private ViewPager viewPager;
        List<Fragment> 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);
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.sloop.tabdemo.MainActivity">
    
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff"
                android:fitsSystemWindows="true"
                app:elevation="0dp">
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:collapsedTitleTextAppearance="@style/collapsedStyle"
                    app:expandedTitleTextAppearance="@style/expandedStyle"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_scrollFlags="scroll">
    
                        <ImageView
                            android:id="@+id/iv"
                            android:layout_width="match_parent"
                            android:layout_height="280dp"
                            android:scaleType="fitXY"
                            android:src="@drawable/img" />
    
                    </RelativeLayout>
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:contentInsetLeft="0dp"
                        app:contentInsetStart="0dp"
                        app:layout_collapseMode="pin">
    
                    </android.support.v7.widget.Toolbar>
                </android.support.design.widget.CollapsingToolbarLayout>
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    app:tabIndicatorHeight="3dp"
                    app:tabSelectedTextColor="#f00"
                    app:tabTextColor="#000" />
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#25979797" />
    
            </android.support.design.widget.AppBarLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:overScrollMode="never" />
            </RelativeLayout>
    
        </android.support.design.widget.CoordinatorLayout>
    
    
    </LinearLayout>
    
    

    相关文章

      网友评论

          本文标题:常用的Toolbar+viewpager汇总(二)

          本文链接:https://www.haomeiwen.com/subject/lzrbgqtx.html