NestedScrollView嵌套ViewPager

效果图

NestedScrollView嵌套ViewPager-LMLPHPNestedScrollView嵌套ViewPager-LMLPHP

重写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;
}
}
05-08 15:51