├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── fragment_1.xml
│ │ │ │ ├── fragment_2.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── app
│ │ │ └── demo
│ │ │ ├── Fragment1.java
│ │ │ ├── Fragment2.java
│ │ │ ├── ViewPager_Fragment_Adapter.java
│ │ │ ├── CustomScrollViewPager.java
│ │ │ ├── HoldTabScrollView.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── app
│ │ │ └── demo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── app
│ │ └── demo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── 1.png
├── xiaoguo.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── caches
│ └── build_file_checksums.ser
├── vcs.xml
├── runConfigurations.xml
├── gradle.xml
├── misc.xml
└── codeStyles
│ └── Project.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/1.png
--------------------------------------------------------------------------------
/xiaoguo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/xiaoguo.gif
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SwipeRececler
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dafaWang/FixedTabScrollView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jun 22 12:01:21 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #3F5100
5 | #303F9F
6 | #FF4081
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/app/demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/app/demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.app.swiperececler", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/Fragment1.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 |
11 | @SuppressLint("ValidFragment")
12 | public class Fragment1 extends Fragment {
13 | private CustomScrollViewPager viewPager;
14 | public Fragment1(CustomScrollViewPager viewPager) {
15 | this.viewPager = viewPager;
16 | }
17 |
18 | @Nullable
19 | @Override
20 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
21 | View view = View.inflate(getActivity(), R.layout.fragment_1, null);
22 | viewPager.setObjectForPosition(view,0);
23 | return view;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/Fragment2.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 |
4 | import android.annotation.SuppressLint;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v4.app.Fragment;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | @SuppressLint("ValidFragment")
13 | public class Fragment2 extends Fragment {
14 | private CustomScrollViewPager viewPager;
15 | public Fragment2(CustomScrollViewPager viewPager) {
16 | this.viewPager = viewPager;
17 | }
18 | @Nullable
19 | @Override
20 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
21 | View view = View.inflate(getActivity(), R.layout.fragment_2, null);
22 | viewPager.setObjectForPosition(view,1);
23 | return view;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion '27.0.3'
6 | defaultConfig {
7 | applicationId "com.app.swiperececler"
8 | minSdkVersion 16
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile 'com.android.support:appcompat-v7:26.0.2'
24 | compile 'com.android.support.constraint:constraint-layout:1.1.2'
25 | compile 'com.android.support:recyclerview-v7:26.0.2'
26 | compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
27 | compile 'com.yanzhenjie:recyclerview-swipe:1.1.4'
28 | }
29 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/ViewPager_Fragment_Adapter.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 | import android.support.v4.app.Fragment;
4 | import android.support.v4.app.FragmentManager;
5 | import android.support.v4.app.FragmentStatePagerAdapter;
6 | import android.support.v4.app.FragmentTransaction;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | public class ViewPager_Fragment_Adapter extends FragmentStatePagerAdapter {
12 | // private String[] mTitles;
13 | private List mFragments;
14 | private FragmentManager fm;
15 | public ViewPager_Fragment_Adapter(FragmentManager fm) {
16 | super(fm);
17 | }
18 | public ViewPager_Fragment_Adapter(FragmentManager fm,List fragments){
19 | super(fm);
20 | this.fm = fm;
21 | // mTitles = titles;
22 | mFragments = fragments;
23 | }
24 | public ViewPager_Fragment_Adapter(FragmentManager fm,String[] titles,List fragments){
25 | super(fm);
26 | this.fm = fm;
27 | // mTitles = titles;
28 | mFragments = fragments;
29 | }
30 | public void setFragments(ArrayList fragments) {
31 | if(this.mFragments != null){
32 | FragmentTransaction ft = fm.beginTransaction();
33 | for(Fragment f:this.mFragments){
34 | ft.remove(f);
35 | }
36 | ft.commit();
37 | ft=null;
38 | fm.executePendingTransactions();
39 | }
40 | this.mFragments = fragments;
41 | notifyDataSetChanged();
42 | }
43 |
44 | @Override
45 | public Fragment getItem(int position) {
46 | return mFragments.get(position);
47 | }
48 |
49 | /*@Override
50 | public CharSequence getPageTitle(int position) {
51 | return mTitles[position];
52 | }*/
53 |
54 | @Override
55 | public int getCount() {
56 | return mFragments.size();
57 | }
58 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
11 |
12 |
16 |
17 |
24 |
25 |
29 |
30 |
38 |
39 |
40 |
41 |
46 |
47 |
48 |
49 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/CustomScrollViewPager.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 |
4 | import android.content.Context;
5 | import android.support.v4.view.ViewPager;
6 | import android.util.AttributeSet;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.widget.LinearLayout;
10 |
11 | import java.util.HashMap;
12 | import java.util.LinkedHashMap;
13 |
14 | public class CustomScrollViewPager extends ViewPager {
15 | private int current;
16 | private int height = 0;
17 | /**
18 | * 保存position与对于的View
19 | */
20 | private HashMap mChildrenViews = new LinkedHashMap();
21 |
22 | private boolean scrollble = true;
23 |
24 | public CustomScrollViewPager(Context context) {
25 | super(context);
26 | }
27 |
28 | public CustomScrollViewPager(Context context, AttributeSet attrs) {
29 | super(context, attrs);
30 | }
31 |
32 |
33 | @Override
34 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
35 | if (mChildrenViews.size() > current) {
36 | View child = mChildrenViews.get(current);
37 | child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
38 | height = child.getMeasuredHeight();
39 | }
40 |
41 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
42 |
43 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
44 | }
45 |
46 | public void resetHeight(int current) {
47 | this.current = current;
48 | if (mChildrenViews.size() > current) {
49 |
50 | LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
51 | if (layoutParams == null) {
52 | layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
53 | } else {
54 | layoutParams.height = height;
55 | }
56 | setLayoutParams(layoutParams);
57 | }
58 | }
59 |
60 | /**
61 | * 保存position与对于的View
62 | */
63 | public void setObjectForPosition(View view, int position) {
64 | mChildrenViews.put(position, view);
65 | }
66 |
67 |
68 | @Override
69 | public boolean onTouchEvent(MotionEvent ev) {
70 | if (!scrollble) {
71 | return true;
72 | }
73 | return super.onTouchEvent(ev);
74 | }
75 |
76 |
77 | public boolean isScrollble() {
78 | return scrollble;
79 | }
80 |
81 | public void setScrollble(boolean scrollble) {
82 | this.scrollble = scrollble;
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/HoldTabScrollView.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 |
4 | import android.content.Context;
5 | import android.util.AttributeSet;
6 | import android.widget.ScrollView;
7 | import android.widget.Scroller;
8 |
9 | public class HoldTabScrollView extends ScrollView {
10 | private Scroller mScroller;
11 |
12 | public HoldTabScrollView(Context context) {
13 | this(context, null);
14 | mScroller = new Scroller(context);
15 | }
16 |
17 | public HoldTabScrollView(Context context, AttributeSet attrs) {
18 | this(context, attrs, 0);
19 | mScroller = new Scroller(context);
20 | }
21 |
22 | public HoldTabScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
23 | super(context, attrs, defStyleAttr);
24 | mScroller = new Scroller(context);
25 | }
26 |
27 | //调用此方法滚动到目标位置 duration滚动时间
28 | public void smoothScrollToSlow(int fx, int fy, int duration) {
29 | int dx = fx - getScrollX();//mScroller.getFinalX(); 普通view使用这种方法
30 | int dy = fy - getScrollY(); //mScroller.getFinalY();
31 | smoothScrollBySlow(dx, dy, duration);
32 | }
33 |
34 | //调用此方法设置滚动的相对偏移
35 | public void smoothScrollBySlow(int dx, int dy, int duration) {
36 |
37 | //设置mScroller的滚动偏移量
38 | mScroller.startScroll(getScrollX(), getScrollY(), dx, dy, duration);//scrollView使用的方法(因为可以触摸拖动)
39 | // mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration); //普通view使用的方法
40 | invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
41 | }
42 |
43 | @Override
44 | public void computeScroll() {
45 | //先判断mScroller滚动是否完成
46 | if (mScroller.computeScrollOffset()) {
47 |
48 | //这里调用View的scrollTo()完成实际的滚动
49 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
50 |
51 | //必须调用该方法,否则不一定能看到滚动效果
52 | postInvalidate();
53 | }
54 | super.computeScroll();
55 | }
56 |
57 | private OnHoldTabScrollViewScrollChanged mOnObservableScrollViewScrollChanged;
58 |
59 | public void setOnObservableScrollViewScrollChanged(OnHoldTabScrollViewScrollChanged mOnObservableScrollViewScrollChanged) {
60 | this.mOnObservableScrollViewScrollChanged = mOnObservableScrollViewScrollChanged;
61 | }
62 |
63 |
64 | public interface OnHoldTabScrollViewScrollChanged {
65 | void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt);
66 | }
67 |
68 | /**
69 | * @param l Current horizontal scroll origin. 当前滑动的x轴距离
70 | * @param t Current vertical scroll origin. 当前滑动的y轴距离
71 | * @param oldl Previous horizontal scroll origin. 上一次滑动的x轴距离
72 | * @param oldt Previous vertical scroll origin. 上一次滑动的y轴距离
73 | */
74 | @Override
75 | protected void onScrollChanged(int l, int t, int oldl, int oldt) {
76 | super.onScrollChanged(l, t, oldl, oldt);
77 | if (mOnObservableScrollViewScrollChanged != null) {
78 | mOnObservableScrollViewScrollChanged.onObservableScrollViewScrollChanged(l, t, oldl, oldt);
79 | }
80 | }
81 |
82 | @Override
83 | public void fling(int velocityY) {
84 | // super.fling(velocityY / 1000);
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.app.demo;
2 |
3 | import android.support.v4.app.Fragment;
4 | import android.support.v4.view.ViewPager;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 | import android.widget.RelativeLayout;
8 | import android.widget.TextView;
9 |
10 | import java.util.ArrayList;
11 | import java.util.List;
12 |
13 | public class MainActivity extends AppCompatActivity implements HoldTabScrollView.OnHoldTabScrollViewScrollChanged {
14 |
15 | private TextView tablayout;
16 | private CustomScrollViewPager viewPager;
17 | private HoldTabScrollView scrollView;
18 | private int mHeight = 0;
19 | private RelativeLayout centerRl, topRl;
20 | private boolean canJump = true;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 | centerRl = findViewById(R.id.rl_center);
27 | topRl = findViewById(R.id.rl_top);
28 | tablayout = findViewById(R.id.tablayout);
29 | viewPager = findViewById(R.id.viewpager);
30 | scrollView = findViewById(R.id.root_scrollview);
31 | scrollView.setOnObservableScrollViewScrollChanged(this);
32 | List mFragments = new ArrayList<>();
33 | mFragments.add(new Fragment1(viewPager));
34 | mFragments.add(new Fragment2(viewPager));
35 |
36 | ViewPager_Fragment_Adapter mAdapter = new ViewPager_Fragment_Adapter(getSupportFragmentManager(),mFragments);
37 | viewPager.setAdapter(mAdapter);
38 | viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
39 | @Override
40 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
41 |
42 | }
43 |
44 | @Override
45 | public void onPageSelected(int position) {
46 | viewPager.resetHeight(position);
47 | }
48 |
49 | @Override
50 | public void onPageScrollStateChanged(int state) {
51 |
52 | }
53 | });
54 | }
55 |
56 | @Override
57 | public void onWindowFocusChanged(boolean hasFocus) {
58 | super.onWindowFocusChanged(hasFocus);
59 | if (hasFocus) {
60 | //获取HeaderView的高度,当滑动大于等于这个高度的时候,需要把topView移除当前布局,放入到外层布局
61 | mHeight = centerRl.getTop();
62 | }
63 | }
64 |
65 | @Override
66 | public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
67 | if (t >= mHeight) {
68 | if (tablayout.getParent() != topRl) {
69 | centerRl.removeView(tablayout);
70 | topRl.addView(tablayout);
71 | canJump = false;
72 | }
73 | } else {
74 | if (tablayout.getParent() != centerRl) {
75 | topRl.removeView(tablayout);
76 | centerRl.addView(tablayout);
77 | canJump = true;
78 | }
79 | }
80 | if (canJump && t >= oldt) {
81 | scrollView.smoothScrollToSlow(0, mHeight, 300);
82 | } else if (canJump && t < oldt) {
83 | scrollView.smoothScrollToSlow(0, 0, 300);
84 | }
85 | }
86 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FixedTabScrollView
2 | **FixedTabScrollView**是一个scrollView+ViewPager 并实现自动顶吸,固定顶部控件,动态计算Viewpager控件高度的 一个解决方式的Demo (这个Demo应该可以给大家提供一种解决问题的思路,希望能帮助到大家)
3 |
4 | [点击进入项目地址](https://github.com/dafaWang/FixedTabScrollView)
5 |
6 | 实际效果:
7 |
8 | 
9 |
10 | ## 需求和问题:
11 |
12 | 在实际项目中我们常常会碰到这样的需求,scrollView包裹ViewPager,ViewPager包裹Fragment,
13 | 在scrollView向上滑动时,会把ViewPager的选择Tab栏固定在顶部。
14 | 但是在scrollView包裹ViewPager是的高度计算问题也很让人头疼,而且如果突然有个需求,让你想做一个像翻页一样的效果,展示出来就像ViewPager占了一页,这个应该怎么做呢?
15 |
16 | ## 自定义ScrollView
17 |
18 | 既然是要在scrollView滑动时候做一些操作,那么我们就需要监听scrollView的滑动事件,但是原生控件没有将 onScrollChanged()对外开放,所以我们需要自定义一个ScrollView,并且将滑动事件暴露出去
19 |
20 | ```java
21 | public class HoldTabScrollView extends ScrollView {
22 | private Scroller mScroller;
23 |
24 | public HoldTabScrollView(Context context) {
25 | this(context, null);
26 | mScroller = new Scroller(context);
27 | }
28 |
29 | public HoldTabScrollView(Context context, AttributeSet attrs) {
30 | this(context, attrs, 0);
31 | mScroller = new Scroller(context);
32 | }
33 |
34 | public HoldTabScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
35 | super(context, attrs, defStyleAttr);
36 | mScroller = new Scroller(context);
37 | }
38 |
39 | //调用此方法滚动到目标位置 duration滚动时间
40 | public void smoothScrollToSlow(int fx, int fy, int duration) {
41 | int dx = fx - getScrollX();//mScroller.getFinalX(); 普通view使用这种方法
42 | int dy = fy - getScrollY(); //mScroller.getFinalY();
43 | smoothScrollBySlow(dx, dy, duration);
44 | }
45 |
46 | //调用此方法设置滚动的相对偏移
47 | public void smoothScrollBySlow(int dx, int dy, int duration) {
48 |
49 | //设置mScroller的滚动偏移量
50 | mScroller.startScroll(getScrollX(), getScrollY(), dx, dy, duration);//scrollView使用的方法(因为可以触摸拖动)
51 | // mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration); //普通view使用的方法
52 | invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
53 | }
54 |
55 | @Override
56 | public void computeScroll() {
57 | //先判断mScroller滚动是否完成
58 | if (mScroller.computeScrollOffset()) {
59 |
60 | //这里调用View的scrollTo()完成实际的滚动
61 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
62 |
63 | //必须调用该方法,否则不一定能看到滚动效果
64 | postInvalidate();
65 | }
66 | super.computeScroll();
67 | }
68 |
69 | private OnHoldTabScrollViewScrollChanged mOnObservableScrollViewScrollChanged;
70 |
71 | public void setOnObservableScrollViewScrollChanged(OnHoldTabScrollViewScrollChanged mOnObservableScrollViewScrollChanged) {
72 | this.mOnObservableScrollViewScrollChanged = mOnObservableScrollViewScrollChanged;
73 | }
74 |
75 |
76 | public interface OnHoldTabScrollViewScrollChanged {
77 | void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt);
78 | }
79 |
80 | /**
81 | * @param l Current horizontal scroll origin. 当前滑动的x轴距离
82 | * @param t Current vertical scroll origin. 当前滑动的y轴距离
83 | * @param oldl Previous horizontal scroll origin. 上一次滑动的x轴距离
84 | * @param oldt Previous vertical scroll origin. 上一次滑动的y轴距离
85 | */
86 | @Override
87 | protected void onScrollChanged(int l, int t, int oldl, int oldt) {
88 | super.onScrollChanged(l, t, oldl, oldt);
89 | if (mOnObservableScrollViewScrollChanged != null) {
90 | mOnObservableScrollViewScrollChanged.onObservableScrollViewScrollChanged(l, t, oldl, oldt);
91 | }
92 | }
93 |
94 | @Override
95 | public void fling(int velocityY) {
96 | // super.fling(velocityY / 1000);
97 | }
98 | }
99 | ```
100 |
101 | 这样一来,在我们使用HoldTabScrollView的时候,通过setOnObservableScrollViewScrollChanged可以设置一个滑动监听,以此来对其滑动过程中做一些操作。
102 |
103 | ## 修改布局
104 |
105 | 固定View的原理就是在顶部创建一个同View高度相同的父容器,在View滑动上去的时候,移除掉原来的View,并且将此View添加到顶部父容器中,,同理,在View滑动下来的时候,移除,顶部父容器中的View,并将此View添加到原父容器中。
106 |
107 |
108 | 
109 |
110 | 我们来修改一下布局:
111 |
112 | ```java
113 |
114 |
115 |
118 |
119 |
123 |
124 |
128 |
129 |
136 |
137 |
141 |
142 |
150 |
151 |
152 |
153 |
158 |
159 |
160 |
161 |
165 |
166 |
167 |
168 | ```
169 |
170 | ## 解决ScrollView 嵌套ViewPager高度计算问题
171 |
172 | 可以看到布局中有一个CustomScrollViewPager 这个ViewPager可以动态的计算它所包裹的Fragment的高度,并将这个实时计算的高度来设置ViewPager
173 |
174 | ```java
175 | public class CustomScrollViewPager extends ViewPager {
176 | private int current;
177 | private int height = 0;
178 | /**
179 | * 保存position与对于的View
180 | */
181 | private HashMap mChildrenViews = new LinkedHashMap();
182 |
183 | private boolean scrollble = true;
184 |
185 | public CustomScrollViewPager(Context context) {
186 | super(context);
187 | }
188 |
189 | public CustomScrollViewPager(Context context, AttributeSet attrs) {
190 | super(context, attrs);
191 | }
192 |
193 |
194 | @Override
195 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
196 | if (mChildrenViews.size() > current) {
197 | View child = mChildrenViews.get(current);
198 | child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
199 | height = child.getMeasuredHeight();
200 | }
201 |
202 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
203 |
204 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
205 | }
206 |
207 | public void resetHeight(int current) {
208 | this.current = current;
209 | if (mChildrenViews.size() > current) {
210 |
211 | LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
212 | if (layoutParams == null) {
213 | layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
214 | } else {
215 | layoutParams.height = height;
216 | }
217 | setLayoutParams(layoutParams);
218 | }
219 | }
220 |
221 | /**
222 | * 保存position与对于的View
223 | */
224 | public void setObjectForPosition(View view, int position) {
225 | mChildrenViews.put(position, view);
226 | }
227 |
228 |
229 | @Override
230 | public boolean onTouchEvent(MotionEvent ev) {
231 | if (!scrollble) {
232 | return true;
233 | }
234 | return super.onTouchEvent(ev);
235 | }
236 |
237 |
238 | public boolean isScrollble() {
239 | return scrollble;
240 | }
241 |
242 | public void setScrollble(boolean scrollble) {
243 | this.scrollble = scrollble;
244 | }
245 |
246 | }
247 | ```
248 |
249 | 我们在创建ViewPager中的Fragment实例的时候需要通过构造函数将这个ViewPager传进去
250 |
251 | ```java
252 | mFragments.add(new Fragment1(viewPager));
253 | mFragments.add(new Fragment2(viewPager));
254 | ```
255 |
256 | 在Fragment中 调用↓来重新计算内部高度并设置ViewPager(第二个参数标识当前fragment在ViewPager中的位置)
257 | ```java
258 | viewPager.setObjectForPosition(view,0);
259 | ```
260 |
261 | ## 计算需要被固定的控件的位置
262 |
263 | 因为我们在监听scrollView滚动的时候需要知道,滚动到哪个地方就移除原tab,添加新tab,所以我们需要知道原来的控件位置,
264 | 我们在onWindowFocusChanged方法中去获取
265 | ```java
266 | @Override
267 | public void onWindowFocusChanged(boolean hasFocus) {
268 | super.onWindowFocusChanged(hasFocus);
269 | if (hasFocus) {
270 | //获取HeaderView的高度,当滑动大于等于这个高度的时候,需要把tabView移除当前布局,放入到外层布局
271 | mHeight = centerRl.getTop();
272 | }
273 | }
274 | ```
275 |
276 | 根据这个高度来写滑动监听事件
277 |
278 | ```java
279 | @Override
280 | public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
281 | if (t >= mHeight) {
282 | if (tablayout.getParent() != topRl) {
283 | centerRl.removeView(tablayout);
284 | topRl.addView(tablayout);
285 | }
286 | } else {
287 | if (tablayout.getParent() != centerRl) {
288 | topRl.removeView(tablayout);
289 | centerRl.addView(tablayout);
290 | }
291 | }
292 | }
293 | ```
294 |
295 |
296 | ## 自动顶吸效果
297 |
298 | 自动顶吸的效果就是 用户向上滑动一点距离,页面自动滑动到scrollView固定的空间位置,完全展示viewPager中的内容。
299 |
300 | 我们使用ScrollView的ScrollTo来实现,不过因为这个方法滚动速度太快,我们需要对速度做一些处理并且禁用惯性滚动
301 |
302 | ### 对滚动速度的处理我们使用Scroller这个对象,在自定义ScrollView中:
303 |
304 | ```java
305 | //调用此方法滚动到目标位置 duration滚动时间
306 | public void smoothScrollToSlow(int fx, int fy, int duration) {
307 | int dx = fx - getScrollX();//mScroller.getFinalX(); 普通view使用这种方法
308 | int dy = fy - getScrollY(); //mScroller.getFinalY();
309 | smoothScrollBySlow(dx, dy, duration);
310 | }
311 |
312 | //调用此方法设置滚动的相对偏移
313 | public void smoothScrollBySlow(int dx, int dy, int duration) {
314 |
315 | //设置mScroller的滚动偏移量
316 | mScroller.startScroll(getScrollX(), getScrollY(), dx, dy, duration);//scrollView使用的方法(因为可以触摸拖动)
317 | // mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration); //普通view使用的方法
318 | invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
319 | }
320 | ```
321 |
322 | ### 禁用惯性滚动
323 |
324 | ```java
325 | @Override
326 | public void fling(int velocityY) {
327 | //此处不进行任何操作就可以了
328 | }
329 | ```
330 |
331 |
332 | ### 修改activity中滑动监听
333 |
334 | ```java
335 | @Override
336 | public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
337 | if (t >= mHeight) {
338 | if (tablayout.getParent() != topRl) {
339 | centerRl.removeView(tablayout);
340 | topRl.addView(tablayout);
341 | canJump = false;
342 | }
343 | } else {
344 | if (tablayout.getParent() != centerRl) {
345 | topRl.removeView(tablayout);
346 | centerRl.addView(tablayout);
347 | canJump = true;
348 | }
349 | }
350 | if (canJump && t >= oldt) {
351 | scrollView.smoothScrollToSlow(0, mHeight, 300);
352 | } else if (canJump && t < oldt) {
353 | scrollView.smoothScrollToSlow(0, 0, 300);
354 | }
355 | }
356 | ```
357 | 我们添加了一个布尔类型的判断标识,如果为true的时候就开启自动吸顶(ViewPager没有占满一页的时候),为false的时候关闭自动吸顶。
358 | 利用ScrollView中自定义的smoothScrollToSlow方法来滑动到指定位置,最后一个参数可以调节滑动速度。
359 |
360 |
361 |
362 | 整套实现方式的关键代码都在这里了,具体的代码欢迎去顶部github地址下载跑一遍看看
363 |
--------------------------------------------------------------------------------