├── .github └── FUNDING.yml ├── LICENSE ├── README.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── ccy │ │ └── focuslayoutmanagerproject │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── ccy │ │ │ └── focuslayoutmanagerproject │ │ │ ├── DetailActivity.java │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── h1.jpg │ │ ├── h2.jpg │ │ ├── h3.jpg │ │ ├── h4.jpg │ │ ├── h5.jpg │ │ ├── h6.jpg │ │ ├── h7.jpg │ │ ├── ic_launcher_background.xml │ │ ├── v1.jpg │ │ ├── v2.jpg │ │ ├── v3.jpg │ │ ├── v4.jpg │ │ ├── v5.jpg │ │ ├── v6.jpg │ │ └── v7.jpg │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── detail_act.xml │ │ └── item_card.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── ccy │ └── focuslayoutmanagerproject │ └── ExampleUnitTest.java ├── build.gradle ├── ccy_ identity ├── focuslayoutmanager ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── ccy │ │ └── focuslayoutmanager │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── ccy │ │ │ └── focuslayoutmanager │ │ │ └── FocusLayoutManager.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── ccy │ └── focuslayoutmanager │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pic ├── 341561604648_.pic.jpg ├── detail.png ├── gif_hor.gif ├── gif_hor_2.gif ├── gif_mac_os.gif ├── gif_ver.gif ├── hor.jpg └── ver.jpg └── settings.gradle /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: https://github.com/CCY0122/FocusLayoutManager/blob/master/pic/341561604648_.pic.jpg 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [ ![Download](https://api.bintray.com/packages/ccy01220122/FocusLayoutManager/FocusLayoutManager/images/download.svg?version=1.0.2) ](https://bintray.com/ccy01220122/FocusLayoutManager/FocusLayoutManager/1.0.2/link) 2 | # FocusLayoutManager 3 | 有焦点item的水平/垂直滚动RecyclerView-LayoutManager。仿Android豆瓣书影音“推荐“频道列表布局 4 | 5 | 6 | ## 效果 7 | 8 |
**截图:**
9 | 10 | 11 | 12 | 13 |
**GIF:**
14 | ![gif1](https://github.com/CCY0122/FocusLayoutManager/blob/master/pic/gif_hor_2.gif) 15 | ![gif2](https://github.com/CCY0122/FocusLayoutManager/blob/master/pic/gif_ver.gif) 16 | 17 |
可自己监听滚动编写效果,如修改成仿MacOS文件浏览:
18 | 19 | 20 | 21 | 22 | ## 依赖 23 | 24 | ``` 25 | implementation 'com.ccy:FocusLayoutManager:1.0.2' 26 | // (or api) 27 | ``` 28 | 29 | ## 使用 30 | 31 | 32 | ```java 33 | focusLayoutManager = 34 | new FocusLayoutManager.Builder() 35 | .layerPadding(dp2px(this, 14)) 36 | .normalViewGap(dp2px(this, 14)) 37 | .focusOrientation(FocusLayoutManager.FOCUS_LEFT) 38 | .isAutoSelect(true) 39 | .maxLayerCount(3) 40 | .setOnFocusChangeListener(new FocusLayoutManager.OnFocusChangeListener() { 41 | @Override 42 | public void onFocusChanged(int focusdPosition, int lastFocusdPosition) { 43 | 44 | } 45 | }) 46 | .build(); 47 | recyclerView.setLayoutManager(focusLayoutManager); 48 | ``` 49 | 各属性意义见图:
50 | 51 |
52 | 注意:因为item在不同区域随着滑动会有不同的缩放(受动画控制),所以实际视觉上堆叠view的间距、普通view的间距未必等于layerPadding、normalViewGap 53 | 54 | #### 调整动画效果: 55 | ```java 56 | new FocusLayoutManager.Builder() 57 | ...... 58 | .setSimpleTrasitionListener(new FocusLayoutManager.SimpleTrasitionListener() { 59 | @Override 60 | public float getLayerViewMaxAlpha(int maxLayerCount) { 61 | return super.getLayerViewMaxAlpha(maxLayerCount); 62 | } 63 | 64 | @Override 65 | public float getLayerViewMinAlpha(int maxLayerCount) { 66 | return super.getLayerViewMinAlpha(maxLayerCount); 67 | } 68 | 69 | @Override 70 | public float getLayerChangeRangePercent() { 71 | return super.getLayerChangeRangePercent(); 72 | } 73 | //and more 74 | 75 | //更多可重写方法和释义见接口声明 76 | }) 77 | .build(); 78 | ``` 79 | 80 | #### 自定义动画/滚动监听: 81 | 如果你想在滑动时不仅仅改变item的大小、透明度,你有更多的想法,可以监听TrasitionListener,该监听暴露了很多关键布局数据, 82 | ```java 83 | 84 | ...... 85 | .setSimpleTrasitionListener(null) //如果默认动画不想要,移除之。or use removeTrasitionlistener(XXX) 86 | .addTrasitionListener(new FocusLayoutManager.TrasitionListener() { 87 | @Override 88 | public void handleLayerView(FocusLayoutManager focusLayoutManager, 89 | View view, int viewLayer, 90 | int maxLayerCount, int position, 91 | float fraction, float offset) { 92 | 93 | } 94 | 95 | @Override 96 | public void handleFocusingView(FocusLayoutManager focusLayoutManager, 97 | View view, int position, 98 | float fraction, float offset) { 99 | 100 | } 101 | 102 | @Override 103 | public void handleNormalView(FocusLayoutManager focusLayoutManager, View view, int position, float fraction, float offset) { 104 | 105 | } 106 | }) 107 | ``` 108 | 各参数意义见接口注释。 109 | 实际上`SimpleTrasitionListener`内部就会被转为`TrasitionListener`。可参考转换类是怎么做的:`TrasitionListenerConvert` 110 | 111 | ## 源码解析 112 | 113 | [https://blog.csdn.net/ccy0122/article/details/90515386](https://blog.csdn.net/ccy0122/article/details/90515386) 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "ccy.focuslayoutmanagerproject" 7 | minSdkVersion 21 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-optimize.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' 24 | testImplementation 'junit:junit:4.12' 25 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 26 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 27 | implementation 'com.android.support:recyclerview-v7:28.0.0' 28 | implementation 'com.android.support:cardview-v7:28.0.0' 29 | implementation 'com.github.bumptech.glide:glide:4.8.0' 30 | annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' 31 | implementation project(':focuslayoutmanager') 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/ccy/focuslayoutmanagerproject/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package ccy.focuslayoutmanagerproject; 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("ccy.focuslayoutmanagerproject", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/ccy/focuslayoutmanagerproject/DetailActivity.java: -------------------------------------------------------------------------------- 1 | package ccy.focuslayoutmanagerproject; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Window; 7 | import android.widget.ImageView; 8 | 9 | import com.bumptech.glide.Glide; 10 | 11 | /** 12 | * Created by ccy(17022) on 2019/5/23 上午10:47 13 | */ 14 | public class DetailActivity extends AppCompatActivity { 15 | 16 | 17 | @Override 18 | protected void onCreate(@Nullable Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | supportRequestWindowFeature(Window.FEATURE_NO_TITLE); 21 | setContentView(R.layout.detail_act); 22 | 23 | int res = getIntent().getIntExtra("resId",0); 24 | if(res != 0){ 25 | Glide.with(this).load(res).into((ImageView) findViewById(R.id.img)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/ccy/focuslayoutmanagerproject/MainActivity.java: -------------------------------------------------------------------------------- 1 | package ccy.focuslayoutmanagerproject; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.support.v4.app.ActivityOptionsCompat; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.CardView; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.view.Window; 17 | import android.widget.CheckBox; 18 | import android.widget.CompoundButton; 19 | import android.widget.EditText; 20 | import android.widget.ImageView; 21 | import android.widget.RadioGroup; 22 | import android.widget.TextView; 23 | import android.widget.Toast; 24 | 25 | import com.bumptech.glide.Glide; 26 | 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | import ccy.focuslayoutmanager.FocusLayoutManager; 31 | 32 | public class MainActivity extends AppCompatActivity { 33 | 34 | public static Toast mToast; 35 | View emptyView; 36 | TextView tvFocusedPos; 37 | CheckBox cbAutoSelect; 38 | CheckBox cbInfinite; 39 | RecyclerView recyclerView; 40 | FocusLayoutManager focusLayoutManager; 41 | Adapter adapter; 42 | 43 | int colors[] = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00, 0xff00ffff, 0xffff00ff, 44 | 0xffd0d0d0, 0xff000000, 0xffe04900, 0xff900909}; 45 | int horRes[] = {R.drawable.h5, R.drawable.h6, R.drawable.h7, R.drawable.h1, R.drawable.h2, 46 | R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7, 47 | R.drawable.h5, R.drawable.h6, R.drawable.h7, R.drawable.h1, R.drawable.h2, 48 | R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7}; 49 | int verRes[] = {R.drawable.v5, R.drawable.v6, R.drawable.v7, R.drawable.v1, R.drawable.v2, 50 | R.drawable.v3, R.drawable.v4, R.drawable.v5, R.drawable.v6, R.drawable.v7}; 51 | 52 | List datas; 53 | 54 | @Override 55 | protected void onCreate(Bundle savedInstanceState) { 56 | super.onCreate(savedInstanceState); 57 | supportRequestWindowFeature(Window.FEATURE_NO_TITLE); 58 | setContentView(R.layout.activity_main); 59 | 60 | recyclerView = findViewById(R.id.rv); 61 | emptyView = findViewById(R.id.empty); 62 | tvFocusedPos = findViewById(R.id.tv_focus_pos); 63 | cbAutoSelect = findViewById(R.id.auto_select_cb); 64 | cbInfinite = findViewById(R.id.infinite_cb); 65 | 66 | focusLayoutManager = 67 | new FocusLayoutManager.Builder() 68 | .layerPadding(dp2px(this, 14)) 69 | .normalViewGap(dp2px(this, 14)) 70 | .focusOrientation(FocusLayoutManager.FOCUS_LEFT) 71 | .isAutoSelect(true) 72 | .maxLayerCount(3) 73 | .setOnFocusChangeListener(new FocusLayoutManager.OnFocusChangeListener() { 74 | @Override 75 | public void onFocusChanged(int focusdPosition, int lastFocusdPosition) { 76 | tvFocusedPos.setText("[" + focusdPosition + "],[" + lastFocusdPosition + "]"); 77 | if (focusdPosition == datas.size() - 1 && 78 | (focusLayoutManager.getFocusOrientation() == FocusLayoutManager.FOCUS_LEFT)) { 79 | emptyView.setVisibility(View.VISIBLE); 80 | } else { 81 | emptyView.setVisibility(View.GONE); 82 | } 83 | } 84 | }) 85 | .build(); 86 | 87 | // datas = new ArrayList<>(); 88 | // for (int i = 0; i < 20; i++) { 89 | // Bean bean = new Bean(); 90 | // bean.useColor = true; 91 | // bean.msg = "" + (i); 92 | // bean.color = colors[i % 10]; 93 | // datas.add(bean); 94 | // } 95 | datas = getDatas(false); 96 | adapter = new Adapter(datas); 97 | recyclerView.setAdapter(adapter); 98 | recyclerView.setLayoutManager(focusLayoutManager); 99 | 100 | cbAutoSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 101 | @Override 102 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 103 | focusLayoutManager.setAutoSelect(isChecked); 104 | } 105 | }); 106 | cbInfinite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 107 | @Override 108 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 109 | recyclerView.setAdapter(new Adapter(datas)); 110 | if (isChecked) { 111 | emptyView.setVisibility(View.GONE); 112 | recyclerView.post(new Runnable() { 113 | @Override 114 | public void run() { 115 | focusLayoutManager.scrollToPosition(1000); 116 | } 117 | }); 118 | } 119 | } 120 | }); 121 | 122 | } 123 | 124 | public List getDatas(boolean vertical) { 125 | List datas = new ArrayList<>(); 126 | if (vertical) { 127 | for (int i = 0; i < verRes.length; i++) { 128 | Bean bean = new Bean(); 129 | bean.useColor = false; 130 | bean.background = verRes[i]; 131 | datas.add(bean); 132 | } 133 | } else { 134 | for (int i = 0; i < horRes.length; i++) { 135 | Bean bean = new Bean(); 136 | bean.useColor = false; 137 | bean.background = horRes[i]; 138 | datas.add(bean); 139 | } 140 | } 141 | 142 | return datas; 143 | } 144 | 145 | public static float dp2px(Context context, float dp) { 146 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 147 | context.getResources().getDisplayMetrics()); 148 | } 149 | 150 | public void layerCount_btn(View view) { 151 | EditText et = findViewById(R.id.layerCount); 152 | try { 153 | int count = Integer.parseInt(et.getText().toString()); 154 | if (count <= 0) { 155 | Toast.makeText(this, "不合法", Toast.LENGTH_SHORT).show(); 156 | return; 157 | } 158 | focusLayoutManager.setMaxLayerCount(count); 159 | } catch (NumberFormatException e) { 160 | e.printStackTrace(); 161 | } 162 | } 163 | 164 | 165 | public void changeTrasition(View view) { 166 | focusLayoutManager.setMaxLayerCount(3); 167 | focusLayoutManager.setNormalViewGap(dp2px(this, 4)); 168 | focusLayoutManager.setLayerPadding(dp2px(this, 50)); 169 | focusLayoutManager.removeTrasitionlistener(null); 170 | focusLayoutManager.addTrasitionListener(new FocusLayoutManager.TrasitionListener() { 171 | @Override 172 | public void handleLayerView(FocusLayoutManager focusLayoutManager, View view, 173 | int viewLayer, int maxLayerCount, int position, 174 | float fraction, float offset) { 175 | if (view instanceof CardView) { 176 | ((CardView) view).setCardElevation(0); 177 | } 178 | float realFraction = fraction; 179 | 180 | float minRo = 80; 181 | float maxRo = 0; 182 | float roDelta = maxRo - minRo; 183 | float currentLayerMaxRo = 184 | minRo + roDelta * (viewLayer + 1) / (maxLayerCount * 1.0f); 185 | float currentLayerMinRo = 186 | minRo + roDelta * viewLayer / (maxLayerCount * 1.0f); 187 | float realRo = 188 | currentLayerMaxRo - (currentLayerMaxRo - currentLayerMinRo) * realFraction; 189 | 190 | float minScale = 0.7f; 191 | float maxScale = 1f; 192 | float scaleDelta = maxScale - minScale; 193 | float currentLayerMaxScale = 194 | minScale + scaleDelta * (viewLayer + 1) / (maxLayerCount * 1.0f); 195 | float currentLayerMinScale = 196 | minScale + scaleDelta * viewLayer / (maxLayerCount * 1.0f); 197 | float realScale = 198 | currentLayerMaxScale - (currentLayerMaxScale - currentLayerMinScale) * realFraction; 199 | 200 | float minAlpha = 0; 201 | float maxAlpha = 1; 202 | float alphaDelta = maxAlpha - minAlpha; //总透明度差 203 | float currentLayerMaxAlpha = 204 | minAlpha + alphaDelta * (viewLayer + 1) / (maxLayerCount * 1.0f); 205 | float currentLayerMinAlpha = 206 | minAlpha + alphaDelta * viewLayer / (maxLayerCount * 1.0f); 207 | float realAlpha = 208 | currentLayerMaxAlpha - (currentLayerMaxAlpha - currentLayerMinAlpha) * realFraction; 209 | 210 | view.setScaleX(realScale); 211 | view.setScaleY(realScale); 212 | view.setRotationY(realRo); 213 | view.setAlpha(realAlpha); 214 | 215 | } 216 | 217 | @Override 218 | public void handleFocusingView(FocusLayoutManager focusLayoutManager, View view, 219 | int position, float fraction, float offset) { 220 | if (view instanceof CardView) { 221 | ((CardView) view).setCardElevation(0); 222 | } 223 | float realFraction = fraction; 224 | 225 | float realScale = 226 | 0.85f + (1f - 0.85f) * realFraction; 227 | float realAlpha = 1; 228 | 229 | view.setScaleX(realScale); 230 | view.setScaleY(realScale); 231 | view.setAlpha(realAlpha); 232 | view.setRotationY(0); 233 | 234 | } 235 | 236 | @Override 237 | public void handleNormalView(FocusLayoutManager focusLayoutManager, View view, 238 | int position, float fraction, float offset) { 239 | if (view instanceof CardView) { 240 | ((CardView) view).setCardElevation(0); 241 | } 242 | view.setScaleX(0.85f); 243 | view.setScaleY(0.85f); 244 | view.setAlpha(1); 245 | view.setRotationY(0); 246 | } 247 | }); 248 | } 249 | 250 | public void normalViewGap_btn(View view) { 251 | EditText et = findViewById(R.id.normalViewGap); 252 | try { 253 | int count = Integer.parseInt(et.getText().toString()); 254 | 255 | focusLayoutManager.setNormalViewGap(dp2px(this, count)); 256 | } catch (NumberFormatException e) { 257 | e.printStackTrace(); 258 | } 259 | } 260 | 261 | public void layerPadding_btn(View view) { 262 | EditText et = findViewById(R.id.layerPadding); 263 | try { 264 | int count = Integer.parseInt(et.getText().toString()); 265 | 266 | focusLayoutManager.setLayerPadding(dp2px(this, count)); 267 | } catch (NumberFormatException e) { 268 | e.printStackTrace(); 269 | } 270 | } 271 | 272 | public void orientation_btn(View view) { 273 | RadioGroup rg = findViewById(R.id.ori_rg); 274 | int id = rg.getCheckedRadioButtonId(); 275 | if (id == R.id.l) { 276 | focusLayoutManager.setFocusOrientation(FocusLayoutManager.FOCUS_LEFT); 277 | ViewGroup.LayoutParams p = recyclerView.getLayoutParams(); 278 | p.width = RecyclerView.LayoutParams.MATCH_PARENT; 279 | p.height = RecyclerView.LayoutParams.WRAP_CONTENT; 280 | recyclerView.setAdapter(new Adapter(datas = getDatas(false))); 281 | } 282 | if (id == R.id.r) { 283 | focusLayoutManager.setFocusOrientation(FocusLayoutManager.FOCUS_RIGHT); 284 | ViewGroup.LayoutParams p = recyclerView.getLayoutParams(); 285 | p.width = RecyclerView.LayoutParams.MATCH_PARENT; 286 | p.height = RecyclerView.LayoutParams.WRAP_CONTENT; 287 | recyclerView.setAdapter(new Adapter(datas = getDatas(false))); 288 | } 289 | if (id == R.id.t) { 290 | focusLayoutManager.setFocusOrientation(FocusLayoutManager.FOCUS_TOP); 291 | ViewGroup.LayoutParams p = recyclerView.getLayoutParams(); 292 | p.width = RecyclerView.LayoutParams.WRAP_CONTENT; 293 | p.height = (int) dp2px(this, 480); 294 | recyclerView.setAdapter(new Adapter(datas = getDatas(true))); 295 | } 296 | if (id == R.id.b) { 297 | focusLayoutManager.setFocusOrientation(FocusLayoutManager.FOCUS_BOTTOM); 298 | ViewGroup.LayoutParams p = recyclerView.getLayoutParams(); 299 | p.width = RecyclerView.LayoutParams.WRAP_CONTENT; 300 | p.height = (int) dp2px(this, 480); 301 | recyclerView.setAdapter(new Adapter(datas = getDatas(true))); 302 | } 303 | } 304 | 305 | 306 | public class Adapter extends RecyclerView.Adapter { 307 | 308 | private final List datas; 309 | private int index = 0; 310 | 311 | public Adapter(List datas) { 312 | this.datas = datas; 313 | } 314 | 315 | 316 | @NonNull 317 | @Override 318 | public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { 319 | View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_card, 320 | viewGroup, false); 321 | if (focusLayoutManager.getFocusOrientation() == FocusLayoutManager.FOCUS_LEFT || focusLayoutManager.getFocusOrientation() == FocusLayoutManager.FOCUS_RIGHT) { 322 | ViewGroup.MarginLayoutParams p = 323 | (ViewGroup.MarginLayoutParams) view.getLayoutParams(); 324 | p.topMargin = (int) dp2px(view.getContext(), 25); 325 | p.bottomMargin = (int) dp2px(view.getContext(), 25); 326 | p.leftMargin = (int) dp2px(view.getContext(), 0); 327 | p.rightMargin = (int) dp2px(view.getContext(), 0); 328 | p.width = (int) dp2px(view.getContext(), 100); 329 | p.height = (int) dp2px(view.getContext(), 150); 330 | } else { 331 | ViewGroup.MarginLayoutParams p = 332 | (ViewGroup.MarginLayoutParams) view.getLayoutParams(); 333 | p.topMargin = (int) dp2px(view.getContext(), 0); 334 | p.bottomMargin = (int) dp2px(view.getContext(), 0); 335 | p.leftMargin = (int) dp2px(view.getContext(), 25); 336 | p.rightMargin = (int) dp2px(view.getContext(), 25); 337 | p.width = (int) dp2px(view.getContext(), 150); 338 | p.height = (int) dp2px(view.getContext(), 100); 339 | } 340 | view.setTag(++index); 341 | Log.d("ccy", "onCreateViewHolder = " + index); 342 | return new ViewHolder(view); 343 | } 344 | 345 | @Override 346 | public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) { 347 | Log.d("ccy", "onBindViewHolder,index = " + (int) (viewHolder.itemView.getTag())); 348 | int realPosition = cbInfinite.isChecked() ? position % datas.size() : position; 349 | 350 | Bean bean = datas.get(realPosition); 351 | 352 | if (bean.useColor) { 353 | ((CardView) viewHolder.itemView).setBackgroundResource(0); 354 | ((CardView) viewHolder.itemView).setBackgroundColor(bean.color); 355 | } else { 356 | Glide.with(viewHolder.itemView) 357 | .load(bean.background) 358 | .into(viewHolder.iv); 359 | } 360 | viewHolder.tv.setText(bean.msg); 361 | } 362 | 363 | @Override 364 | public int getItemCount() { 365 | return cbInfinite.isChecked() ? Integer.MAX_VALUE : datas.size(); 366 | } 367 | 368 | public class ViewHolder extends RecyclerView.ViewHolder { 369 | 370 | TextView tv; 371 | ImageView iv; 372 | 373 | public ViewHolder(@NonNull final View itemView) { 374 | super(itemView); 375 | tv = itemView.findViewById(R.id.item_tv); 376 | iv = itemView.findViewById(R.id.item_iv); 377 | itemView.setOnClickListener(new View.OnClickListener() { 378 | @Override 379 | public void onClick(View v) { 380 | int pos = getAdapterPosition(); 381 | if (mToast == null) { 382 | mToast = Toast.makeText(MainActivity.this, "" + pos, 383 | Toast.LENGTH_SHORT); 384 | } 385 | mToast.setText("" + pos); 386 | mToast.show(); 387 | 388 | if (pos == focusLayoutManager.getFocusdPosition()) { 389 | 390 | Intent intent = new Intent(MainActivity.this, DetailActivity.class); 391 | intent.putExtra("resId", datas.get(pos).background); 392 | startActivity(intent, ActivityOptionsCompat.makeSceneTransitionAnimation 393 | (MainActivity.this, itemView, "img").toBundle()); 394 | } else { 395 | focusLayoutManager.setFocusdPosition(pos, true); 396 | } 397 | } 398 | }); 399 | } 400 | } 401 | 402 | } 403 | 404 | public static class Bean { 405 | boolean useColor = true; 406 | int color; 407 | int background; 408 | String msg; 409 | } 410 | } 411 | -------------------------------------------------------------------------------- /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/drawable/h1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h5.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h6.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/h7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/h7.jpg -------------------------------------------------------------------------------- /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/drawable/v1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v5.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v6.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/v7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CCY0122/FocusLayoutManager/165ae982f73433c3c73a56674a980ea9749cb656/app/src/main/res/drawable/v7.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 16 | 17 | 22 | 23 | 32 | 33 | 39 | 40 | 45 | 46 | 51 | 52 | 53 | 54 | 60 | 61 | 66 | 67 | 72 | 73 |