NETWORK_IMAGES = new ArrayList<>();
16 |
17 | public static int[] NATIVE_IMAGES = {R.mipmap.icon_1, R.mipmap.icon_2, R.mipmap.icon_3, R.mipmap.icon_4, R.mipmap.icon_5};
18 |
19 |
20 | static {
21 | NETWORK_IMAGES.add("https://ws1.sinaimg.cn/large/610dc034ly1fhj53yz5aoj21hc0xcn41.jpg");
22 | NETWORK_IMAGES.add("https://ws1.sinaimg.cn/large/610dc034ly1fhhz28n9vyj20u00u00w9.jpg");
23 | NETWORK_IMAGES.add("https://ws1.sinaimg.cn/large/610dc034ly1fhgsi7mqa9j20ku0kuh1r.jpg");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/leon/loopviewpager/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpager;
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 | * Instrumentation 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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.leon.loopviewpager", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/lib/src/androidTest/java/com/leon/loopviewpagerlib/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpagerlib;
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 | * Instrumentation 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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.leon.loopviewpagerlib.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.3"
6 | defaultConfig {
7 | applicationId "com.leon.loopviewpager"
8 | minSdkVersion 15
9 | targetSdkVersion 25
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 fileTree(include: ['*.jar'], dir: 'libs')
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.1'
28 | testCompile 'junit:junit:4.12'
29 | compile project(':lib')
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/leon/loopviewpager/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpager;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 |
8 | public class MainActivity extends AppCompatActivity {
9 |
10 | @Override
11 | protected void onCreate(Bundle savedInstanceState) {
12 | super.onCreate(savedInstanceState);
13 | setContentView(R.layout.activity_main);
14 |
15 | }
16 |
17 | public void onStartNormalActivity(View view) {
18 | Intent intent = new Intent(this, DemoActivity.class);
19 | startActivity(intent);
20 | }
21 |
22 | public void onStartNetworkActivity(View view) {
23 | Intent intent = new Intent(this, NetworkActivity.class);
24 | startActivity(intent);
25 | }
26 |
27 | public void onStartListActivity(View view) {
28 | Intent intent = new Intent(this, FunBannerListActivity.class);
29 | startActivity(intent);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
21 |
22 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/leon/loopviewpager/DemoActivity.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpager;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.widget.FrameLayout;
8 |
9 | import com.leon.loopviewpagerlib.FunBanner;
10 |
11 | /**
12 | * Created by Leon on 2017/3/1.
13 | */
14 |
15 | public class DemoActivity extends AppCompatActivity {
16 |
17 |
18 | @Override
19 | protected void onCreate(@Nullable Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_demo);
22 | init();
23 | }
24 |
25 | private void init() {
26 | FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);
27 | FunBanner.Builder builder = new FunBanner.Builder(this);
28 | FunBanner funBanner = builder.setEnableAutoLoop(true)
29 | .setImageResIds(ImagesProvider.NATIVE_IMAGES)
30 | .setDotSelectedColor(Color.GREEN)
31 | .setHeightWidthRatio(0.5556f)
32 | .setLoopInterval(5000)
33 | .setEnableAutoLoop(true)
34 | .setIndicatorBackgroundColor(R.color.indicator_bg)
35 | .build();
36 | frameLayout.addView(funBanner);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/lib/src/main/res/layout/view_loop.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
14 |
21 |
22 |
29 |
30 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/lib/src/main/res/values/attrs.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 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/leon/loopviewpager/FunBannerListActivity.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpager;
2 |
3 | import android.app.ListActivity;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.BaseAdapter;
10 |
11 | import com.leon.loopviewpagerlib.FunBanner;
12 |
13 | /**
14 | * 作者:Leon
15 | * 时间:2017/7/16
16 | * 包名:com.leon.loopviewpager
17 | * 公司:黑马程序员
18 | */
19 |
20 | public class FunBannerListActivity extends ListActivity{
21 |
22 | @Override
23 | protected void onCreate(@Nullable Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_list);
26 |
27 | FunBanner funBanner = new FunBanner.Builder(this).setEnableAutoLoop(true)
28 | .setImageUrls(ImagesProvider.NETWORK_IMAGES)
29 | .setIndicatorBackgroundColor(R.color.indicator_bg)
30 | .setIndicatorBarHeight(80)
31 | .setHeight(400)
32 | .build();
33 | getListView().addHeaderView(funBanner);
34 |
35 | setListAdapter(mBaseAdapter);
36 | }
37 |
38 | private BaseAdapter mBaseAdapter = new BaseAdapter() {
39 | @Override
40 | public int getCount() {
41 | return 30;
42 | }
43 |
44 | @Override
45 | public Object getItem(int position) {
46 | return null;
47 | }
48 |
49 | @Override
50 | public long getItemId(int position) {
51 | return 0;
52 | }
53 |
54 | @Override
55 | public View getView(int position, View convertView, ViewGroup parent) {
56 | if (convertView == null) {
57 | convertView = LayoutInflater.from(FunBannerListActivity.this).inflate(R.layout.list_item, null);
58 | }
59 | return convertView;
60 | }
61 | };
62 | }
63 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/leon/loopviewpagerlib/PageIndicator.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpagerlib;
2 |
3 | import android.support.v4.view.ViewPager;
4 |
5 | /*
6 | * Copyright (C) 2011 Patrik Akerfeldt
7 | * Copyright (C) 2011 Jake Wharton
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | */
21 |
22 | /**
23 | * A PageIndicator is responsible to show an visual indicator on the total views
24 | * number and the current visible view.
25 | */
26 | public interface PageIndicator extends ViewPager.OnPageChangeListener {
27 | /**
28 | * Bind the indicator to a ViewPager.
29 | *
30 | * @param view
31 | */
32 | void setViewPager(ViewPager view);
33 |
34 | /**
35 | * Bind the indicator to a ViewPager.
36 | *
37 | * @param view
38 | * @param initialPosition
39 | */
40 | void setViewPager(ViewPager view, int initialPosition);
41 |
42 | /**
43 | * Set the current page of both the ViewPager and indicator.
44 | *
45 | * This must be used if you need to set the page before
46 | * the views are drawn on screen (e.g., default start page).
47 | *
48 | * @param item
49 | */
50 | void setCurrentItem(int item);
51 |
52 | /**
53 | * Set a page change listener which will receive forwarded events.
54 | *
55 | * @param listener
56 | */
57 | void setOnPageChangeListener(ViewPager.OnPageChangeListener listener);
58 |
59 | /**
60 | * Notify the indicator that the fragment list has changed.
61 | */
62 | void notifyDataSetChanged();
63 | }
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
19 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 简介 #
2 | 本项目是一个轻量级的轮播图,提供了许多属性的配置,其中包括了设置图片宽高比例来调整Banner的宽高以防止图片缩放造成的显示问题,简单易用。
3 | # 使用姿势 #
4 |
5 | ## 集成 ##
6 | //项目下的build.gradle
7 | allprojects {
8 | repositories {
9 | ...
10 | maven { url 'https://jitpack.io' }
11 | }
12 | }
13 |
14 | //module下的build.gradle
15 | dependencies {
16 | compile 'com.github.uncleleonfan:FunBanner:1.2.0'
17 | }
18 |
19 | ## 通过xml配置 ##
20 |
21 |
35 |
36 |
37 | >可配置属性
38 |
39 | * dot_radius 点的半径
40 | * dot_normal_color 点的正常颜色
41 | * dot_selected_color 点被选中时的颜色
42 | * enable_auto_loop 是否开启自动轮播
43 | * loop_interval 自动轮播的时间间隔
44 | * show_indicator 是否显示指示器
45 | * indicator_bar_height 指示器的高度
46 | * indicator_bar_background_color 指示器的背景颜色
47 | * height_width_ratio 显示图片的高宽比
48 | * height_width_ratio 标题的颜色,如果没有设置标题数据,则不会显示标题
49 |
50 | ## 通过代码配置 ##
51 |
52 | private void init() {
53 | FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);
54 | FunBanner.Builder builder = new FunBanner.Builder(this);
55 | FunBanner funBanner = builder.setEnableAutoLoop(true)
56 | .setImageResIds(imageResIds)
57 | .setDotSelectedColor(Color.GREEN)
58 | .setHeightWidthRatio(0.5556f)
59 | .setLoopInterval(5000)
60 | .setEnableAutoLoop(true)
61 | .setIndicatorBackgroundColor(R.color.indicator_bg)
62 | .build();
63 | frameLayout.addView(funBanner);
64 | }
65 |
66 | # License #
67 | Copyright (c) 2017 uncleleonfan.
68 |
69 | Licensed under the Apache License, Version 2.0 (the "License");
70 | you may not use this file except in compliance with the License.
71 | You may obtain a copy of the License at
72 |
73 | http://www.apache.org/licenses/LICENSE-2.0
74 |
75 | Unless required by applicable law or agreed to in writing, software
76 | distributed under the License is distributed on an "AS IS" BASIS,
77 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78 | See the License for the specific language governing permissions and
79 | limitations under the License.
--------------------------------------------------------------------------------
/lib/src/main/java/com/leon/loopviewpagerlib/LoopPagerAdapterWrapper.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpagerlib;
2 |
3 |
4 | import android.os.Parcelable;
5 | import android.support.v4.view.PagerAdapter;
6 | import android.util.SparseArray;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | /**
11 | * A PagerAdapter wrapper responsible for providing a proper page to
12 | * LoopViewPager
13 | *
14 | * This class shouldn't be used directly
15 | */
16 | public class LoopPagerAdapterWrapper extends PagerAdapter {
17 |
18 | private PagerAdapter mAdapter;
19 |
20 | private SparseArray mToDestroy = new SparseArray();
21 |
22 | private boolean mBoundaryCaching;
23 |
24 | void setBoundaryCaching(boolean flag) {
25 | mBoundaryCaching = flag;
26 | }
27 |
28 | LoopPagerAdapterWrapper(PagerAdapter adapter) {
29 | this.mAdapter = adapter;
30 | }
31 |
32 | @Override
33 | public void notifyDataSetChanged() {
34 | mToDestroy = new SparseArray();
35 | super.notifyDataSetChanged();
36 | }
37 |
38 | int toRealPosition(int position) {
39 | int realCount = getRealCount();
40 | if (realCount == 0)
41 | return 0;
42 | int realPosition = (position-1) % realCount;
43 | if (realPosition < 0)
44 | realPosition += realCount;
45 |
46 | return realPosition;
47 | }
48 |
49 | public int toInnerPosition(int realPosition) {
50 | int position = (realPosition + 1);
51 | return position;
52 | }
53 |
54 | private int getRealFirstPosition() {
55 | return 1;
56 | }
57 |
58 | private int getRealLastPosition() {
59 | return getRealFirstPosition() + getRealCount() - 1;
60 | }
61 |
62 | @Override
63 | public int getCount() {
64 | return mAdapter.getCount() + 2;
65 | }
66 |
67 | public int getRealCount() {
68 | return mAdapter.getCount();
69 | }
70 |
71 |
72 | public PagerAdapter getRealAdapter() {
73 | return mAdapter;
74 | }
75 |
76 | @Override
77 | public Object instantiateItem(ViewGroup container, int position) {
78 | int realPosition = toRealPosition(position);
79 | if (mBoundaryCaching) {
80 | ToDestroy toDestroy = mToDestroy.get(position);
81 | if (toDestroy != null) {
82 | mToDestroy.remove(position);
83 | return toDestroy.object;
84 | }
85 | }
86 | return mAdapter.instantiateItem(container, realPosition);
87 | }
88 |
89 | @Override
90 | public void destroyItem(ViewGroup container, int position, Object object) {
91 | int realFirst = getRealFirstPosition();
92 | int realLast = getRealLastPosition();
93 | int realPosition = toRealPosition(position);
94 |
95 | if (mBoundaryCaching && (position == realFirst || position == realLast)) {
96 | mToDestroy.put(position, new ToDestroy(container, realPosition,
97 | object));
98 | } else {
99 | mAdapter.destroyItem(container, realPosition, object);
100 | }
101 | }
102 |
103 | /*
104 | * Delegate rest of methods directly to the inner adapter.
105 | */
106 |
107 | @Override
108 | public void finishUpdate(ViewGroup container) {
109 | mAdapter.finishUpdate(container);
110 | }
111 |
112 | @Override
113 | public boolean isViewFromObject(View view, Object object) {
114 | return mAdapter.isViewFromObject(view, object);
115 | }
116 |
117 | @Override
118 | public void restoreState(Parcelable bundle, ClassLoader classLoader) {
119 | mAdapter.restoreState(bundle, classLoader);
120 | }
121 |
122 | @Override
123 | public Parcelable saveState() {
124 | return mAdapter.saveState();
125 | }
126 |
127 | @Override
128 | public void startUpdate(ViewGroup container) {
129 | mAdapter.startUpdate(container);
130 | }
131 |
132 | @Override
133 | public void setPrimaryItem(ViewGroup container, int position, Object object) {
134 | mAdapter.setPrimaryItem(container, position, object);
135 | }
136 |
137 | /*
138 | * End delegation
139 | */
140 |
141 | /**
142 | * Container class for caching the boundary views
143 | */
144 | static class ToDestroy {
145 | ViewGroup container;
146 | int position;
147 | Object object;
148 |
149 | public ToDestroy(ViewGroup container, int position, Object object) {
150 | this.container = container;
151 | this.position = position;
152 | this.object = object;
153 | }
154 | }
155 |
156 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/leon/loopviewpagerlib/LoopViewPager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Leszek Mzyk
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.leon.loopviewpagerlib;
17 |
18 | import android.content.Context;
19 | import android.support.v4.view.PagerAdapter;
20 | import android.support.v4.view.ViewPager;
21 | import android.util.AttributeSet;
22 | import android.view.MotionEvent;
23 |
24 | public class LoopViewPager extends ViewPager {
25 |
26 | private static final String TAG = "LoopViewPager";
27 |
28 | private static final boolean DEFAULT_BOUNDARY_CASHING = true;
29 |
30 | OnPageChangeListener mOuterPageChangeListener;
31 | private LoopPagerAdapterWrapper mAdapter;
32 | private boolean mBoundaryCaching = DEFAULT_BOUNDARY_CASHING;
33 |
34 | private boolean mEnableAutoLoop;
35 | private int mLoopInterval = 3000;
36 |
37 | public void setEnableAutoLoop(boolean enableAutoLoop) {
38 | mEnableAutoLoop = enableAutoLoop;
39 | }
40 |
41 | public void setLoopInterval(int loopInterval) {
42 | mLoopInterval = loopInterval;
43 | }
44 |
45 |
46 | /**
47 | * helper function which may be used when implementing FragmentPagerAdapter
48 | *
49 | * @param position
50 | * @param count
51 | * @return (position-1)%count
52 | */
53 | public static int toRealPosition( int position, int count ){
54 | position = position-1;
55 | if( position < 0 ){
56 | position += count;
57 | }else{
58 | position = position%count;
59 | }
60 | return position;
61 | }
62 |
63 | /**
64 | * If set to true, the boundary views (i.e. first and last) will never be destroyed
65 | * This may help to prevent "blinking" of some views
66 | *
67 | * @param flag
68 | */
69 | public void setBoundaryCaching(boolean flag) {
70 | mBoundaryCaching = flag;
71 | if (mAdapter != null) {
72 | mAdapter.setBoundaryCaching(flag);
73 | }
74 | }
75 |
76 | @Override
77 | public void setAdapter(PagerAdapter adapter) {
78 | mAdapter = new LoopPagerAdapterWrapper(adapter);
79 | mAdapter.setBoundaryCaching(mBoundaryCaching);
80 | super.setAdapter(mAdapter);
81 | setCurrentItem(0, false);
82 | }
83 |
84 | @Override
85 | public PagerAdapter getAdapter() {
86 | return mAdapter != null ? mAdapter.getRealAdapter() : mAdapter;
87 | }
88 |
89 | @Override
90 | public int getCurrentItem() {
91 | return mAdapter != null ? mAdapter.toRealPosition(super.getCurrentItem()) : 0;
92 | }
93 |
94 |
95 | public void setCurrentItem(int item, boolean smoothScroll) {
96 | int realItem = mAdapter.toInnerPosition(item);
97 | super.setCurrentItem(realItem, smoothScroll);
98 | }
99 |
100 | @Override
101 | public void setCurrentItem(int item) {
102 | if (getCurrentItem() != item) {
103 | setCurrentItem(item, true);
104 | }
105 | }
106 |
107 | @Override
108 | public void setOnPageChangeListener(OnPageChangeListener listener) {
109 | mOuterPageChangeListener = listener;
110 | };
111 |
112 | public LoopViewPager(Context context) {
113 | super(context);
114 | init();
115 | }
116 |
117 | public LoopViewPager(Context context, AttributeSet attrs) {
118 | super(context, attrs);
119 | init();
120 | }
121 |
122 | private void init() {
123 | super.setOnPageChangeListener(onPageChangeListener);
124 | }
125 |
126 | private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
127 | private float mPreviousOffset = -1;
128 | private float mPreviousPosition = -1;
129 |
130 | @Override
131 | public void onPageSelected(int position) {
132 |
133 | int realPosition = mAdapter.toRealPosition(position);
134 | if (mPreviousPosition != realPosition) {
135 | mPreviousPosition = realPosition;
136 | if (mOuterPageChangeListener != null) {
137 | mOuterPageChangeListener.onPageSelected(realPosition);
138 | }
139 | }
140 | }
141 |
142 | @Override
143 | public void onPageScrolled(int position, float positionOffset,
144 | int positionOffsetPixels) {
145 | int realPosition = position;
146 | if (mAdapter == null) {
147 | return;
148 | }
149 | if (mAdapter != null) {
150 | realPosition = mAdapter.toRealPosition(position);
151 |
152 | if (positionOffset == 0
153 | && mPreviousOffset == 0
154 | && (position == 0 || position == mAdapter.getCount() - 1)) {
155 | setCurrentItem(realPosition, false);
156 | }
157 | }
158 |
159 | mPreviousOffset = positionOffset;
160 | if (mOuterPageChangeListener != null) {
161 | if (realPosition != mAdapter.getRealCount() - 1) {
162 | mOuterPageChangeListener.onPageScrolled(realPosition,
163 | positionOffset, positionOffsetPixels);
164 | } else {
165 | if (positionOffset > .5) {
166 | mOuterPageChangeListener.onPageScrolled(0, 0, 0);
167 | } else {
168 | mOuterPageChangeListener.onPageScrolled(realPosition,
169 | 0, 0);
170 | }
171 | }
172 | }
173 | }
174 |
175 | @Override
176 | public void onPageScrollStateChanged(int state) {
177 | if (mAdapter != null) {
178 | int position = LoopViewPager.super.getCurrentItem();
179 | int realPosition = mAdapter.toRealPosition(position);
180 | if (state == ViewPager.SCROLL_STATE_IDLE
181 | && (position == 0 || position == mAdapter.getCount() - 1)) {
182 | setCurrentItem(realPosition, false);
183 | }
184 | }
185 | if (mOuterPageChangeListener != null) {
186 | mOuterPageChangeListener.onPageScrollStateChanged(state);
187 | }
188 | }
189 | };
190 |
191 | private float mDownX;
192 |
193 | private float mDownY;
194 |
195 | public boolean onTouchEvent(MotionEvent ev) {
196 | switch (ev.getAction()) {
197 | case MotionEvent.ACTION_DOWN:
198 | mDownX = ev.getX();
199 | mDownY = ev.getY();
200 | stopLoop();
201 | break;
202 | case MotionEvent.ACTION_MOVE:
203 | float moveX = ev.getX();
204 | float moveY = ev.getY();
205 | float dx = Math.abs(mDownX - moveX);//x轴变化量
206 | float dy = Math.abs(mDownY - moveY);//y轴变化量
207 | if (dx > dy) {
208 | requestDisallowInterceptTouchEvent(true);
209 | }
210 | break;
211 | case MotionEvent.ACTION_UP:
212 | startLoop();
213 | break;
214 |
215 | }
216 |
217 | return super.onTouchEvent(ev);//super方法里面实现viewpager的拖动
218 | }
219 |
220 |
221 |
222 | private Runnable mLooper = new Runnable() {
223 | @Override
224 | public void run() {
225 | int next = getCurrentItem() + 1;
226 | setCurrentItem(next);
227 | startLoop();
228 | }
229 | };
230 |
231 | @Override
232 | protected void onAttachedToWindow() {
233 | super.onAttachedToWindow();
234 | startLoop();
235 | }
236 |
237 | private void startLoop() {
238 | //Fix issue that when user scroll FunBanner offscreen and then destroy activity, the onDetachedFromWindow will not call
239 | if (mEnableAutoLoop) {
240 | postDelayed(mLooper, mLoopInterval);
241 | }
242 | }
243 |
244 | @Override
245 | protected void onDetachedFromWindow() {
246 | super.onDetachedFromWindow();
247 | stopLoop();
248 | }
249 |
250 | public void stopLoop() {
251 | if (mEnableAutoLoop) {
252 | removeCallbacks(mLooper);
253 | }
254 | }
255 |
256 | }
257 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/leon/loopviewpagerlib/FunBanner.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpagerlib;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Color;
7 | import android.support.v4.view.PagerAdapter;
8 | import android.support.v4.view.ViewPager;
9 | import android.util.AttributeSet;
10 | import android.util.TypedValue;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.FrameLayout;
15 | import android.widget.ImageView;
16 | import android.widget.LinearLayout;
17 | import android.widget.TextView;
18 |
19 | import com.bumptech.glide.Glide;
20 |
21 | import java.util.Arrays;
22 | import java.util.List;
23 | import java.util.Stack;
24 |
25 | public class FunBanner extends FrameLayout {
26 | LoopViewPager mVp;
27 | CirclePageIndicator mCirclePageIndicator;
28 | TextView mTitle;
29 | FunBannerParams mFunBannerParams = new FunBannerParams();
30 | LinearLayout mIndicatorBar;
31 |
32 | private static class FunBannerParams {
33 | private boolean mEnableAutoLoop;
34 | private int mLoopInterval = 3000;
35 | private int mDotRadius = 0;
36 | private List mImageUrls;
37 | private List mTitles;
38 | private String mHost = "";
39 | private int[] mImagesResIds;
40 | private int mDotNormalColor;
41 | private int mDotSelectedColor;
42 | private boolean mShowIndicator = true;
43 | private int mIndicatorBarBackgroundColor;
44 | private float mHeightWidthRatio;
45 | private int mIndicatorBarHeight;
46 | private int mTitleColor;
47 | private int mHeight;
48 |
49 | public void apply(FunBanner funBanner) {
50 | funBanner.mFunBannerParams.mEnableAutoLoop = this.mEnableAutoLoop;
51 | funBanner.mFunBannerParams.mLoopInterval = this.mLoopInterval;
52 | if (this.mDotRadius > 0) {
53 | funBanner.mFunBannerParams.mDotRadius = this.mDotRadius;
54 | }
55 | if (this.mImageUrls != null) {
56 | funBanner.mFunBannerParams.mImageUrls = this.mImageUrls;
57 | }
58 | if (this.mHost.length() > 0) {
59 | funBanner.mFunBannerParams.mHost = this.mHost;
60 | }
61 | if (this.mImagesResIds != null) {
62 | funBanner.mFunBannerParams.mImagesResIds = this.mImagesResIds;
63 | }
64 | if (this.mDotNormalColor != 0) {
65 | funBanner.mFunBannerParams.mDotNormalColor = this.mDotNormalColor;
66 | }
67 | if (this.mDotSelectedColor != 0) {
68 | funBanner.mFunBannerParams.mDotSelectedColor = this.mDotSelectedColor;
69 | }
70 | funBanner.mFunBannerParams.mShowIndicator = this.mShowIndicator;
71 |
72 | if (this.mIndicatorBarBackgroundColor != 0) {
73 | funBanner.mFunBannerParams.mIndicatorBarBackgroundColor = this.mIndicatorBarBackgroundColor;
74 | }
75 | if (this.mHeightWidthRatio != 0) {
76 | funBanner.mFunBannerParams.mHeightWidthRatio = this.mHeightWidthRatio;
77 | }
78 | if (this.mIndicatorBarHeight != 0) {
79 | funBanner.mFunBannerParams.mIndicatorBarHeight = this.mIndicatorBarHeight;
80 | }
81 |
82 | if (this.mTitles != null) {
83 | funBanner.mFunBannerParams.mTitles = this.mTitles;
84 | }
85 |
86 | if (this.mTitleColor != 0) {
87 | funBanner.mFunBannerParams.mTitleColor = this.mTitleColor;
88 | }
89 |
90 | if (this.mHeight != 0) {
91 | funBanner.mFunBannerParams.mHeight = this.mHeight;
92 | }
93 | }
94 | }
95 |
96 |
97 | public FunBanner(Context context, AttributeSet attrs) {
98 | super(context, attrs);
99 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoopViewStyle);
100 | mFunBannerParams.mDotRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());
101 | mFunBannerParams.mDotRadius = a.getDimensionPixelSize(R.styleable.LoopViewStyle_dot_radius, mFunBannerParams.mDotRadius);
102 | mFunBannerParams.mEnableAutoLoop = a.getBoolean(R.styleable.LoopViewStyle_enable_auto_loop, false);
103 | mFunBannerParams.mLoopInterval = a.getInt(R.styleable.LoopViewStyle_loop_interval, mFunBannerParams.mLoopInterval);
104 | mFunBannerParams.mShowIndicator = a.getBoolean(R.styleable.LoopViewStyle_show_indicator, true);
105 | mFunBannerParams.mDotNormalColor = a.getColor(R.styleable.LoopViewStyle_dot_normal_color, Color.WHITE);
106 | mFunBannerParams.mDotSelectedColor = a.getColor(R.styleable.LoopViewStyle_dot_selected_color, Color.BLUE);
107 | mFunBannerParams.mIndicatorBarBackgroundColor = a.getColor(R.styleable.LoopViewStyle_indicator_bar_background_color, Color.TRANSPARENT);
108 | mFunBannerParams.mHeightWidthRatio = a.getFloat(R.styleable.LoopViewStyle_height_width_ratio, 0);
109 | mFunBannerParams.mIndicatorBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());
110 | mFunBannerParams.mIndicatorBarHeight = a.getDimensionPixelSize(R.styleable.LoopViewStyle_indicator_bar_height, mFunBannerParams.mIndicatorBarHeight);
111 | mFunBannerParams.mTitleColor = a.getColor(R.styleable.LoopViewStyle_title_color, Color.WHITE);
112 | a.recycle();
113 | init();
114 | }
115 |
116 | private FunBanner(Context context) {
117 | this(context, null);
118 | }
119 |
120 | private void init() {
121 | LayoutInflater.from(getContext()).inflate(R.layout.view_loop, this);
122 | }
123 |
124 | @Override
125 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
126 | if (mFunBannerParams.mHeightWidthRatio > 0) {
127 | int size = MeasureSpec.getSize(widthMeasureSpec);
128 | int height = (int) (size * mFunBannerParams.mHeightWidthRatio + 0.5);
129 | int changeHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
130 | super.onMeasure(widthMeasureSpec, changeHeightMeasureSpec);
131 | } else if (mFunBannerParams.mHeight > 0) {
132 | super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(mFunBannerParams.mHeight, MeasureSpec.EXACTLY));
133 | } else {
134 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
135 | }
136 | }
137 |
138 | private void initViewPager() {
139 | mVp = (LoopViewPager) findViewById(R.id.vp);
140 | mTitle = (TextView) findViewById(R.id.title);
141 | mIndicatorBar = (LinearLayout) findViewById(R.id.indicator_bar);
142 | mCirclePageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
143 |
144 | mVp.setEnableAutoLoop(mFunBannerParams.mEnableAutoLoop);
145 | mVp.setLoopInterval(mFunBannerParams.mLoopInterval);
146 | if (mFunBannerParams.mImageUrls != null || mFunBannerParams.mImagesResIds != null) {
147 | mVp.setAdapter(mPagerAdapter);
148 | if (mFunBannerParams.mShowIndicator) {
149 | mCirclePageIndicator.setViewPager(mVp);
150 | mCirclePageIndicator.setRadius(mFunBannerParams.mDotRadius);
151 | mCirclePageIndicator.setPageColor(mFunBannerParams.mDotNormalColor);
152 | mCirclePageIndicator.setFillColor(mFunBannerParams.mDotSelectedColor);
153 | mCirclePageIndicator.setOnPageChangeListener(mOnPageChangeListener);
154 | mIndicatorBar.setMinimumHeight(mFunBannerParams.mIndicatorBarHeight);
155 | mIndicatorBar.setBackgroundColor(mFunBannerParams.mIndicatorBarBackgroundColor);
156 |
157 | } else {
158 | mCirclePageIndicator.setVisibility(GONE);
159 | }
160 | }
161 |
162 | if (mFunBannerParams.mTitles != null) {
163 | mTitle.setVisibility(VISIBLE);
164 | mTitle.setText(mFunBannerParams.mTitles.get(0));
165 | mTitle.setTextColor(mFunBannerParams.mTitleColor);
166 | } else {
167 | mTitle.setVisibility(GONE);
168 | }
169 |
170 | }
171 |
172 | private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
173 | @Override
174 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
175 |
176 | }
177 |
178 | @Override
179 | public void onPageSelected(int position) {
180 | if (mFunBannerParams.mTitles != null) {
181 | mTitle.setText(mFunBannerParams.mTitles.get(position));
182 | }
183 | }
184 |
185 | @Override
186 | public void onPageScrollStateChanged(int state) {
187 |
188 | }
189 | };
190 |
191 | private PagerAdapter mPagerAdapter = new PagerAdapter() {
192 |
193 | private Stack mRecyclerViews = new Stack<>();
194 |
195 | @Override
196 | public int getCount() {
197 | if (mFunBannerParams.mImageUrls != null) {
198 | return mFunBannerParams.mImageUrls.size();
199 | }
200 | if (mFunBannerParams.mImagesResIds != null) {
201 | return mFunBannerParams.mImagesResIds.length;
202 | }
203 | return 0;
204 | }
205 |
206 | @Override
207 | public boolean isViewFromObject(View view, Object object) {
208 | return view == object;
209 | }
210 |
211 | @Override
212 | public Object instantiateItem(ViewGroup container, int position) {
213 | //Fix LoopViewPager onDetachFromWindow not called by ListView while sdk is below 25
214 | if (getContext() instanceof Activity) {
215 | if (((Activity) getContext()).isFinishing()) {
216 | mVp.stopLoop();
217 | return null;
218 | }
219 | }
220 | ImageView imageView = null;
221 | if (mRecyclerViews.isEmpty()) {
222 | //Cache is empty, create new ImageView
223 | imageView = new ImageView(getContext());
224 | } else {
225 | //Get ImageView from Cache
226 | imageView = (ImageView) mRecyclerViews.pop();
227 | }
228 | if (mFunBannerParams.mImageUrls == null) {
229 | imageView.setImageResource(mFunBannerParams.mImagesResIds[position]);
230 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
231 | } else {
232 | String url = mFunBannerParams.mHost + mFunBannerParams.mImageUrls.get(position);
233 | if (mFunBannerParams.mHeightWidthRatio > 0) {
234 | Glide.with(getContext()).load(url).into(imageView);
235 | } else {
236 | Glide.with(getContext()).load(url).centerCrop().into(imageView);
237 | }
238 | }
239 |
240 | container.addView(imageView);
241 | return imageView;
242 | }
243 |
244 | @Override
245 | public void destroyItem(ViewGroup container, int position, Object object) {
246 | container.removeView((View) object);
247 | mRecyclerViews.add((View) object);
248 | }
249 | };
250 |
251 | public void setImageUrls(List data) {
252 | mFunBannerParams.mImageUrls = data;
253 | initViewPager();
254 | }
255 |
256 | public void setImageUrlsAndTitles(List urls, List titles) {
257 | mFunBannerParams.mImageUrls = urls;
258 | mFunBannerParams.mTitles = titles;
259 | initViewPager();
260 | }
261 |
262 | public void setImageUrlsAndTitles(String[] urls, String[] titles) {
263 | mFunBannerParams.mImageUrls = Arrays.asList(urls);
264 | mFunBannerParams.mTitles = Arrays.asList(titles);
265 | initViewPager();
266 | }
267 |
268 | public void setImageUrls(String[] data) {
269 | List list = Arrays.asList(data);
270 | setImageUrls(list);
271 | }
272 |
273 | public void setImageUrlHost(String host) {
274 | mFunBannerParams.mHost = host;
275 | }
276 |
277 | public void setImageResIds(int[] resIds) {
278 | mFunBannerParams.mImagesResIds = resIds;
279 | initViewPager();
280 | }
281 |
282 | public static class Builder {
283 |
284 | private final FunBannerParams mFunBannerParams;
285 | private final Context mContext;
286 |
287 | public Builder(Context context) {
288 | mFunBannerParams = new FunBannerParams();
289 | mContext = context;
290 | }
291 |
292 | public Builder setEnableAutoLoop(boolean enableAutoLoop) {
293 | mFunBannerParams.mEnableAutoLoop = enableAutoLoop;
294 | return this;
295 | }
296 |
297 | public Builder setLoopInterval(int loopInterval) {
298 | mFunBannerParams.mLoopInterval = loopInterval;
299 | return this;
300 | }
301 |
302 | public Builder setDotRadius(int dotRadius) {
303 | mFunBannerParams.mDotRadius = dotRadius;
304 | return this;
305 | }
306 |
307 | public Builder setDotNormalColor(int dotNormalColor) {
308 | mFunBannerParams.mDotNormalColor = dotNormalColor;
309 | return this;
310 | }
311 |
312 | public Builder setDotSelectedColor(int dotSelectedColor) {
313 | mFunBannerParams.mDotSelectedColor = dotSelectedColor;
314 | return this;
315 | }
316 |
317 | public Builder setShowIndicator(boolean showIndicator) {
318 | mFunBannerParams.mShowIndicator = showIndicator;
319 | return this;
320 | }
321 |
322 |
323 | public Builder setIndicatorBackgroundColor(int indicatorBackgroundColor) {
324 | mFunBannerParams.mIndicatorBarBackgroundColor = indicatorBackgroundColor;
325 | return this;
326 | }
327 |
328 | public Builder setIndicatorBarHeight(int height) {
329 | mFunBannerParams.mIndicatorBarHeight = height;
330 | return this;
331 | }
332 |
333 | public Builder setHeightWidthRatio(float ratio) {
334 | mFunBannerParams.mHeightWidthRatio = ratio;
335 | return this;
336 | }
337 |
338 | public Builder setImageResIds(int[] resIds) {
339 | mFunBannerParams.mImagesResIds = resIds;
340 | return this;
341 | }
342 |
343 | public Builder setImageUrlHost(String host) {
344 | mFunBannerParams.mHost = host;
345 | return this;
346 | }
347 |
348 | public Builder setImageUrls(List data) {
349 | mFunBannerParams.mImageUrls = data;
350 | return this;
351 | }
352 |
353 | public Builder setImageUrls(String[] data) {
354 | List list = Arrays.asList(data);
355 | setImageUrls(list);
356 | return this;
357 | }
358 |
359 | public Builder setTitles(List titles) {
360 | mFunBannerParams.mTitles = titles;
361 | return this;
362 | }
363 |
364 | public Builder setTitles(String[] titles) {
365 | List list = Arrays.asList(titles);
366 | setTitles(list);
367 | return this;
368 | }
369 |
370 | public Builder setTitleColor(int color) {
371 | mFunBannerParams.mTitleColor = color;
372 | return this;
373 | }
374 |
375 | public Builder setHeight(int height) {
376 | mFunBannerParams.mHeight = height;
377 | return this;
378 | }
379 |
380 | public FunBanner build() {
381 | FunBanner funBanner = new FunBanner(mContext);
382 | mFunBannerParams.apply(funBanner);
383 | funBanner.initViewPager();
384 | return funBanner;
385 | }
386 | }
387 |
388 | }
389 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/leon/loopviewpagerlib/CirclePageIndicator.java:
--------------------------------------------------------------------------------
1 | package com.leon.loopviewpagerlib;
2 |
3 | /**
4 | * Created by Leon on 2017/1/22.
5 | */
6 |
7 | /*
8 | * Copyright (C) 2011 Patrik Akerfeldt
9 | * Copyright (C) 2011 Jake Wharton
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | */
23 |
24 | import android.content.Context;
25 | import android.content.res.Resources;
26 | import android.content.res.TypedArray;
27 | import android.graphics.Canvas;
28 | import android.graphics.Paint;
29 | import android.graphics.Paint.Style;
30 | import android.graphics.drawable.Drawable;
31 | import android.os.Parcel;
32 | import android.os.Parcelable;
33 | import android.support.v4.view.MotionEventCompat;
34 | import android.support.v4.view.ViewConfigurationCompat;
35 | import android.support.v4.view.ViewPager;
36 | import android.util.AttributeSet;
37 | import android.view.MotionEvent;
38 | import android.view.View;
39 | import android.view.ViewConfiguration;
40 |
41 | import static android.graphics.Paint.ANTI_ALIAS_FLAG;
42 | import static android.widget.LinearLayout.HORIZONTAL;
43 | import static android.widget.LinearLayout.VERTICAL;
44 |
45 | /**
46 | * Draws circles (one for each view). The current view position is filled and
47 | * others are only stroked.
48 | */
49 | public class CirclePageIndicator extends View implements PageIndicator {
50 | private static final int INVALID_POINTER = -1;
51 |
52 | private float mRadius;
53 | private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
54 | private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
55 | private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
56 | private ViewPager mViewPager;
57 | private ViewPager.OnPageChangeListener mListener;
58 | private int mCurrentPage;
59 | private int mSnapPage;
60 | private float mPageOffset;
61 | private int mScrollState;
62 | private int mOrientation;
63 | private boolean mCentered;
64 | private boolean mSnap;
65 |
66 | private int mTouchSlop;
67 | private float mLastMotionX = -1;
68 | private int mActivePointerId = INVALID_POINTER;
69 | private boolean mIsDragging;
70 |
71 |
72 | public CirclePageIndicator(Context context) {
73 | this(context, null);
74 | }
75 |
76 | public CirclePageIndicator(Context context, AttributeSet attrs) {
77 | super(context, attrs);
78 | if (isInEditMode()) return;
79 |
80 | //Load defaults from resources
81 | final Resources res = getResources();
82 | final int defaultPageColor = res.getColor(R.color.default_circle_indicator_page_color);
83 | final int defaultFillColor = res.getColor(R.color.default_circle_indicator_fill_color);
84 | final int defaultOrientation = res.getInteger(R.integer.default_circle_indicator_orientation);
85 | final int defaultStrokeColor = res.getColor(R.color.default_circle_indicator_stroke_color);
86 | final float defaultStrokeWidth = res.getDimension(R.dimen.default_circle_indicator_stroke_width);
87 | final float defaultRadius = res.getDimension(R.dimen.default_circle_indicator_radius);
88 | final boolean defaultCentered = res.getBoolean(R.bool.default_circle_indicator_centered);
89 | final boolean defaultSnap = res.getBoolean(R.bool.default_circle_indicator_snap);
90 |
91 | //Retrieve styles attributes
92 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator);
93 |
94 | mCentered = a.getBoolean(R.styleable.CirclePageIndicator_centered, defaultCentered);
95 | mOrientation = a.getInt(R.styleable.CirclePageIndicator_android_orientation, defaultOrientation);
96 | mPaintPageFill.setStyle(Style.FILL);
97 | mPaintPageFill.setColor(a.getColor(R.styleable.CirclePageIndicator_pageColor, defaultPageColor));
98 | mPaintStroke.setStyle(Style.STROKE);
99 | mPaintStroke.setColor(a.getColor(R.styleable.CirclePageIndicator_strokeColor, defaultStrokeColor));
100 | mPaintStroke.setStrokeWidth(a.getDimension(R.styleable.CirclePageIndicator_strokeWidth, defaultStrokeWidth));
101 | mPaintFill.setStyle(Style.FILL);
102 | mPaintFill.setColor(a.getColor(R.styleable.CirclePageIndicator_fillColor, defaultFillColor));
103 | mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius, defaultRadius);
104 | mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap);
105 |
106 | Drawable background = a.getDrawable(R.styleable.CirclePageIndicator_android_background);
107 | if (background != null) {
108 | setBackgroundDrawable(background);
109 | }
110 |
111 | a.recycle();
112 |
113 | final ViewConfiguration configuration = ViewConfiguration.get(context);
114 | mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
115 | }
116 |
117 |
118 | public void setCentered(boolean centered) {
119 | mCentered = centered;
120 | invalidate();
121 | }
122 |
123 | public boolean isCentered() {
124 | return mCentered;
125 | }
126 |
127 | public void setPageColor(int pageColor) {
128 | mPaintPageFill.setColor(pageColor);
129 | invalidate();
130 | }
131 |
132 | public int getPageColor() {
133 | return mPaintPageFill.getColor();
134 | }
135 |
136 | public void setFillColor(int fillColor) {
137 | mPaintFill.setColor(fillColor);
138 | invalidate();
139 | }
140 |
141 | public int getFillColor() {
142 | return mPaintFill.getColor();
143 | }
144 |
145 | public void setOrientation(int orientation) {
146 | switch (orientation) {
147 | case HORIZONTAL:
148 | case VERTICAL:
149 | mOrientation = orientation;
150 | requestLayout();
151 | break;
152 |
153 | default:
154 | throw new IllegalArgumentException("Orientation must be either HORIZONTAL or VERTICAL.");
155 | }
156 | }
157 |
158 | public int getOrientation() {
159 | return mOrientation;
160 | }
161 |
162 | public void setStrokeColor(int strokeColor) {
163 | mPaintStroke.setColor(strokeColor);
164 | invalidate();
165 | }
166 |
167 | public int getStrokeColor() {
168 | return mPaintStroke.getColor();
169 | }
170 |
171 | public void setStrokeWidth(float strokeWidth) {
172 | mPaintStroke.setStrokeWidth(strokeWidth);
173 | invalidate();
174 | }
175 |
176 | public float getStrokeWidth() {
177 | return mPaintStroke.getStrokeWidth();
178 | }
179 |
180 | public void setRadius(float radius) {
181 | mRadius = radius;
182 | invalidate();
183 | }
184 |
185 | public float getRadius() {
186 | return mRadius;
187 | }
188 |
189 | public void setSnap(boolean snap) {
190 | mSnap = snap;
191 | invalidate();
192 | }
193 |
194 | public boolean isSnap() {
195 | return mSnap;
196 | }
197 |
198 | @Override
199 | protected void onDraw(Canvas canvas) {
200 | super.onDraw(canvas);
201 |
202 | if (mViewPager == null) {
203 | return;
204 | }
205 | final int count = mViewPager.getAdapter().getCount();
206 | if (count == 0) {
207 | return;
208 | }
209 |
210 | if (mCurrentPage >= count) {
211 | setCurrentItem(count - 1);
212 | return;
213 | }
214 |
215 | int longSize;
216 | int longPaddingBefore;
217 | int longPaddingAfter;
218 | int shortPaddingBefore;
219 | if (mOrientation == HORIZONTAL) {
220 | longSize = getWidth();
221 | longPaddingBefore = getPaddingLeft();
222 | longPaddingAfter = getPaddingRight();
223 | shortPaddingBefore = getPaddingTop();
224 | } else {
225 | longSize = getHeight();
226 | longPaddingBefore = getPaddingTop();
227 | longPaddingAfter = getPaddingBottom();
228 | shortPaddingBefore = getPaddingLeft();
229 | }
230 |
231 | final float threeRadius = mRadius * 3;
232 | final float shortOffset = shortPaddingBefore + mRadius;
233 | float longOffset = longPaddingBefore + mRadius;
234 | if (mCentered) {
235 | longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
236 | }
237 |
238 | float dX;
239 | float dY;
240 |
241 | float pageFillRadius = mRadius;
242 | if (mPaintStroke.getStrokeWidth() > 0) {
243 | pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f;
244 | }
245 |
246 | //Draw stroked circles
247 | for (int iLoop = 0; iLoop < count; iLoop++) {
248 | float drawLong = longOffset + (iLoop * threeRadius);
249 | if (mOrientation == HORIZONTAL) {
250 | dX = drawLong;
251 | dY = shortOffset;
252 | } else {
253 | dX = shortOffset;
254 | dY = drawLong;
255 | }
256 | // Only paint fill if not completely transparent
257 | if (mPaintPageFill.getAlpha() > 0) {
258 | canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
259 | }
260 |
261 | // Only paint stroke if a stroke width was non-zero
262 | if (pageFillRadius != mRadius) {
263 | canvas.drawCircle(dX, dY, mRadius, mPaintStroke);
264 | }
265 | }
266 |
267 | //Draw the filled circle according to the current scroll
268 | float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
269 | if (!mSnap) {
270 | cx += mPageOffset * threeRadius;
271 | }
272 | if (mOrientation == HORIZONTAL) {
273 | dX = longOffset + cx;
274 | dY = shortOffset;
275 | } else {
276 | dX = shortOffset;
277 | dY = longOffset + cx;
278 | }
279 | canvas.drawCircle(dX, dY, mRadius, mPaintFill);
280 | }
281 |
282 | public boolean onTouchEvent(android.view.MotionEvent ev) {
283 | if (super.onTouchEvent(ev)) {
284 | return true;
285 | }
286 | if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
287 | return false;
288 | }
289 |
290 | final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
291 | switch (action) {
292 | case MotionEvent.ACTION_DOWN:
293 | mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
294 | mLastMotionX = ev.getX();
295 | break;
296 |
297 | case MotionEvent.ACTION_MOVE: {
298 | final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
299 | final float x = MotionEventCompat.getX(ev, activePointerIndex);
300 | final float deltaX = x - mLastMotionX;
301 |
302 | if (!mIsDragging) {
303 | if (Math.abs(deltaX) > mTouchSlop) {
304 | mIsDragging = true;
305 | }
306 | }
307 |
308 | if (mIsDragging) {
309 | mLastMotionX = x;
310 | if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
311 | mViewPager.fakeDragBy(deltaX);
312 | }
313 | }
314 |
315 | break;
316 | }
317 |
318 | case MotionEvent.ACTION_CANCEL:
319 | case MotionEvent.ACTION_UP:
320 | if (!mIsDragging) {
321 | final int count = mViewPager.getAdapter().getCount();
322 | final int width = getWidth();
323 | final float halfWidth = width / 2f;
324 | final float sixthWidth = width / 6f;
325 |
326 | if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
327 | if (action != MotionEvent.ACTION_CANCEL) {
328 | mViewPager.setCurrentItem(mCurrentPage - 1);
329 | }
330 | return true;
331 | } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
332 | if (action != MotionEvent.ACTION_CANCEL) {
333 | mViewPager.setCurrentItem(mCurrentPage + 1);
334 | }
335 | return true;
336 | }
337 | }
338 |
339 | mIsDragging = false;
340 | mActivePointerId = INVALID_POINTER;
341 | if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
342 | break;
343 |
344 | case MotionEventCompat.ACTION_POINTER_DOWN: {
345 | final int index = MotionEventCompat.getActionIndex(ev);
346 | mLastMotionX = MotionEventCompat.getX(ev, index);
347 | mActivePointerId = MotionEventCompat.getPointerId(ev, index);
348 | break;
349 | }
350 |
351 | case MotionEventCompat.ACTION_POINTER_UP:
352 | final int pointerIndex = MotionEventCompat.getActionIndex(ev);
353 | final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
354 | if (pointerId == mActivePointerId) {
355 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
356 | mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
357 | }
358 | mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
359 | break;
360 | }
361 |
362 | return true;
363 | }
364 |
365 | @Override
366 | public void setViewPager(ViewPager view) {
367 | if (mViewPager == view) {
368 | return;
369 | }
370 | if (mViewPager != null) {
371 | mViewPager.setOnPageChangeListener(null);
372 | }
373 | if (view.getAdapter() == null) {
374 | throw new IllegalStateException("ViewPager does not have adapter instance.");
375 | }
376 | mViewPager = view;
377 | mViewPager.setOnPageChangeListener(this);
378 | invalidate();
379 | }
380 |
381 | @Override
382 | public void setViewPager(ViewPager view, int initialPosition) {
383 | setViewPager(view);
384 | setCurrentItem(initialPosition);
385 | }
386 |
387 | @Override
388 | public void setCurrentItem(int item) {
389 | if (mViewPager == null) {
390 | throw new IllegalStateException("ViewPager has not been bound.");
391 | }
392 | mViewPager.setCurrentItem(item);
393 | mCurrentPage = item;
394 | invalidate();
395 | }
396 |
397 | @Override
398 | public void notifyDataSetChanged() {
399 | invalidate();
400 | }
401 |
402 | @Override
403 | public void onPageScrollStateChanged(int state) {
404 | mScrollState = state;
405 |
406 | if (mListener != null) {
407 | mListener.onPageScrollStateChanged(state);
408 | }
409 | }
410 |
411 | @Override
412 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
413 | mCurrentPage = position;
414 | mPageOffset = positionOffset;
415 | invalidate();
416 |
417 | if (mListener != null) {
418 | mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
419 | }
420 | }
421 |
422 | @Override
423 | public void onPageSelected(int position) {
424 | if (mSnap || mScrollState == ViewPager.SCROLL_STATE_IDLE) {
425 | mCurrentPage = position;
426 | mSnapPage = position;
427 | invalidate();
428 | }
429 |
430 | if (mListener != null) {
431 | mListener.onPageSelected(position);
432 | }
433 | }
434 |
435 | @Override
436 | public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
437 | mListener = listener;
438 | }
439 |
440 | /*
441 | * (non-Javadoc)
442 | *
443 | * @see android.view.View#onMeasure(int, int)
444 | */
445 | @Override
446 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
447 | if (mOrientation == HORIZONTAL) {
448 | setMeasuredDimension(measureLong(widthMeasureSpec), measureShort(heightMeasureSpec));
449 | } else {
450 | setMeasuredDimension(measureShort(widthMeasureSpec), measureLong(heightMeasureSpec));
451 | }
452 | }
453 |
454 | /**
455 | * Determines the width of this view
456 | *
457 | * @param measureSpec A measureSpec packed into an int
458 | * @return The width of the view, honoring constraints from measureSpec
459 | */
460 | private int measureLong(int measureSpec) {
461 | int result;
462 | int specMode = MeasureSpec.getMode(measureSpec);
463 | int specSize = MeasureSpec.getSize(measureSpec);
464 |
465 | if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
466 | //We were told how big to be
467 | result = specSize;
468 | } else {
469 | //Calculate the width according the views count
470 | final int count = mViewPager.getAdapter().getCount();
471 | result = (int) (getPaddingLeft() + getPaddingRight()
472 | + (count * 2 * mRadius) + (count - 1) * mRadius + 1);
473 | //Respect AT_MOST value if that was what is called for by measureSpec
474 | if (specMode == MeasureSpec.AT_MOST) {
475 | result = Math.min(result, specSize);
476 | }
477 | }
478 | return result;
479 | }
480 |
481 | /**
482 | * Determines the height of this view
483 | *
484 | * @param measureSpec A measureSpec packed into an int
485 | * @return The height of the view, honoring constraints from measureSpec
486 | */
487 | private int measureShort(int measureSpec) {
488 | int result;
489 | int specMode = MeasureSpec.getMode(measureSpec);
490 | int specSize = MeasureSpec.getSize(measureSpec);
491 |
492 | if (specMode == MeasureSpec.EXACTLY) {
493 | //We were told how big to be
494 | result = specSize;
495 | } else {
496 | //Measure the height
497 | result = (int) (2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
498 | //Respect AT_MOST value if that was what is called for by measureSpec
499 | if (specMode == MeasureSpec.AT_MOST) {
500 | result = Math.min(result, specSize);
501 | }
502 | }
503 | return result;
504 | }
505 |
506 | @Override
507 | public void onRestoreInstanceState(Parcelable state) {
508 | SavedState savedState = (SavedState) state;
509 | super.onRestoreInstanceState(savedState.getSuperState());
510 | mCurrentPage = savedState.currentPage;
511 | mSnapPage = savedState.currentPage;
512 | requestLayout();
513 | }
514 |
515 | @Override
516 | public Parcelable onSaveInstanceState() {
517 | Parcelable superState = super.onSaveInstanceState();
518 | SavedState savedState = new SavedState(superState);
519 | savedState.currentPage = mCurrentPage;
520 | return savedState;
521 | }
522 |
523 | static class SavedState extends BaseSavedState {
524 | int currentPage;
525 |
526 | public SavedState(Parcelable superState) {
527 | super(superState);
528 | }
529 |
530 | private SavedState(Parcel in) {
531 | super(in);
532 | currentPage = in.readInt();
533 | }
534 |
535 | @Override
536 | public void writeToParcel(Parcel dest, int flags) {
537 | super.writeToParcel(dest, flags);
538 | dest.writeInt(currentPage);
539 | }
540 |
541 | @SuppressWarnings("UnusedDeclaration")
542 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
543 | @Override
544 | public SavedState createFromParcel(Parcel in) {
545 | return new SavedState(in);
546 | }
547 |
548 | @Override
549 | public SavedState[] newArray(int size) {
550 | return new SavedState[size];
551 | }
552 | };
553 | }
554 | }
--------------------------------------------------------------------------------