├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── fx_bztj.png │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── sk_search_icon.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── 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-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable │ │ │ │ ├── abc_bg_search.xml │ │ │ │ ├── abc_bg_bookstack.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── list_item_cover.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── activity_carousel.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── icool │ │ │ └── carousel3 │ │ │ ├── CarouselImageView.java │ │ │ ├── CarouselAdapter.java │ │ │ ├── MainActivity.java │ │ │ └── Carousel3Activity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── icool │ │ │ └── carousel3 │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── icool │ │ └── carousel3 │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── carousel-lib ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ └── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── icool │ │ │ └── carousel_lib │ │ │ ├── CarouselRecyclerView.java │ │ │ └── CarouselLayout.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── icool │ │ │ └── carousel_lib │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── icool │ │ └── carousel_lib │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /carousel-lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':carousel-lib', ':carousel-lib' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /carousel-lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | carousel-lib 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/fx_bztj.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xxhdpi/fx_bztj.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/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/sugarkawhi/Carousel3/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/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/sk_search_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xxhdpi/sk_search_icon.png -------------------------------------------------------------------------------- /carousel-lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarkawhi/Carousel3/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/sugarkawhi/Carousel3/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Carousel3 3 | 2018.09.17 4 | 5 | -------------------------------------------------------------------------------- /.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 -------------------------------------------------------------------------------- /app/src/main/res/drawable/abc_bg_search.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/abc_bg_bookstack.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 17 16:52:37 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/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/layout/list_item_cover.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /carousel-lib/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/icool/carousel3/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 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 | } -------------------------------------------------------------------------------- /carousel-lib/src/test/java/com/icool/carousel_lib/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel_lib; 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 | } -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /carousel-lib/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/icool/carousel3/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 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.icool.carousel3", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /carousel-lib/src/androidTest/java/com/icool/carousel_lib/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel_lib; 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.icool.carousel_lib.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /carousel-lib/src/main/java/com/icool/carousel_lib/CarouselRecyclerView.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel_lib; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.AttributeSet; 7 | import android.view.MotionEvent; 8 | 9 | /** 10 | * @author zhzy 11 | * created on 2018/9/18. 12 | */ 13 | public class CarouselRecyclerView extends RecyclerView { 14 | public CarouselRecyclerView(Context context) { 15 | super(context); 16 | } 17 | 18 | public CarouselRecyclerView(Context context, @Nullable AttributeSet attrs) { 19 | super(context, attrs); 20 | } 21 | 22 | public CarouselRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | } 25 | 26 | 27 | @Override 28 | public boolean onTouchEvent(MotionEvent e) { 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /carousel-lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 19 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(include: ['*.jar'], dir: 'libs') 29 | implementation 'com.android.support:appcompat-v7:28.0.0-rc02' 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 33 | compile 'com.android.support:recyclerview-v7:27.1.0' 34 | } 35 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.icool.carousel3" 7 | minSdkVersion 19 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:28.0.0-rc02' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | implementation 'com.squareup.picasso:picasso:2.71828' 29 | /** immer **/ 30 | implementation 'com.gyf.immersionbar:immersionbar:2.3.1' 31 | implementation project(':carousel-lib') 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/icool/carousel3/CarouselImageView.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.util.AttributeSet; 6 | import android.widget.ImageView; 7 | 8 | /** 9 | * @author zhzy 10 | * created on 2018/9/18. 11 | */ 12 | public class CarouselImageView extends ImageView { 13 | public static final float BOOK_COVER_RATIO = 1.36627907f; 14 | 15 | public CarouselImageView(Context context) { 16 | this(context, null); 17 | } 18 | 19 | public CarouselImageView(Context context, @Nullable AttributeSet attrs) { 20 | this(context, attrs, 0); 21 | } 22 | 23 | public CarouselImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 24 | super(context, attrs, defStyleAttr); 25 | } 26 | 27 | 28 | @Override 29 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 30 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); 31 | int heightSize; 32 | switch (heightMode) { 33 | case MeasureSpec.AT_MOST: 34 | heightSize = 471 / 2; 35 | break; 36 | case MeasureSpec.EXACTLY: 37 | case MeasureSpec.UNSPECIFIED: 38 | heightSize = MeasureSpec.getSize(heightMeasureSpec); 39 | break; 40 | default: 41 | heightSize = 471; 42 | break; 43 | } 44 | int widthSize = (int) (heightSize / BOOK_COVER_RATIO); 45 | setMeasuredDimension(widthSize, heightSize); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/icool/carousel3/CarouselAdapter.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.Log; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.Toast; 11 | 12 | import com.squareup.picasso.Picasso; 13 | 14 | /** 15 | * @author zhzy 16 | * created on 2018/9/18. 17 | */ 18 | public class CarouselAdapter extends RecyclerView.Adapter { 19 | 20 | private static final String TAG = "CarouselAdapter"; 21 | private String[] mSources; 22 | 23 | public CarouselAdapter(String[] sources) { 24 | mSources = sources; 25 | } 26 | 27 | @NonNull 28 | @Override 29 | public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 30 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_cover, parent, false); 31 | return new ItemHolder(view); 32 | } 33 | 34 | @Override 35 | public void onBindViewHolder(@NonNull final ItemHolder holder, final int position) { 36 | String url = mSources[position % mSources.length]; 37 | Picasso.get().load(url).placeholder(R.color.colorAccent).into(holder.mImageView); 38 | holder.itemView.setOnClickListener(new View.OnClickListener() { 39 | @Override 40 | public void onClick(View v) { 41 | Log.i(TAG, "position = " + position); 42 | } 43 | }); 44 | } 45 | 46 | @Override 47 | public int getItemCount() { 48 | return Integer.MAX_VALUE; 49 | } 50 | 51 | static class ItemHolder extends RecyclerView.ViewHolder { 52 | CarouselImageView mImageView; 53 | 54 | public ItemHolder(View itemView) { 55 | super(itemView); 56 | mImageView = itemView.findViewById(R.id.image); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /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 | 10 | 11 | 23 | 24 | 34 | 35 | 42 | 43 | 49 | 50 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /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/icool/carousel3/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import com.gyf.barlibrary.ImmersionBar; 9 | import com.icool.carousel_lib.CarouselLayout; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | public static final String[] RES_1 = new String[]{ 14 | "https://img.xhhread.cn/images/covers/120/20171212160717948317.jpg", 15 | "https://img.xhhread.cn/images/covers/120/20171114170340861.jpg", 16 | "https://img.xhhread.cn/images/covers/120/20170908114858367.jpg", 17 | "https://img.xhhread.cn/images/covers/120/20171129151704679316.jpg", 18 | "https://img.xhhread.cn/images/covers/20170524190259477.png" 19 | }; 20 | public static final String[] RES_2 = new String[]{ 21 | "https://img.xhhread.cn/images/covers/120/20180719162856280497.jpg", 22 | "https://img.xhhread.cn/images/covers/120/20180211151726743903.jpg", 23 | "https://img.xhhread.cn/images/covers/20161218174211850.png", 24 | "https://img.xhhread.cn/images/covers/120/20171103182048278.jpg", 25 | "https://img.xhhread.cn/images/covers/120/20171121104914447.jpg" 26 | }; 27 | public static final String[] RES_3 = new String[]{ 28 | "https://img.xhhread.cn/images/covers/120/20171102153743559.jpg", 29 | "https://img.xhhread.cn/images/covers/120/20161116184433248.jpg", 30 | "https://img.xhhread.cn/images/covers/20170402001402658.png", 31 | "https://img.xhhread.cn/images/covers/120/20170330215335077.jpg", 32 | "https://img.xhhread.cn/images/covers/20170321195717153.png" 33 | }; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_main); 39 | ImmersionBar.with(this) 40 | .statusBarDarkFont(true) 41 | .fitsSystemWindows(false) 42 | .transparentStatusBar() 43 | .init(); 44 | findViewById(R.id.tv_search) 45 | .setOnClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View v) { 48 | Intent intent = new Intent(MainActivity.this, Carousel3Activity.class); 49 | startActivity(intent); 50 | } 51 | }); 52 | 53 | CarouselLayout carouselLayout = findViewById(R.id.carousel); 54 | 55 | CarouselAdapter adapter1 = new CarouselAdapter(RES_1); 56 | CarouselAdapter adapter2 = new CarouselAdapter(RES_2); 57 | CarouselAdapter adapter3 = new CarouselAdapter(RES_3); 58 | 59 | carouselLayout.setAdapter(adapter1, adapter2, adapter3); 60 | 61 | carouselLayout.start(); 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/icool/carousel3/Carousel3Activity.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel3; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.RadioGroup; 8 | 9 | import com.icool.carousel_lib.CarouselLayout; 10 | 11 | /** 12 | * @author zhzy 13 | * created on 2018/9/18. 14 | */ 15 | public class Carousel3Activity extends AppCompatActivity { 16 | 17 | private CarouselLayout carouselLayout; 18 | 19 | @Override 20 | protected void onCreate(@Nullable Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_carousel); 23 | carouselLayout = findViewById(R.id.carousel); 24 | CarouselAdapter adapter1 = new CarouselAdapter(MainActivity.RES_1); 25 | CarouselAdapter adapter2 = new CarouselAdapter(MainActivity.RES_2); 26 | CarouselAdapter adapter3 = new CarouselAdapter(MainActivity.RES_3); 27 | carouselLayout.setAdapter(adapter1, adapter2, adapter3); 28 | 29 | init(); 30 | } 31 | 32 | private void init() { 33 | RadioGroup rg_speed = findViewById(R.id.rg_speed); 34 | rg_speed.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 35 | @Override 36 | public void onCheckedChanged(RadioGroup group, int checkedId) { 37 | switch (checkedId) { 38 | case R.id.speed_1: 39 | carouselLayout.setSpeed(1); 40 | break; 41 | case R.id.speed_2: 42 | carouselLayout.setSpeed(2); 43 | break; 44 | case R.id.speed_3: 45 | carouselLayout.setSpeed(3); 46 | break; 47 | case R.id.speed_4: 48 | carouselLayout.setSpeed(4); 49 | break; 50 | } 51 | } 52 | }); 53 | RadioGroup rg_angle = findViewById(R.id.rg_angle); 54 | rg_angle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 55 | @Override 56 | public void onCheckedChanged(RadioGroup group, int checkedId) { 57 | switch (checkedId) { 58 | case R.id.angle_30: 59 | carouselLayout.setAngle(-30); 60 | break; 61 | case R.id.angle_45: 62 | carouselLayout.setAngle(-45); 63 | break; 64 | case R.id.angle_60: 65 | carouselLayout.setAngle(-60); 66 | break; 67 | } 68 | } 69 | }); 70 | 71 | RadioGroup rg_spacing = findViewById(R.id.rg_spacing); 72 | rg_spacing.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 73 | @Override 74 | public void onCheckedChanged(RadioGroup group, int checkedId) { 75 | switch (checkedId) { 76 | case R.id.spacing_20: 77 | carouselLayout.setGapSpacing(20); 78 | break; 79 | case R.id.spacing_40: 80 | carouselLayout.setGapSpacing(40); 81 | break; 82 | case R.id.spacing_60: 83 | carouselLayout.setGapSpacing(60); 84 | break; 85 | } 86 | } 87 | }); 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Carousel3 2 | 微信读书 本周推送的类似传送带View的具体实现 3 | ### 实现效果 4 | 5 | ![](https://ws1.sinaimg.cn/large/92d4722bly1fvemr1qgvgg20as0j6b2c.jpg) 6 | 7 | ### 使用 8 | 9 | ``` 10 | 17 | 18 | ``` 19 | 20 | 只需要通过设置Adapter就OK了 21 | ``` 22 | public void setAdapter(RecyclerView.Adapter adapter1, RecyclerView.Adapter adapter2, RecyclerView.Adapter adapter3) { 23 | mRv1.setAdapter(adapter1); 24 | mRv2.setAdapter(adapter2); 25 | mRv3.setAdapter(adapter3); 26 | } 27 | ``` 28 | 29 | 30 | 31 | #### 属性说明 32 | 33 | | 属性值 | 说明 | 值| 34 | |:---------------:|:-------:|:--:| 35 | | carousel_angle | 倾斜角度 | 默认-30°| 36 | | carousel_spacing | 列表之间的间隙,通常设置为recyclerView的item间距大小一致 |dp| 37 | | carousel_speed | 速度,值越大传送越快,不小于0 | 默认1| 38 | 39 | 1. 可在代码中设置间隙 `setGapSpacing` 40 | 2. 代码中设置角度 `setAngle` 41 | 3. 代码中设置速度 `setSpeed` 42 | 43 | ### 需求分析 44 | 45 | + 直观有 三条传送带式列表 46 | + 一个正向移动 两个反向移动 47 | + 有一个倾斜角度 48 | + 可以循环展示 49 | 50 | ### 具体分析 51 | 52 | + 根据样式 可以确定的是需要自定义ViewGroup来实现 53 | + 结合列表的正向反向移动 可以确定:`RecyclerView` + `LinearLayoutManager` 可以做到 54 | + 不停滚动借助 `Scroller` 来实现 55 | + 倾斜角度 可以通过 `setRatation()` 方法来旋转一个角度 56 | + 循环展示 通过设置 `RecyclerView.Adapter`的 `itemCount` 为 `Inter.MAX_VALUE` 57 | 58 | ### 具体实现 59 | 60 | #### 自定义`CarouselLayout`继承自`ViewGroup` 61 | 添加一个子View `LinearLayout`, `setOrientation(LinearLayout.VERTICAL);` 62 | 依次添加三个 `RecyclerView`,设置其 `marginTop`为 `gapSpacing`的值 63 | 64 | ``` 65 | mContainer = new LinearLayout(getContext()); 66 | mContainer.setOrientation(LinearLayout.VERTICAL); 67 | addView(mContainer, generateDefaultLayoutParams()); 68 | mRv1 = new CarouselRecyclerView(getContext()); 69 | mRv2 = new CarouselRecyclerView(getContext()); 70 | mRv3 = new CarouselRecyclerView(getContext()); 71 | mContainer.addView(mRv1); 72 | mContainer.addView(mRv2); 73 | mContainer.addView(mRv3); 74 | setSpacing();//此方法设置margin,详见代码 75 | 76 | mRv1.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); 77 | mRv2.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, true)); 78 | mRv3.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); 79 | 80 | ``` 81 | 82 | #### 旋转一个角度 83 | 84 | 设置 `LinearLayout` 的rotation 85 | 86 | ``` 87 | mContainer.setRotation(mAngle);//旋转角度 88 | 89 | ``` 90 | 91 | #### 设置`LinearLayout`的大小来保证切斜后仍可以占满全屏 92 | 由于在ViewGroup中最大的距离就是对角线,所以 设置 子View的宽高都为对角线的长度 93 | 94 | ``` 95 | //对角线长度 96 | mDiagonalLine = (int) Math.sqrt(getMeasuredWidth() * getMeasuredWidth() + getMeasuredHeight() * getMeasuredHeight()); 97 | ViewGroup.LayoutParams params = mContainer.getLayoutParams(); 98 | params.width = mDiagonalLine; 99 | params.height = mDiagonalLine; 100 | mContainer.setLayoutParams(params); 101 | 102 | ``` 103 | 104 | #### 移动起来 105 | 借助Scroller类来不断 调用 `computeScroll`方法实现滚动 106 | ``` 107 | @Override 108 | public void computeScroll() { 109 | super.computeScroll(); 110 | mRv1.scrollBy(mSpeed, 0);//speed 对应移动像素值 111 | mRv2.scrollBy(-mSpeed, 0); 112 | mRv3.scrollBy(mSpeed, 0); 113 | if (mScroller.isFinished()) { 114 | start(); 115 | } 116 | } 117 | 118 | ``` 119 | 120 | #### 其他 121 | 122 | 1. 因为列表使用`RecyclerView`实现,所以我们手动还可以滑动它。 123 | 如果不想手动滑动的话,重写`RecyclerView`的`onTouchEvent`方法, ` return false;` 124 | 2. 无限循环 125 | 设置Adapter的时候 126 | ``` 127 | @Override 128 | public int getItemCount() { 129 | return Integer.MAX_VALUE; 130 | } 131 | 132 | ``` 133 | 然后在 `onBindViewHolder` 方法取item的时候 进行取余操作 134 | ``` 135 | String url = mSources[position % mSources.length]; 136 | ``` 137 | 138 | ### 后记 139 | 140 | 这个效果在微信阅读上是WebView实现的,我们的UI直接抄了过来。所以只能用Android代码实现一下。 141 | 如果有更好的实现方式,或者需要改进的地方,希望可以一起探讨。 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /carousel-lib/src/main/java/com/icool/carousel_lib/CarouselLayout.java: -------------------------------------------------------------------------------- 1 | package com.icool.carousel_lib; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.AttributeSet; 8 | import android.util.Log; 9 | import android.view.ViewGroup; 10 | import android.widget.LinearLayout; 11 | import android.widget.Scroller; 12 | 13 | /** 14 | * @author zhzy 15 | * created on 2018/9/17. 16 | */ 17 | public class CarouselLayout extends ViewGroup { 18 | private static final String TAG = "CarouselLayout"; 19 | 20 | //边距 21 | private int mGapSpacing; 22 | //角度e 23 | private float mAngle; 24 | //速度 25 | private int mSpeed; 26 | //滚动辅助类 27 | private Scroller mScroller; 28 | 29 | private LinearLayout mContainer; 30 | private RecyclerView mRv1; 31 | private RecyclerView mRv2; 32 | private RecyclerView mRv3; 33 | private int mDiagonalLine; 34 | 35 | 36 | public CarouselLayout(Context context) { 37 | this(context, null); 38 | } 39 | 40 | public CarouselLayout(Context context, AttributeSet attrs) { 41 | this(context, attrs, 0); 42 | } 43 | 44 | public CarouselLayout(Context context, AttributeSet attrs, int defStyleAttr) { 45 | super(context, attrs, defStyleAttr); 46 | TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CarouselLayout); 47 | mGapSpacing = array.getDimensionPixelSize(R.styleable.CarouselLayout_carousel_spacing, 20); 48 | mAngle = array.getFloat(R.styleable.CarouselLayout_carousel_angle, -30); 49 | mSpeed = array.getInt(R.styleable.CarouselLayout_carousel_speed, 1); 50 | array.recycle(); 51 | mScroller = new Scroller(getContext()); 52 | init(); 53 | } 54 | 55 | @Override 56 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 57 | int width = MeasureSpec.getSize(widthMeasureSpec); 58 | int height = MeasureSpec.getSize(heightMeasureSpec); 59 | measureChildren(widthMeasureSpec, heightMeasureSpec); 60 | setMeasuredDimension(width, height); 61 | mDiagonalLine = (int) Math.sqrt(getMeasuredWidth() * getMeasuredWidth() + getMeasuredHeight() * getMeasuredHeight()); 62 | ViewGroup.LayoutParams params = mContainer.getLayoutParams(); 63 | params.width = mDiagonalLine; 64 | params.height = mDiagonalLine; 65 | mContainer.setLayoutParams(params); 66 | } 67 | 68 | @Override 69 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 70 | int left = -(mDiagonalLine - getMeasuredWidth()) / 2; 71 | int top = -(mDiagonalLine - getMeasuredHeight()) / 2; 72 | int right = left + mDiagonalLine; 73 | int bottom = top + mDiagonalLine; 74 | mContainer.layout(left, top, right, bottom); 75 | } 76 | 77 | 78 | @Override 79 | public void computeScroll() { 80 | super.computeScroll(); 81 | mRv1.scrollBy(mSpeed, 0); 82 | mRv2.scrollBy(-mSpeed, 0); 83 | mRv3.scrollBy(mSpeed, 0); 84 | if (mScroller.isFinished()) { 85 | Log.i(TAG, "Scroller finished"); 86 | start(); 87 | } 88 | invalidate(); 89 | } 90 | 91 | public void init() { 92 | mContainer = new LinearLayout(getContext()); 93 | mContainer.setOrientation(LinearLayout.VERTICAL); 94 | addView(mContainer, generateDefaultLayoutParams()); 95 | mRv1 = new CarouselRecyclerView(getContext()); 96 | mRv2 = new CarouselRecyclerView(getContext()); 97 | mRv3 = new CarouselRecyclerView(getContext()); 98 | mContainer.addView(mRv1); 99 | mContainer.addView(mRv2); 100 | mContainer.addView(mRv3); 101 | setSpacing(); 102 | 103 | 104 | mRv1.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); 105 | mRv2.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, true)); 106 | mRv3.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); 107 | 108 | mContainer.setRotation(mAngle); 109 | } 110 | 111 | private void setSpacing() { 112 | LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1); 113 | lp1.topMargin = mGapSpacing; 114 | mRv1.setLayoutParams(lp1); 115 | 116 | LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1); 117 | lp2.topMargin = mGapSpacing; 118 | mRv2.setLayoutParams(lp2); 119 | 120 | LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1); 121 | lp3.topMargin = mGapSpacing; 122 | lp3.bottomMargin = mGapSpacing; 123 | mRv3.setLayoutParams(lp3); 124 | } 125 | 126 | 127 | public void setAdapter(RecyclerView.Adapter adapter1, RecyclerView.Adapter adapter2, RecyclerView.Adapter adapter3) { 128 | mRv1.setAdapter(adapter1); 129 | mRv2.setAdapter(adapter2); 130 | mRv3.setAdapter(adapter3); 131 | } 132 | 133 | /** 134 | * start 135 | */ 136 | public void start() { 137 | mScroller.startScroll(0, 0, 100, 100, 1000); 138 | } 139 | 140 | /** 141 | * move speed 142 | * recommend 1~3 143 | * the bigger the fast 144 | * 145 | * @param speed speed 146 | */ 147 | public void setSpeed(int speed) { 148 | mSpeed = speed; 149 | } 150 | 151 | /** 152 | * set gap spacing 153 | * recommend this value equals recyclerView's item space 154 | * 155 | * @param spacing space 156 | */ 157 | public void setGapSpacing(int spacing) { 158 | mGapSpacing = spacing; 159 | setSpacing(); 160 | requestLayout(); 161 | } 162 | 163 | /** 164 | * set angel 165 | * recommend -20~-50 166 | * 167 | * @param angle angel 168 | */ 169 | public void setAngle(int angle) { 170 | mAngle = angle; 171 | mContainer.setRotation(mAngle); 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_carousel.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 18 | 19 | 20 | 27 | 28 | 33 | 34 | 38 | 39 | 44 | 45 | 52 | 53 | 60 | 61 | 66 | 67 | 68 | 69 | 70 | 71 | 76 | 77 | 81 | 82 | 87 | 88 | 95 | 96 | 101 | 102 | 109 | 110 | 115 | 116 | 117 | 118 | 123 | 124 | 128 | 129 | 134 | 135 | 136 | 143 | 144 | 150 | 151 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | --------------------------------------------------------------------------------