├── .gitignore ├── LICENSE ├── README-zh.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── app │ │ └── tvrecyclerview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── app │ │ │ └── tvrecyclerview │ │ │ ├── MainActivity.java │ │ │ ├── Utils.java │ │ │ ├── bean │ │ │ ├── Row.java │ │ │ └── itemPosition.java │ │ │ ├── irregular │ │ │ ├── IrregularActivity.java │ │ │ ├── IrregularPresenter.java │ │ │ └── IrregularVerticalActivity.java │ │ │ └── reuglar │ │ │ ├── RegularActivity.java │ │ │ ├── RegularPresenter.java │ │ │ ├── RegularVerticalActivity.java │ │ │ └── RegularVerticalPresenter.java │ └── res │ │ ├── anim │ │ ├── anim_scale_big.xml │ │ └── anim_scale_small.xml │ │ ├── drawable │ │ ├── default_focus.9.png │ │ ├── pic0.jpg │ │ ├── pic1.jpg │ │ ├── pic2.jpg │ │ ├── pic3.jpg │ │ ├── pic4.jpg │ │ ├── pic5.jpg │ │ ├── pic6.jpg │ │ ├── pic7.jpg │ │ ├── pic8.jpg │ │ ├── rl_shape_corner.xml │ │ └── tv_shape_corner.xml │ │ ├── layout │ │ ├── activity_irregular.xml │ │ ├── activity_irregular_vertical.xml │ │ ├── activity_main.xml │ │ ├── activity_regular.xml │ │ └── activity_regular_vertical.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ ├── horizonal_irregular.json │ │ ├── horizonal_regular.json │ │ └── vertical_irregular.json │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── app │ └── tvrecyclerview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screen1.png ├── screen2.png ├── settings.gradle └── tvrecyclerview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── app │ └── com │ └── tvrecyclerview │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── app │ │ └── com │ │ └── tvrecyclerview │ │ ├── BaseGridView.java │ │ ├── FocusHighlightHandler.java │ │ ├── FocusHighlightHelper.java │ │ ├── GridLayoutManager.java │ │ ├── GridObjectAdapter.java │ │ ├── HorizontalGridView.java │ │ ├── ItemBridgeAdapter.java │ │ ├── OnChildSelectedListener.java │ │ ├── OnChildViewHolderSelectedListener.java │ │ ├── OnFocusSearchFailedListener.java │ │ ├── Presenter.java │ │ ├── PresenterSelector.java │ │ ├── RowItem.java │ │ ├── SinglePresenterSelector.java │ │ └── VerticalGridView.java └── res │ └── values │ ├── attrs.xml │ ├── dims.xml │ └── strings.xml └── test └── java └── app └── com └── tvrecyclerview └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/* 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /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-zh.md: -------------------------------------------------------------------------------- 1 | # TvRecyclerView 2 | 用在android TV端的自定义recyclerView控件, 不支持手机端. 3 | 4 | ## ScreenShot 5 |

6 | 7 |
8 |
9 | 10 |
11 |
12 | 13 |

14 | 15 | ## 使用步骤 16 | 1.首先在你项目的gradle配置以下命令, 引用TvRecyclerView : 17 | ```groovy 18 | dependencies { 19 | ...... 20 | compile 'com.henryblue.library:tvrecyclerview:1.2.3' 21 | } 22 | 23 | ``` 24 | 将上面的引用命令添加到build.gradle(是module下面的, 不是项目下面的) 25 | 26 | 2. 添加TvRecyclerView到你的布局中 27 | ``` 28 | 33 | ``` 34 | 35 | 3.设置LayoutManager 和 Adapter 36 | ``` 37 | mTvRecyclerView = (TvRecyclerView) findViewById(R.id.tv_recycler_view); 38 | GridLayoutManager manager = new GridLayoutManager(NormalFocusActivity.this, 3); 39 | manager.setOrientation(LinearLayoutManager.HORIZONTAL); 40 | mTvRecyclerView.setLayoutManager(manager); 41 | 42 | int itemSpace = getResources(). 43 | getDimensionPixelSize(R.dimen.recyclerView_item_space); 44 | mTvRecyclerView.addItemDecoration(new SpaceItemDecoration(itemSpace)); 45 | 46 | NormalAdapter mAdapter = new NormalAdapter(this); 47 | mTvRecyclerView.setAdapter(mAdapter); 48 | 49 | mTvRecyclerView.setOnItemStateListener(new TvRecyclerView.OnItemStateListener() { 50 | @Override 51 | public void onItemViewClick(View view, int position) { 52 | Log.i(TAG, "you click item position: " + position); 53 | } 54 | 55 | @Override 56 | public void onItemViewFocusChanged(boolean gainFocus, View view, int position) { 57 | } 58 | }); 59 | mTvRecyclerView.setSelectPadding(35, 34, 35, 38); 60 | ``` 61 | 62 | ## TvRecyclerView的自定义属性 63 | 64 | ```xml 65 | 73 | ``` 74 | 75 | 76 | | Name | Type | Default | Description | 77 | |:----:|:----:|:-------:|:-----------:| 78 | |scrollMode|enum|normalScroll| 设置滑动模式 | 79 | |isAutoProcessFocus|boolean|true| 设置是否由TvRecyclerView处理焦点 | 80 | |focusScale|Float|1.04f| 设置item获取焦点时放大倍数. 如果设置属性'isAutoProcessFocus', 这个属性不生效, 总是默认值1.0f | 81 | |focusDrawable|reference|null| 设置焦点框背景图, 如果设置属性'isAutoProcessFocus', 这个属性不生效 | 82 | 83 | ## TvRecyclerView提供的公共接口 84 | 85 | | Name | Description | 86 | |:----:|:---------------:| 87 | |setSelectedScale(float)| 设置item获取焦点时放大倍数 | 88 | |setIsAutoProcessFocus(boolean)| 设置属性'isAutoProcessFocus' | 89 | |setFocusDrawable(Drawable)| 置焦点框背景图 | 90 | |setItemSelected(int)| 设置指定的item获取焦点 | 91 | |setSelectPadding(int,int,int,int)| 调整焦点框的padding值, 确保焦点框包裹住item | 92 | 93 | 此外: 因为TvRecyclerView继承RecyclerView, 所以你可以使用所有的RecyclerView的所有公共接口. 94 | 95 | ## 其他 96 | 当isAutoProcessFocus属性设置为true(默认为true), 意味着焦点是由TvRecyclerView来处理, 不会交给子view, 这时需要注意子view不要将背景设为 97 | 透明, 因为如果背景为透明, 那么根据焦点框的实现逻辑, 会发生重影的效果。 98 | 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TvRecyclerView 2 | A custom RecyclerView for Android TV end, It can implement grid and irregular grid two display types. 3 | 4 | ## ScreenShot 5 |

6 | 7 |
8 |
9 | 10 |

11 | 12 | ## Usage 13 | 1.First you can add gradle dependency with command : 14 | ```groovy 15 | dependencies { 16 | ...... 17 | compile 'com.henryblue.library:tvrecyclerview:1.2.4' 18 | } 19 | 20 | ``` 21 | To add gradle dependency you need to open build.gradle (in your app folder,not in a project folder) then copy and add the dependencies there in the dependencies block; 22 | 23 | 2.Add HorizontalGridView in your layout 24 | ``` 25 | 34 | ``` 35 | 3.Set the presenter(adpter create child view) 36 | ``` 37 | public class RegularPresenter extends Presenter { 38 | RegularPresenter(Context context) { 39 | super(context); 40 | } 41 | @Override 42 | public View onCreateView() { 43 | ImageView view = new ImageView(getContext()); 44 | view.setSelected(true); 45 | view.setFocusable(true); 46 | view.setLayoutParams(new ViewGroup.LayoutParams(423, 138)); 47 | return view; 48 | } 49 | 50 | @Override 51 | public void onBindViewHolder(ViewHolder viewHolder, Object item) { 52 | viewHolder.view.setBackgroundColor(Color.GREEN); 53 | } 54 | 55 | @Override 56 | public void onUnbindViewHolder(ViewHolder viewHolder) { 57 | } 58 | } 59 | ``` 60 | 61 | 4.Set GridObjectAdapter 62 | ``` 63 | HorizontalGridView gridView = findViewById(R.id.id_grid_first); 64 | gridView.addItemDecoration(new SpaceItemDecoration()); 65 | gridView.setNumRows(2); 66 | GridObjectAdapter adapter = new GridObjectAdapter(new RegularPresenter(this)); 67 | gridView.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 68 | gridView.setAdapter(adapter); 69 | for (int i = 0; i < 10; i++) { 70 | adapter.add(new RowItem()); 71 | } 72 | ``` 73 | For more usage, please refer to the examples in the code. For example how to implement irregular style, please refer [**here.**](https://github.com/henryblue/TvRecyclerView/tree/master/app/src/main/java/com/app/tvrecyclerview/irregular) 74 | 75 | ## XML attributes 76 | 77 | 78 | | Name | Type | Default | Description | 79 | |:----:|:----:|:-------:|:-----------:| 80 | |android:gravity|enum|Gravity.TOP&Gravity.LEFT| Set the way to gravity (Works in regular mode) | 81 | |focusOutFront|boolean|false| Allow DPAD key to navigate out at the front of the View | 82 | |focusOutEnd|boolean|false|Allow DPAD key to navigate out at the end of the view| 83 | |numberOfRows|integer|1| set the number of rows in the HorizontalGridView (Works in regular mode) | 84 | |numberOfColumns|integer|1| set the number of columns in the VerticalGridView (Works in regular mode) | 85 | 86 | 87 | 88 | 89 | ## License 90 | ``` 91 | Copyright 2016 henryblue 92 | 93 | Licensed under the Apache License, Version 2.0 (the "License"); 94 | you may not use this file except in compliance with the License. 95 | You may obtain a copy of the License at 96 | 97 | http://www.apache.org/licenses/LICENSE-2.0 98 | 99 | Unless required by applicable law or agreed to in writing, software 100 | distributed under the License is distributed on an "AS IS" BASIS, 101 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 102 | See the License for the specific language governing permissions and 103 | limitations under the License. 104 | 105 | ``` 106 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | buildToolsVersion "27.0.3" 6 | defaultConfig { 7 | applicationId "com.app.tvrecyclerview" 8 | minSdkVersion 16 9 | targetSdkVersion 27 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | implementation 'com.android.support:appcompat-v7:27.1.1' 28 | implementation 'com.android.support:appcompat-v7:27.1.1' 29 | implementation 'com.android.support:recyclerview-v7:27.1.1' 30 | testImplementation 'junit:junit:4.12' 31 | compile 'com.google.code.gson:gson:2.8.5' 32 | implementation project(path: ':tvrecyclerview') 33 | } 34 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/henry/software/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/app/tvrecyclerview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.app.tvrecyclerview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import com.app.tvrecyclerview.irregular.IrregularActivity; 10 | import com.app.tvrecyclerview.irregular.IrregularVerticalActivity; 11 | import com.app.tvrecyclerview.reuglar.RegularActivity; 12 | import com.app.tvrecyclerview.reuglar.RegularVerticalActivity; 13 | 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | Button btnNormal = findViewById(R.id.btn_normal); 22 | btnNormal.setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | Intent intent = new Intent(MainActivity.this, RegularActivity.class); 26 | startActivity(intent); 27 | } 28 | }); 29 | 30 | Button btnModule = findViewById(R.id.btn_module); 31 | btnModule.setOnClickListener(new View.OnClickListener() { 32 | @Override 33 | public void onClick(View v) { 34 | Intent intent = new Intent(MainActivity.this, IrregularActivity.class); 35 | startActivity(intent); 36 | } 37 | }); 38 | 39 | Button btnModuleV = findViewById(R.id.btn_module_vertical); 40 | btnModuleV.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | Intent intent = new Intent(MainActivity.this, RegularVerticalActivity.class); 44 | startActivity(intent); 45 | } 46 | }); 47 | 48 | Button btnModuleV1 = findViewById(R.id.btn_regular_vertical); 49 | btnModuleV1.setOnClickListener(new View.OnClickListener() { 50 | @Override 51 | public void onClick(View v) { 52 | Intent intent = new Intent(MainActivity.this, IrregularVerticalActivity.class); 53 | startActivity(intent); 54 | } 55 | }); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/Utils.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview; 2 | 3 | import android.graphics.Color; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.util.Random; 8 | 9 | 10 | public class Utils { 11 | public static String inputStreamToString(InputStream inputStream) { 12 | try { 13 | byte[] bytes = new byte[inputStream.available()]; 14 | inputStream.read(bytes, 0, bytes.length); 15 | String json = new String(bytes); 16 | return json; 17 | } catch (IOException e) { 18 | return null; 19 | } 20 | } 21 | 22 | public static int getRandColor() { 23 | Random random = new Random(); 24 | return Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/bean/Row.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class Row { 8 | @SerializedName("rowSpacing") private int rowSpacing = 15; 9 | @SerializedName("columnSpacing") private int columnSpacing = 15; 10 | @SerializedName("columns") private int columns = 1; 11 | @SerializedName("aspectRatio") private float aspectRatio = 1.0f; 12 | @SerializedName("items") private List items= null; 13 | 14 | public List getItems() { 15 | return items; 16 | } 17 | 18 | public void setItems(List items) { 19 | this.items = items; 20 | } 21 | 22 | public int getRowSpacing() { 23 | return rowSpacing; 24 | } 25 | 26 | public void setRowSpacing(int rowSpacing) { 27 | this.rowSpacing = rowSpacing; 28 | } 29 | 30 | public int getColumnSpacing() { 31 | return columnSpacing; 32 | } 33 | 34 | public void setColumnSpacing(int columnSpacing) { 35 | this.columnSpacing = columnSpacing; 36 | } 37 | 38 | public int getColumns() { 39 | return columns; 40 | } 41 | 42 | public void setColumns(int columns) { 43 | this.columns = columns; 44 | } 45 | 46 | public float getAspectRatio() { 47 | return aspectRatio; 48 | } 49 | 50 | public void setAspectRatio(float aspectRatio) { 51 | this.aspectRatio = aspectRatio; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/bean/itemPosition.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.bean; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public class itemPosition { 6 | 7 | @SerializedName("x") private int x; 8 | @SerializedName("y") private int y; 9 | @SerializedName("w") private int width; 10 | @SerializedName("h") private int height; 11 | 12 | public int getX() { 13 | return x; 14 | } 15 | 16 | public void setX(int x) { 17 | this.x = x; 18 | } 19 | 20 | public int getY() { 21 | return y; 22 | } 23 | 24 | public void setY(int y) { 25 | this.y = y; 26 | } 27 | 28 | public int getWidth() { 29 | return width; 30 | } 31 | 32 | public void setWidth(int width) { 33 | this.width = width; 34 | } 35 | 36 | public int getHeight() { 37 | return height; 38 | } 39 | 40 | public void setHeight(int height) { 41 | this.height = height; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/irregular/IrregularActivity.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.irregular; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | 6 | import com.app.tvrecyclerview.R; 7 | import com.app.tvrecyclerview.Utils; 8 | import com.app.tvrecyclerview.bean.Row; 9 | import com.app.tvrecyclerview.bean.itemPosition; 10 | import com.google.gson.Gson; 11 | 12 | import app.com.tvrecyclerview.FocusHighlightHelper; 13 | import app.com.tvrecyclerview.GridObjectAdapter; 14 | import app.com.tvrecyclerview.HorizontalGridView; 15 | import app.com.tvrecyclerview.RowItem; 16 | 17 | 18 | public class IrregularActivity extends Activity { 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_irregular); 23 | HorizontalGridView gridView = findViewById(R.id.id_grid); 24 | 25 | 26 | String json = Utils.inputStreamToString(getResources().openRawResource(R.raw.horizonal_irregular)); 27 | Row row = new Gson().fromJson(json, Row.class); 28 | GridObjectAdapter adapter = new GridObjectAdapter(new IrregularPresenter(this), 29 | row.getRowSpacing(), row.getColumnSpacing(), row.getColumns(), 30 | row.getAspectRatio()); 31 | gridView.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 32 | gridView.setAdapter(adapter); 33 | for (itemPosition position : row.getItems()) { 34 | RowItem rowItem = new RowItem(); 35 | rowItem.setX(position.getX()); 36 | rowItem.setY(position.getY()); 37 | rowItem.setWidth(position.getWidth()); 38 | rowItem.setHeight(position.getHeight()); 39 | adapter.add(rowItem); 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/irregular/IrregularPresenter.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.irregular; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.widget.ImageView; 6 | 7 | import com.app.tvrecyclerview.Utils; 8 | 9 | import app.com.tvrecyclerview.Presenter; 10 | 11 | 12 | public class IrregularPresenter extends Presenter { 13 | 14 | IrregularPresenter(Context context) { 15 | super(context); 16 | } 17 | 18 | @Override 19 | public View onCreateView() { 20 | ImageView view = new ImageView(getContext()); 21 | view.setSelected(true); 22 | view.setFocusable(true); 23 | return view; 24 | } 25 | 26 | @Override 27 | public void onBindViewHolder(ViewHolder viewHolder, Object item) { 28 | viewHolder.view.setBackgroundColor(Utils.getRandColor()); 29 | } 30 | 31 | @Override 32 | public void onUnbindViewHolder(ViewHolder viewHolder) { 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/irregular/IrregularVerticalActivity.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.irregular; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | 6 | import com.app.tvrecyclerview.R; 7 | import com.app.tvrecyclerview.Utils; 8 | import com.app.tvrecyclerview.bean.Row; 9 | import com.app.tvrecyclerview.bean.itemPosition; 10 | import com.app.tvrecyclerview.reuglar.RegularVerticalPresenter; 11 | import com.google.gson.Gson; 12 | 13 | import app.com.tvrecyclerview.FocusHighlightHelper; 14 | import app.com.tvrecyclerview.GridObjectAdapter; 15 | import app.com.tvrecyclerview.RowItem; 16 | import app.com.tvrecyclerview.VerticalGridView; 17 | 18 | 19 | public class IrregularVerticalActivity extends Activity { 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_irregular_vertical); 24 | VerticalGridView gridView = findViewById(R.id.id_grid_vertical_ir); 25 | String json = Utils.inputStreamToString(getResources().openRawResource(R.raw.vertical_irregular)); 26 | Row row = new Gson().fromJson(json, Row.class); 27 | 28 | GridObjectAdapter adapter = new GridObjectAdapter(new RegularVerticalPresenter(this), 29 | row.getRowSpacing(), row.getColumnSpacing(), row.getColumns(), 30 | row.getAspectRatio()); 31 | gridView.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 32 | gridView.setAdapter(adapter); 33 | 34 | for (itemPosition position : row.getItems()) { 35 | RowItem rowItem = new RowItem(); 36 | rowItem.setX(position.getX()); 37 | rowItem.setY(position.getY()); 38 | rowItem.setWidth(position.getWidth()); 39 | rowItem.setHeight(position.getHeight()); 40 | adapter.add(rowItem); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/reuglar/RegularActivity.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.reuglar; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Rect; 5 | import android.os.Bundle; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.View; 8 | 9 | import com.app.tvrecyclerview.R; 10 | import com.app.tvrecyclerview.Utils; 11 | import com.app.tvrecyclerview.bean.Row; 12 | import com.app.tvrecyclerview.bean.itemPosition; 13 | import com.google.gson.Gson; 14 | 15 | import app.com.tvrecyclerview.FocusHighlightHelper; 16 | import app.com.tvrecyclerview.GridObjectAdapter; 17 | import app.com.tvrecyclerview.HorizontalGridView; 18 | import app.com.tvrecyclerview.RowItem; 19 | 20 | 21 | public class RegularActivity extends Activity { 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_regular); 26 | 27 | HorizontalGridView gridView = findViewById(R.id.id_grid_first); 28 | gridView.addItemDecoration(new SpaceItemDecoration()); 29 | gridView.setNumRows(2); 30 | GridObjectAdapter adapter = new GridObjectAdapter(new RegularPresenter(this)); 31 | gridView.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 32 | gridView.setAdapter(adapter); 33 | for (int i = 0; i < 10; i++) { 34 | adapter.add(new RowItem()); 35 | } 36 | 37 | 38 | HorizontalGridView gridView1 = findViewById(R.id.id_grid_second); 39 | String json1 = Utils.inputStreamToString(getResources().openRawResource(R.raw.horizonal_regular)); 40 | Row row1 = new Gson().fromJson(json1, Row.class); 41 | GridObjectAdapter adapter1 = new GridObjectAdapter(new RegularPresenter(this), 42 | row1.getRowSpacing(), row1.getColumnSpacing(), row1.getColumns(), 43 | row1.getAspectRatio()); 44 | gridView1.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 45 | gridView1.setAdapter(adapter1); 46 | for (itemPosition position : row1.getItems()) { 47 | RowItem rowItem = new RowItem(); 48 | rowItem.setX(position.getX()); 49 | rowItem.setY(position.getY()); 50 | rowItem.setWidth(position.getWidth()); 51 | rowItem.setHeight(position.getHeight()); 52 | adapter1.add(rowItem); 53 | } 54 | } 55 | 56 | private class SpaceItemDecoration extends RecyclerView.ItemDecoration { 57 | @Override 58 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 59 | outRect.right = 30; 60 | outRect.bottom = 30; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/reuglar/RegularPresenter.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.reuglar; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.view.ViewGroup; 6 | import android.widget.ImageView; 7 | 8 | import com.app.tvrecyclerview.Utils; 9 | 10 | import app.com.tvrecyclerview.Presenter; 11 | 12 | 13 | public class RegularPresenter extends Presenter { 14 | 15 | RegularPresenter(Context context) { 16 | super(context); 17 | } 18 | 19 | @Override 20 | public View onCreateView() { 21 | ImageView view = new ImageView(getContext()); 22 | view.setSelected(true); 23 | view.setFocusable(true); 24 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(423, 171); 25 | view.setLayoutParams(params); 26 | return view; 27 | } 28 | 29 | @Override 30 | public void onBindViewHolder(ViewHolder viewHolder, Object item) { 31 | viewHolder.view.setBackgroundColor(Utils.getRandColor()); 32 | } 33 | 34 | @Override 35 | public void onUnbindViewHolder(ViewHolder viewHolder) { 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/reuglar/RegularVerticalActivity.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.reuglar; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Rect; 5 | import android.os.Bundle; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.View; 8 | 9 | import com.app.tvrecyclerview.R; 10 | 11 | import app.com.tvrecyclerview.FocusHighlightHelper; 12 | import app.com.tvrecyclerview.GridObjectAdapter; 13 | import app.com.tvrecyclerview.RowItem; 14 | import app.com.tvrecyclerview.VerticalGridView; 15 | 16 | 17 | public class RegularVerticalActivity extends Activity { 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_regular_vertical); 22 | VerticalGridView gridView = findViewById(R.id.id_grid_vertical); 23 | 24 | 25 | gridView.addItemDecoration(new SpaceItemDecoration()); 26 | gridView.setNumColumns(3); 27 | GridObjectAdapter adapter = new GridObjectAdapter(new RegularVerticalPresenter(this)); 28 | gridView.setFocusZoomFactor(FocusHighlightHelper.ZOOM_FACTOR_SMALL); 29 | gridView.setAdapter(adapter); 30 | for (int i = 0; i < 15; i++) { 31 | adapter.add(new RowItem()); 32 | } 33 | } 34 | 35 | private class SpaceItemDecoration extends RecyclerView.ItemDecoration { 36 | @Override 37 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 38 | outRect.right = 30; 39 | outRect.bottom = 30; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/tvrecyclerview/reuglar/RegularVerticalPresenter.java: -------------------------------------------------------------------------------- 1 | package com.app.tvrecyclerview.reuglar; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.view.ViewGroup; 6 | import android.widget.ImageView; 7 | 8 | import com.app.tvrecyclerview.Utils; 9 | 10 | import app.com.tvrecyclerview.Presenter; 11 | 12 | 13 | public class RegularVerticalPresenter extends Presenter { 14 | 15 | public RegularVerticalPresenter(Context context) { 16 | super(context); 17 | } 18 | 19 | @Override 20 | public View onCreateView() { 21 | ImageView view = new ImageView(getContext()); 22 | view.setSelected(true); 23 | view.setFocusable(true); 24 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(413, 238); 25 | view.setLayoutParams(params); 26 | return view; 27 | } 28 | 29 | @Override 30 | public void onBindViewHolder(ViewHolder viewHolder, Object item) { 31 | viewHolder.view.setBackgroundColor(Utils.getRandColor()); 32 | } 33 | 34 | @Override 35 | public void onUnbindViewHolder(ViewHolder viewHolder) { 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/res/anim/anim_scale_big.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/anim/anim_scale_small.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/default_focus.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/default_focus.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic0.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic5.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic6.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic7.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henryblue/TvRecyclerView/6868bb887b9fd471edf0803b8c31791e668976f1/app/src/main/res/drawable/pic8.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/rl_shape_corner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/tv_shape_corner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_irregular.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_irregular_vertical.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 12 | 13 |