美文网首页Androidandroid
NestedScrollView嵌套ViewPager

NestedScrollView嵌套ViewPager

作者: kongqw | 来源:发表于2016-03-09 15:23 被阅读5809次

    效果图

    这里写图片描述 这里写图片描述

    重写ViewPager

    package com.kongqw.kbox.view;
    
    import android.content.Context;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    import android.view.View;
    
    
    public class WrapContentHeightViewPager extends ViewPager {
        public WrapContentHeightViewPager(Context context) {
            super(context);
        }
    
        public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int height = 0;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                if (h > height) height = h;
            }
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    XML布局

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.kongqw.kbox.activity.ResumeActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@mipmap/resume" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:title="Toolbar">
    
                </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:tabIndicatorColor="#FFFFFFFF"
                app:tabSelectedTextColor="#FF888888"
                app:tabTextColor="#FFFFFFFF" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <com.kongqw.kbox.view.WrapContentHeightViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    主类

    package com.kongqw.kbox.activity;
    
    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.view.ViewPager;
    import android.support.v4.widget.NestedScrollView;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    
    import com.kongqw.kbox.R;
    import com.kongqw.kbox.adapter.ResumeAdapter;
    
    public class ResumeActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_resume);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            // 设置返回主页的按钮
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            // TabLayout
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
            // ViewPager
            ViewPager mPager = (ViewPager) findViewById(R.id.viewPager);
            ResumeAdapter mPagerAdapter = new ResumeAdapter(getSupportFragmentManager());
            mPager.setAdapter(mPagerAdapter);
            tabLayout.setupWithViewPager(mPager);
            // ViewPager切换时NestedScrollView滑动到顶部
            mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    ((NestedScrollView) findViewById(R.id.nestedScrollView)).scrollTo(0, 0);
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
        }
    }
    

    Adapter

    package com.kongqw.kbox.adapter;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    import com.kongqw.kbox.fragment.ResumeBasicFragment;
    import com.kongqw.kbox.fragment.ResumeProjectsExperienceFragment;
    import com.kongqw.kbox.fragment.ResumeSkillFragment;
    import com.kongqw.kbox.fragment.ResumeWorksExperienceFragment;
    
    /**
     * Created by kongqw on 2016/3/7.
     */
    public class ResumeAdapter extends FragmentPagerAdapter {
    
        public ResumeAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            if (0 == position) {
                fragment = new ResumeBasicFragment();
            } else if (1 == position) {
                fragment = new ResumeSkillFragment();
            } else if (2 == position) {
                fragment = new ResumeWorksExperienceFragment();
            } else if (3 == position) {
                fragment = new ResumeProjectsExperienceFragment();
            }
            return fragment;
        }
    
        @Override
        public int getCount() {
            return 4;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "TAB 1";
                case 1:
                    return "TAB 2";
                case 2:
                    return "TAB 3";
                case 3:
                    return "TAB 4";
            }
            return null;
        }
    }
    

    相关文章

      网友评论

      • 杨艳伟:上下滚动时,会出现viewpager中所有fragment同时滚动,这是什么情况
      • 1a1f17196705:你好,是这样的,我用了这种方式嵌套可以实现,但是在viewpager里面的fragment中,有EditText,并且AndroidManifest中有设置android:windowSoftInputMode="adjustPan",一旦EditText被focus后,并滑动屏幕,整个界面可以滑动到屏幕的最上面去
      • 夙愿終不毁:有demo吗?viewpager的子页面里面的内容高度不一样,最终结果是按照最长的子页面来画高度,另一个短的页面也和最长的一样高,怎么解决,让所有页面都包裹内容。
        C丶_bb46:你好,我碰到同样的问题了 ,你解决了吗 哥们
      • 夙愿終不毁:有demo吗,大大,麻烦贴一下demo啊 拜托 么么哒
      • MambasJi:设置将高度固定吗
      • 木马上的骑士:有这个的demo吗,拜托了
        夙愿終不毁:有demo吗?viewpager的子页面里面的内容高度不一样,最终结果是按照最长的子页面来画高度,另一个短的页面也和最长的一样高,怎么解决,让所有页面都包裹内容。
      • af272c6e74df:如果viewpager内还有listview,重写listview解决滑动冲突后tabLayout就无法置顶了,要怎么解决啊?
        开心wonderful:@天天童话丶 最近也碰上这问题了,想问下解决思路,或者有什么相关博文推荐?
        bcf9bfb444ec:你是又重写的listview 或者重写RecyclerView 吗 ?
        HuaYang:你的问题有解决?

      本文标题:NestedScrollView嵌套ViewPager

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