├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-hdpi │ │ │ │ ├── app_item_bg.9.png │ │ │ │ ├── item_shadow_v.9.png │ │ │ │ └── comm_bg_card_focus.9.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable │ │ │ │ ├── appcard_bg_selector.xml │ │ │ │ └── divider_bg.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── adapter_myapp_item.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── widget_my_app_header.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── maikoulin │ │ │ │ └── recyclerviewtv │ │ │ │ ├── ScreenUtil.java │ │ │ │ ├── MyAppCardView.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── CustNineDrawable.java │ │ │ │ ├── MyAppGridLayoutManager.java │ │ │ │ ├── MyAppRecyclerAdapter.java │ │ │ │ ├── BlowUpUtil.java │ │ │ │ ├── MyAppRecyclerHeaderView.java │ │ │ │ ├── BaseRecyclerAdapter.java │ │ │ │ ├── MyAppHeaderItemDecoration.java │ │ │ │ ├── MyAPPGridItemDecoration.java │ │ │ │ └── MyAppRecyclerView.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── maikoulin │ │ │ └── recyclerviewtv │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── maikoulin │ │ └── recyclerviewtv │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── screens └── GIF.gif ├── .idea ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── README.md ├── gradle.properties ├── .gitignore ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /screens/GIF.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/screens/GIF.gif -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RecyclerviewTv 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RecyclerViewTv 2 | RecyclerViewTv Used for displaying a list on the TV 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/app_item_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/drawable-hdpi/app_item_bg.9.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/item_shadow_v.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/drawable-hdpi/item_shadow_v.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/comm_bg_card_focus.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maikoulin/RecyclerViewTv/HEAD/app/src/main/res/drawable-hdpi/comm_bg_card_focus.9.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 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-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/appcard_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #00000000 7 | #ffffff 8 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/divider_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/test/java/com/maikoulin/recyclerviewtv/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 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() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/ScreenUtil.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by maikoulin on 2016/8/18. 7 | */ 8 | public class ScreenUtil { 9 | // dip->px 10 | public static int dip2px(Context context, float dipValue) { 11 | final float scale = context.getResources().getDisplayMetrics().density; 12 | return (int) (dipValue * scale + 0.5f); 13 | } 14 | 15 | // px->dip 16 | public static int px2dip(Context context, float pxValue) { 17 | final float scale = context.getResources().getDisplayMetrics().density; 18 | return (int) (pxValue / scale + 0.5f); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /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 D:\sdk\android-sdk/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 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | *.iml 3 | .gradle 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | ======= 12 | # Built application files 13 | *.apk 14 | *.ap_ 15 | 16 | # Files for the ART/Dalvik VM 17 | *.dex 18 | 19 | # Java class files 20 | *.class 21 | 22 | # Generated files 23 | bin/ 24 | gen/ 25 | out/ 26 | 27 | # Gradle files 28 | .gradle/ 29 | build/ 30 | 31 | # Local configuration file (sdk path, etc) 32 | local.properties 33 | 34 | # Proguard folder generated by Eclipse 35 | proguard/ 36 | 37 | # Log Files 38 | *.log 39 | 40 | # Android Studio Navigation editor temp files 41 | .navigation/ 42 | 43 | # Android Studio captures folder 44 | captures/ 45 | 46 | # Intellij 47 | *.iml 48 | .idea/workspace.xml 49 | 50 | # Keystore files 51 | *.jks 52 | >>>>>>> ee16f56a8fa9e35f44d5eb788a1375a8fa02d950 53 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/maikoulin/recyclerviewtv/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 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.maikoulin.recyclerviewtv", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/adapter_myapp_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 21 | 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.2" 6 | defaultConfig { 7 | applicationId "com.maikoulin.recyclerviewtv" 8 | minSdkVersion 17 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.0.1' 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.android.support:recyclerview-v7:25.0.1' 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/widget_my_app_header.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 15 | 16 | 22 | 23 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppCardView.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.LayoutInflater; 6 | import android.view.ViewGroup; 7 | import android.widget.FrameLayout; 8 | 9 | /** 10 | * Created by lmh on 2016/8/22. 11 | */ 12 | public class MyAppCardView extends FrameLayout { 13 | 14 | private boolean isHeader = false; 15 | 16 | public MyAppCardView(Context context) { 17 | super(context); 18 | } 19 | 20 | public MyAppCardView(Context context, boolean isHeader) { 21 | super(context); 22 | this.isHeader = isHeader; 23 | initView(); 24 | } 25 | 26 | public MyAppCardView(Context context, AttributeSet attrs) { 27 | super(context, attrs); 28 | } 29 | 30 | public MyAppCardView(Context context, AttributeSet attrs, int defStyleAttr) { 31 | super(context, attrs, defStyleAttr); 32 | } 33 | 34 | private void initView() { 35 | this.setFocusable(true); 36 | this.setClipChildren(false); 37 | LayoutParams flp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 38 | int effect = ScreenUtil.dip2px(getContext(), 11); 39 | if (isHeader) { 40 | flp.setMargins(effect, 0, effect, 0); 41 | } else { 42 | flp.setMargins(effect, effect, effect, effect); 43 | } 44 | this.setLayoutParams(flp); 45 | LayoutInflater.from(getContext()).inflate(R.layout.adapter_myapp_item, this); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.DefaultItemAnimator; 6 | import android.support.v7.widget.GridLayoutManager; 7 | import android.util.Log; 8 | 9 | import java.util.ArrayList; 10 | 11 | public class MainActivity extends AppCompatActivity implements MyAppRecyclerView.BorderListener { 12 | private static final String TAG = "MainActivity"; 13 | private GridLayoutManager mLayoutManager; 14 | private MyAppRecyclerAdapter mAdapter; 15 | private MyAppRecyclerView mRecyclerView; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | mRecyclerView = (MyAppRecyclerView) this.findViewById(R.id.rv_my_app_content); 22 | mRecyclerView.setHasFixedSize(true); 23 | mLayoutManager = new MyAppGridLayoutManager(this, 6); 24 | mAdapter = new MyAppRecyclerAdapter(this); 25 | mRecyclerView.setLayoutManager(mLayoutManager); 26 | mRecyclerView.setFocusable(false); 27 | mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 28 | mRecyclerView.setAdapter(mAdapter); 29 | mAdapter.setRecyclerView(mRecyclerView); 30 | mRecyclerView.setBorderListener(this); 31 | mRecyclerView.addItemDecoration(new MyAPPGridItemDecoration(this, true)); 32 | mAdapter.addDatas(getDatas()); 33 | } 34 | 35 | private ArrayList getDatas() { 36 | ArrayList datas = new ArrayList<>(); 37 | for (int i = 0; i < 60; i++) { 38 | datas.add(" 第" + i + "个数据"); 39 | } 40 | return datas; 41 | } 42 | 43 | @Override 44 | public boolean onKeyBottomDown() { 45 | Log.e(TAG, "onKeyBottomDown"); 46 | return false; 47 | } 48 | 49 | @Override 50 | public boolean onKeyTopUp() { 51 | Log.e(TAG, "onKeyTopUp"); 52 | return false; 53 | } 54 | 55 | @Override 56 | public boolean onKeyLeftEnd() { 57 | Log.e(TAG, "onKeyLeftEnd"); 58 | return false; 59 | } 60 | 61 | @Override 62 | public boolean onKeyRightEnd() { 63 | Log.e(TAG, "onKeyRightEnd"); 64 | return false; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/CustNineDrawable.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.ColorFilter; 6 | import android.graphics.PixelFormat; 7 | import android.graphics.Rect; 8 | import android.graphics.drawable.Drawable; 9 | 10 | /** 11 | * Created by lmh on 2016/1/13. 12 | */ 13 | public class CustNineDrawable extends Drawable { 14 | private Drawable mScaleFocusDrawable = null; 15 | 16 | public CustNineDrawable(Context context) { 17 | mScaleFocusDrawable = context.getResources().getDrawable(R.drawable.item_shadow_v); 18 | } 19 | 20 | public void setFocusDrawable(Context context, int drawableId) { 21 | mScaleFocusDrawable = context.getResources().getDrawable(drawableId); 22 | } 23 | 24 | @Override 25 | public void draw(Canvas canvas) { 26 | 27 | //LogDebugUtil.d("nine", getBounds().toString()); 28 | mScaleFocusDrawable.setBounds(getBounds()); 29 | mScaleFocusDrawable.draw(canvas); 30 | } 31 | 32 | @Override 33 | public void setAlpha(int alpha) { 34 | 35 | } 36 | 37 | @Override 38 | public void setColorFilter(ColorFilter cf) { 39 | 40 | } 41 | 42 | 43 | @Override 44 | public int getOpacity() { 45 | return PixelFormat.TRANSLUCENT; 46 | } 47 | 48 | 49 | public void setBounds(int left, int top, int right, int bottom) { 50 | // LogDebugUtil.d("nine", "src = " + right + " bottom = " + bottom); 51 | Rect padding = new Rect(); 52 | mScaleFocusDrawable.getPadding(padding); 53 | // LogDebugUtil.e("setBounds", padding.toString()); 54 | // 55 | // LogDebugUtil.e("setBounds", "left=" + left + "top=" + top + "right=" + right + "bottom=" + bottom); 56 | int l = (left - padding.left); 57 | int t = (top - padding.top); 58 | int r = (right + padding.right); 59 | int b = (bottom + padding.bottom); 60 | 61 | super.setBounds(l, t, r, b); 62 | } 63 | 64 | public int getSrcLeftPadding() { 65 | Rect padding = new Rect(); 66 | mScaleFocusDrawable.getPadding(padding); 67 | return padding.left; 68 | } 69 | 70 | public int getSrcRightPadding() { 71 | Rect padding = new Rect(); 72 | mScaleFocusDrawable.getPadding(padding); 73 | return padding.right; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppGridLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.GridLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.AttributeSet; 7 | import android.view.View; 8 | 9 | /** 10 | * Created by lmh on 2016/8/15. 11 | */ 12 | public class MyAppGridLayoutManager extends GridLayoutManager { 13 | 14 | private double speedDownRatio = 1.11; //向下滑动时多移动的距离倍数 15 | private double speedUpRatio = 1.2; // 向上滑动时多移动的距离倍数 16 | 17 | public MyAppGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 18 | super(context, attrs, defStyleAttr, defStyleRes); 19 | } 20 | 21 | public MyAppGridLayoutManager(Context context, int spanCount) { 22 | super(context, spanCount); 23 | } 24 | 25 | public MyAppGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) { 26 | super(context, spanCount, orientation, reverseLayout); 27 | } 28 | 29 | 30 | @Override 31 | public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) { 32 | return super.onFocusSearchFailed(focused, focusDirection, recycler, state); 33 | // LogDebugUtil.e("onFocusSearchFailed", "onFocusSearchFailed"); 34 | // return null; 35 | } 36 | 37 | @Override 38 | public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { 39 | super.smoothScrollToPosition(recyclerView, state, position); 40 | } 41 | 42 | @Override 43 | public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { 44 | double speedRatio; 45 | if (dy > 0) { 46 | speedRatio = speedDownRatio; 47 | } else { 48 | speedRatio = speedUpRatio; 49 | } 50 | int a = super.scrollVerticallyBy((int) (speedRatio * dy), recycler, state); 51 | if (a == (int) (speedRatio * dy)) { 52 | return dy; 53 | } 54 | return a; 55 | 56 | } 57 | 58 | public void setSpeedDownRatio(double speedDownRatio) { 59 | this.speedDownRatio = speedDownRatio; 60 | } 61 | 62 | public void setSpeedUpRatio(double speedUpRatio) { 63 | this.speedUpRatio = speedUpRatio; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | /** 11 | * Created by lmh on 2016/8/12. 12 | */ 13 | public class MyAppRecyclerAdapter extends BaseRecyclerAdapter { 14 | private BlowUpUtil blowUpUtil; 15 | private Context mContext; 16 | private boolean isHeader = false; 17 | private RecyclerView recyclerView; 18 | 19 | public MyAppRecyclerAdapter(Context mContext) { 20 | this.mContext = mContext; 21 | blowUpUtil = new BlowUpUtil(mContext); 22 | blowUpUtil.setFocusDrawable(R.drawable.comm_bg_card_focus); 23 | } 24 | 25 | public void setRecyclerView(RecyclerView recyclerView) { 26 | this.recyclerView = recyclerView; 27 | } 28 | 29 | public void isHeader(boolean isHeader) { 30 | this.isHeader = isHeader; 31 | } 32 | 33 | @Override 34 | public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) { 35 | View layout = new MyAppCardView(parent.getContext(), isHeader); 36 | layout.setFocusable(true); 37 | return new MyHolder(layout); 38 | } 39 | 40 | @Override 41 | public int getItemCount() { 42 | if (isHeader) { 43 | return mDatas.size() <= 6 ? mDatas.size() : 6; 44 | } 45 | return super.getItemCount(); 46 | } 47 | 48 | @Override 49 | public void onBind(final RecyclerView.ViewHolder viewHolder, final int RealPosition, final String data) { 50 | if (viewHolder instanceof MyHolder) { 51 | ((MyHolder) viewHolder).tvDescribe.setText(data); 52 | viewHolder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() { 53 | @Override 54 | public void onFocusChange(View v, boolean hasFocus) { 55 | if (hasFocus) { 56 | blowUpUtil.setScaleUp(v); 57 | } else { 58 | blowUpUtil.setScaleDown(v); 59 | } 60 | } 61 | }); 62 | } 63 | } 64 | 65 | @Override 66 | public void onHeadBind(RecyclerView.ViewHolder viewHolder) { 67 | 68 | } 69 | 70 | public BlowUpUtil getBlowUpUtil() { 71 | return blowUpUtil; 72 | } 73 | 74 | private class MyHolder extends BaseRecyclerAdapter.Holder { 75 | private TextView tvDescribe; 76 | 77 | 78 | MyHolder(View itemView) { 79 | super(itemView); 80 | tvDescribe = (TextView) itemView.findViewById(R.id.tv_describe); 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/BlowUpUtil.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.view.animation.Animation; 6 | import android.view.animation.ScaleAnimation; 7 | 8 | 9 | /** 10 | * Created by lmh on 2016/1/13. 11 | */ 12 | public class BlowUpUtil { 13 | private float SCALE_FACTOR = 1.05f; 14 | private float SCALE_FACTOR_2 = 1.08f; 15 | 16 | private ScaleAnimation mAnimation; 17 | private ScaleAnimation mAnimationUpDown; 18 | private Context context; 19 | private CustNineDrawable custNineDrawable; 20 | private View lastView; 21 | private int itemDistanceEnd = 200;//default 30dp 22 | 23 | public boolean isAnimationAble = true; 24 | 25 | public BlowUpUtil(Context context) { 26 | this.context = context; 27 | 28 | custNineDrawable = new CustNineDrawable(context); 29 | if (isAnimationAble) { 30 | mAnimation = new ScaleAnimation(1.00f, SCALE_FACTOR, 1.00f, SCALE_FACTOR, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 31 | 32 | mAnimation.setFillBefore(false); 33 | mAnimation.setDuration(100); 34 | mAnimation.setFillAfter(true); 35 | 36 | mAnimationUpDown = new ScaleAnimation(SCALE_FACTOR, SCALE_FACTOR_2, SCALE_FACTOR, SCALE_FACTOR_2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 37 | mAnimationUpDown.setDuration(50); 38 | mAnimationUpDown.setFillAfter(true); 39 | mAnimationUpDown.setFillEnabled(true); 40 | mAnimationUpDown.setRepeatCount(1); 41 | mAnimationUpDown.setRepeatMode(Animation.REVERSE); 42 | } 43 | 44 | } 45 | 46 | public void setFocusDrawable(int drawableId) { 47 | if (custNineDrawable != null) { 48 | custNineDrawable.setFocusDrawable(context, drawableId); 49 | } 50 | } 51 | 52 | public CustNineDrawable getCustNineDrawable() { 53 | return custNineDrawable; 54 | } 55 | 56 | public void setScaleUp(View view) { 57 | 58 | if (lastView != null) { 59 | view.setBackgroundResource(R.color.transparent); 60 | view.clearAnimation(); 61 | lastView = null; 62 | } 63 | 64 | lastView = view; 65 | view.setBackgroundDrawable(custNineDrawable); 66 | 67 | if (!isAnimationAble) { 68 | return; 69 | } 70 | view.setAnimation(mAnimation); 71 | view.startAnimation(mAnimation); 72 | view.bringToFront(); 73 | } 74 | 75 | public void setScaleDown(View view) { 76 | 77 | if (view != null) { 78 | view.setBackgroundResource(R.color.transparent); 79 | view.clearAnimation(); 80 | lastView = null; 81 | } else if (lastView != null) { 82 | lastView.setBackgroundResource(R.color.transparent); 83 | lastView.clearAnimation(); 84 | lastView = null; 85 | } 86 | } 87 | 88 | public void setScaleDown() { 89 | 90 | if (lastView != null) { 91 | setScaleDown(lastView); 92 | } 93 | } 94 | 95 | public void setScaleUpDown(View view) { 96 | if (!isAnimationAble) { 97 | return; 98 | } 99 | if (view != null) { 100 | view.startAnimation(mAnimationUpDown); 101 | } 102 | } 103 | 104 | public int getDistance(View view) { 105 | int scaleDistance = ((int) (view.getWidth() * (SCALE_FACTOR - 1))); 106 | return scaleDistance + custNineDrawable.getSrcLeftPadding(); 107 | } 108 | 109 | /** 110 | * 距离右边 111 | * 112 | * @param view 113 | * @return 114 | */ 115 | public int getDistanceRight(View view) { 116 | int scaleDistance = 0; 117 | if (isAnimationAble) { 118 | scaleDistance = ((int) (view.getWidth() * (SCALE_FACTOR - 1))); 119 | } 120 | 121 | return scaleDistance + custNineDrawable.getSrcRightPadding(); 122 | } 123 | 124 | public int getItemDistanceEnd() { 125 | 126 | return itemDistanceEnd; 127 | } 128 | 129 | public void setItemDistanceEnd(int itemDistanceEnd) { 130 | 131 | this.itemDistanceEnd = itemDistanceEnd; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppRecyclerHeaderView.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.DefaultItemAnimator; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.widget.LinearLayout; 10 | import android.widget.TextView; 11 | 12 | import java.util.ArrayList; 13 | 14 | /** 15 | * Created by lmh on 2016/8/19. 16 | */ 17 | public class MyAppRecyclerHeaderView extends LinearLayout implements MyAppRecyclerView.BorderListener { 18 | 19 | private MyAppRecyclerView rvContent; 20 | private ArrayList mDatas = new ArrayList<>(); 21 | private GridLayoutManager mLayoutManager; 22 | private MyAppRecyclerAdapter mAdapter; 23 | private HeaderBorderListener headerBorderListener; 24 | private TextView tvRecent; 25 | public MyAppRecyclerHeaderView(Context context) { 26 | super(context); 27 | initView(); 28 | } 29 | public MyAppRecyclerHeaderView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | initView(); 32 | } 33 | 34 | public MyAppRecyclerHeaderView(Context context, AttributeSet attrs, int defStyleAttr) { 35 | super(context, attrs, defStyleAttr); 36 | initView(); 37 | } 38 | 39 | public void positionRequestFocus(int position) { 40 | if (position < 0) { 41 | return; 42 | } 43 | View view = rvContent.getChildAt(position); 44 | if (view != null) { 45 | view.requestFocus(); 46 | } else { 47 | rvContent.getChildAt(rvContent.getChildCount() - 1).requestFocus(); 48 | } 49 | } 50 | 51 | public void setHeaderBorderListener(HeaderBorderListener headerBorderListener) { 52 | this.headerBorderListener = headerBorderListener; 53 | } 54 | 55 | 56 | 57 | private void initView() { 58 | this.setOrientation(VERTICAL); 59 | this.setClipChildren(false); 60 | this.setClipToPadding(false); 61 | this.setFocusable(false); 62 | View view = LayoutInflater.from(getContext()).inflate(R.layout.widget_my_app_header, this); 63 | rvContent = (MyAppRecyclerView) view.findViewById(R.id.rv_my_app_header_content); 64 | tvRecent = (TextView) view.findViewById(R.id.tv_my_app_header_recent); 65 | rvContent.setClipChildren(false); 66 | rvContent.setClipToPadding(false); 67 | rvContent.setHasFixedSize(true); 68 | mLayoutManager = new MyAppGridLayoutManager(getContext(), 6); 69 | rvContent.setLayoutManager(mLayoutManager); 70 | rvContent.setFocusable(false); 71 | rvContent.setItemAnimator(new DefaultItemAnimator()); 72 | mAdapter = new MyAppRecyclerAdapter(getContext()); 73 | mAdapter.isHeader(true); 74 | rvContent.addItemDecoration(new MyAppHeaderItemDecoration(getContext(), true)); 75 | rvContent.setAdapter(mAdapter); 76 | rvContent.setBorderListener(this); 77 | setItemListener(); 78 | } 79 | 80 | 81 | public void initData(ArrayList datas) { 82 | mDatas.addAll(datas); 83 | mAdapter.addDatas(mDatas); 84 | if (datas != null && !datas.isEmpty()) { 85 | rvContent.setVisibility(GONE); 86 | tvRecent.setVisibility(GONE); 87 | } 88 | } 89 | 90 | private void setItemListener() { 91 | mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() { 92 | @Override 93 | public void onItemClick(View view, int position, String data) { 94 | 95 | 96 | } 97 | }); 98 | } 99 | @Override 100 | public boolean onKeyBottomDown() { 101 | return false; 102 | } 103 | 104 | @Override 105 | public boolean onKeyTopUp() { 106 | return true; 107 | } 108 | 109 | @Override 110 | public boolean onKeyLeftEnd() { 111 | if (headerBorderListener != null) { 112 | return headerBorderListener.onKeyHeaderLeftEnd(); 113 | } 114 | return false; 115 | } 116 | 117 | @Override 118 | public boolean onKeyRightEnd() { 119 | if (headerBorderListener != null) { 120 | return headerBorderListener.onKeyHeaderRightEnd(); 121 | } 122 | return false; 123 | } 124 | 125 | public interface HeaderBorderListener { 126 | boolean onKeyHeaderLeftEnd(); 127 | 128 | boolean onKeyHeaderRightEnd(); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/BaseRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.support.v7.widget.GridLayoutManager; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.support.v7.widget.StaggeredGridLayoutManager; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import java.util.ArrayList; 10 | 11 | /** 12 | * Created by lmh on 2016/8/12. 13 | */ 14 | public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter { 15 | public static final int TYPE_HEADER = 0; 16 | public static final int TYPE_NORMAL = 1; 17 | protected ArrayList mDatas = null; 18 | private View mHeaderView; 19 | private OnItemClickListener mListener; 20 | 21 | public void setOnItemClickListener(OnItemClickListener li) { 22 | mListener = li; 23 | } 24 | 25 | public void setHeaderView(View headerView) { 26 | mHeaderView = headerView; 27 | notifyItemInserted(0); 28 | } 29 | 30 | public View getHeaderView() { 31 | return mHeaderView; 32 | } 33 | 34 | public void addDatas(ArrayList datas) { 35 | mDatas = datas; 36 | notifyDataSetChanged(); 37 | } 38 | 39 | @Override 40 | public int getItemViewType(int position) { 41 | if (mHeaderView == null) return TYPE_NORMAL; 42 | if (position == 0) return TYPE_HEADER; 43 | return TYPE_NORMAL; 44 | } 45 | 46 | @Override 47 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) { 48 | if (mHeaderView != null && viewType == TYPE_HEADER) return new Holder(mHeaderView); 49 | return onCreate(parent, viewType); 50 | } 51 | 52 | @Override 53 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { 54 | if (getItemViewType(position) == TYPE_HEADER) { 55 | onHeadBind(viewHolder); 56 | return; 57 | } 58 | final int pos = getRealPosition(viewHolder); 59 | final T data = mDatas.get(pos); 60 | onBind(viewHolder, pos, data); 61 | if (mListener != null) { 62 | viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 63 | @Override 64 | public void onClick(View v) { 65 | mListener.onItemClick(v, pos, data); 66 | } 67 | }); 68 | } 69 | } 70 | 71 | @Override 72 | public void onAttachedToRecyclerView(RecyclerView recyclerView) { 73 | super.onAttachedToRecyclerView(recyclerView); 74 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); 75 | if (manager instanceof GridLayoutManager) { 76 | final GridLayoutManager gridManager = ((GridLayoutManager) manager); 77 | gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 78 | @Override 79 | public int getSpanSize(int position) { 80 | return getItemViewType(position) == TYPE_HEADER 81 | ? gridManager.getSpanCount() : 1; 82 | } 83 | }); 84 | } 85 | } 86 | 87 | @Override 88 | public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) { 89 | super.onViewAttachedToWindow(holder); 90 | ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); 91 | if (lp != null 92 | && lp instanceof StaggeredGridLayoutManager.LayoutParams 93 | && holder.getLayoutPosition() == 0) { 94 | StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; 95 | p.setFullSpan(true); 96 | } 97 | } 98 | 99 | public int getRealPosition(RecyclerView.ViewHolder holder) { 100 | int position = holder.getLayoutPosition(); 101 | return mHeaderView == null ? position : position - 1; 102 | } 103 | 104 | @Override 105 | public int getItemCount() { 106 | return mHeaderView == null ? mDatas.size() : mDatas.size() + 1; 107 | } 108 | 109 | public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType); 110 | 111 | public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data); 112 | 113 | public abstract void onHeadBind(RecyclerView.ViewHolder viewHolder); 114 | 115 | public class Holder extends RecyclerView.ViewHolder { 116 | public Holder(View itemView) { 117 | super(itemView); 118 | } 119 | } 120 | 121 | public interface OnItemClickListener { 122 | void onItemClick(View view, int position, T data); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppHeaderItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.support.v7.widget.GridLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.support.v7.widget.StaggeredGridLayoutManager; 11 | import android.view.View; 12 | 13 | /** 14 | * Created by lmh on 2016/8/26. 15 | */ 16 | public class MyAppHeaderItemDecoration extends RecyclerView.ItemDecoration { 17 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 18 | private Drawable mDivider; 19 | private boolean hasHeader; 20 | 21 | public MyAppHeaderItemDecoration(Context context) { 22 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 23 | mDivider = a.getDrawable(0); 24 | a.recycle(); 25 | } 26 | 27 | public MyAppHeaderItemDecoration(Context context, boolean header) { 28 | this(context); 29 | hasHeader = header; 30 | } 31 | 32 | @Override 33 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 34 | 35 | drawHorizontal(c, parent); 36 | drawVertical(c, parent); 37 | 38 | } 39 | 40 | private int getSpanCount(RecyclerView parent) { 41 | // 列数 42 | int spanCount = -1; 43 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 44 | if (layoutManager instanceof GridLayoutManager) { 45 | 46 | spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); 47 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 48 | spanCount = ((StaggeredGridLayoutManager) layoutManager) 49 | .getSpanCount(); 50 | } 51 | return spanCount; 52 | } 53 | 54 | public void drawHorizontal(Canvas c, RecyclerView parent) { 55 | int childCount = parent.getChildCount(); 56 | for (int i = 0; i < childCount; i++) { 57 | final View child = parent.getChildAt(i); 58 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 59 | .getLayoutParams(); 60 | final int left = child.getLeft() - params.leftMargin; 61 | final int right = child.getRight() + params.rightMargin 62 | + mDivider.getIntrinsicWidth(); 63 | final int top = child.getBottom() + params.bottomMargin; 64 | final int bottom = top + mDivider.getIntrinsicHeight(); 65 | mDivider.setBounds(left, top, right, bottom); 66 | mDivider.draw(c); 67 | } 68 | } 69 | 70 | public void drawVertical(Canvas c, RecyclerView parent) { 71 | final int childCount = parent.getChildCount(); 72 | for (int i = 0; i < childCount; i++) { 73 | final View child = parent.getChildAt(i); 74 | 75 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 76 | .getLayoutParams(); 77 | final int top = child.getTop() - params.topMargin; 78 | final int bottom = child.getBottom() + params.bottomMargin; 79 | final int left = child.getRight() + params.rightMargin; 80 | final int right = left + mDivider.getIntrinsicWidth(); 81 | 82 | mDivider.setBounds(left, top, right, bottom); 83 | mDivider.draw(c); 84 | } 85 | } 86 | 87 | private boolean isLastCount(RecyclerView parent, int pos, int spanCount, 88 | int childCount) { 89 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 90 | if (layoutManager instanceof GridLayoutManager) { 91 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 92 | { 93 | return true; 94 | } 95 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 96 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 97 | .getOrientation(); 98 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 99 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 100 | { 101 | return true; 102 | } 103 | } else { 104 | childCount = childCount - childCount % spanCount; 105 | if (pos >= childCount)// 如果是最后一列,则不需要绘制右边 106 | return true; 107 | } 108 | } 109 | return false; 110 | } 111 | 112 | @Override 113 | public void getItemOffsets(Rect outRect, View view, 114 | RecyclerView parent, RecyclerView.State state) { 115 | int position = parent.getChildAdapterPosition(view); 116 | int spanCount = getSpanCount(parent); 117 | int childCount = parent.getAdapter().getItemCount(); 118 | 119 | if (isLastCount(parent, position, spanCount, childCount)) { 120 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 121 | } else { 122 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 123 | 0); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAPPGridItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.support.v7.widget.GridLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.support.v7.widget.StaggeredGridLayoutManager; 11 | import android.view.View; 12 | 13 | /** 14 | * Created by maikoulin on 2016/8/18. 15 | */ 16 | public class MyAPPGridItemDecoration extends RecyclerView.ItemDecoration { 17 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 18 | private Drawable mDivider; 19 | private boolean hasHeader; 20 | private Context mContext; 21 | 22 | public MyAPPGridItemDecoration(Context context) { 23 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 24 | mDivider = a.getDrawable(0); 25 | a.recycle(); 26 | mContext = context; 27 | } 28 | 29 | public MyAPPGridItemDecoration(Context context, boolean header) { 30 | this(context); 31 | hasHeader = header; 32 | } 33 | 34 | @Override 35 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 36 | 37 | drawHorizontal(c, parent); 38 | drawVertical(c, parent); 39 | 40 | } 41 | 42 | private int getSpanCount(RecyclerView parent) { 43 | // 列数 44 | int spanCount = -1; 45 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 46 | if (layoutManager instanceof GridLayoutManager) { 47 | 48 | spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); 49 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 50 | spanCount = ((StaggeredGridLayoutManager) layoutManager) 51 | .getSpanCount(); 52 | } 53 | return spanCount; 54 | } 55 | 56 | public void drawHorizontal(Canvas c, RecyclerView parent) { 57 | int childCount = parent.getChildCount(); 58 | for (int i = 0; i < childCount; i++) { 59 | final View child = parent.getChildAt(i); 60 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 61 | .getLayoutParams(); 62 | final int left = child.getLeft() - params.leftMargin; 63 | final int right = child.getRight() + params.rightMargin 64 | + mDivider.getIntrinsicWidth(); 65 | final int top = child.getBottom() + params.bottomMargin; 66 | final int bottom = top + mDivider.getIntrinsicHeight(); 67 | mDivider.setBounds(left, top, right, bottom); 68 | mDivider.draw(c); 69 | } 70 | } 71 | 72 | public void drawVertical(Canvas c, RecyclerView parent) { 73 | final int childCount = parent.getChildCount(); 74 | for (int i = 0; i < childCount; i++) { 75 | final View child = parent.getChildAt(i); 76 | 77 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 78 | .getLayoutParams(); 79 | final int top = child.getTop() - params.topMargin; 80 | final int bottom = child.getBottom() + params.bottomMargin; 81 | final int left = child.getRight() + params.rightMargin; 82 | final int right = left + mDivider.getIntrinsicWidth(); 83 | 84 | mDivider.setBounds(left, top, right, bottom); 85 | mDivider.draw(c); 86 | } 87 | } 88 | 89 | private boolean isLastCount(RecyclerView parent, int pos, int spanCount, 90 | int childCount) { 91 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 92 | if (layoutManager instanceof GridLayoutManager) { 93 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 94 | { 95 | return true; 96 | } 97 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 98 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 99 | .getOrientation(); 100 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 101 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 102 | { 103 | return true; 104 | } 105 | } else { 106 | childCount = childCount - childCount % spanCount; 107 | if (pos >= childCount)// 如果是最后一列,则不需要绘制右边 108 | return true; 109 | } 110 | } 111 | return false; 112 | } 113 | 114 | @Override 115 | public void getItemOffsets(Rect outRect, View view, 116 | RecyclerView parent, RecyclerView.State state) { 117 | int position = parent.getChildAdapterPosition(view); 118 | 119 | int spanCount = getSpanCount(parent); 120 | int childCount = parent.getAdapter().getItemCount(); 121 | 122 | int pos = position; 123 | 124 | if (hasHeader) { 125 | if (position == 0) { 126 | outRect.set(0, 0, 0, 0); 127 | return; 128 | } else { 129 | pos = position - 1; 130 | } 131 | } 132 | int effect = ScreenUtil.dip2px(mContext, 6); 133 | if (isLastCount(parent, pos, spanCount, childCount)) { 134 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight() + effect); 135 | } else { 136 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 137 | mDivider.getIntrinsicHeight() + effect); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /app/src/main/java/com/maikoulin/recyclerviewtv/MyAppRecyclerView.java: -------------------------------------------------------------------------------- 1 | package com.maikoulin.recyclerviewtv; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.AttributeSet; 8 | import android.util.Log; 9 | import android.view.KeyEvent; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.ViewParent; 13 | 14 | /** 15 | * Created by lmh on 2016/8/15. 16 | */ 17 | public class MyAppRecyclerView extends RecyclerView { 18 | private BorderListener borderListener; 19 | private double scale = 1; 20 | private boolean isHeader = false; 21 | private boolean headerIsNull = false; 22 | 23 | public MyAppRecyclerView(Context context) { 24 | super(context); 25 | } 26 | 27 | public MyAppRecyclerView(Context context, @Nullable AttributeSet attrs) { 28 | super(context, attrs); 29 | } 30 | 31 | public MyAppRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { 32 | super(context, attrs, defStyle); 33 | } 34 | 35 | 36 | @Override 37 | protected int getChildDrawingOrder(int childCount, int i) { 38 | return super.getChildDrawingOrder(childCount, i); 39 | } 40 | 41 | @Override 42 | public boolean dispatchKeyEvent(KeyEvent event) { 43 | if (event.getAction() == KeyEvent.ACTION_DOWN) { 44 | return isBorder(event); 45 | } 46 | return super.dispatchKeyEvent(event); 47 | } 48 | 49 | public void addHeader() { 50 | isHeader = true; 51 | } 52 | 53 | public void setHeaderIsNull(boolean headerIsNull) { 54 | this.headerIsNull = headerIsNull; 55 | } 56 | 57 | public boolean isHeaderIsNull() { 58 | return headerIsNull; 59 | } 60 | 61 | /** 62 | * 判断焦点是否在边界 63 | * 64 | * @param event 当前焦点的下个事件 65 | * @return true or false 66 | */ 67 | protected boolean isBorder(KeyEvent event) { 68 | int focusDirection = event.getKeyCode(); 69 | View view = this.getFocusedChild(); 70 | LayoutManager layoutManager = this.getLayoutManager(); 71 | int focusPos = layoutManager.getPosition(view); 72 | if (layoutManager instanceof GridLayoutManager) { 73 | GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; 74 | int spanCount = gridLayoutManager.getSpanCount(); 75 | int itemCount = layoutManager.getItemCount(); 76 | int rowCount; 77 | int row; 78 | int span; 79 | if (isHeader && itemCount != 1) { 80 | rowCount = (int) (Math.ceil((double) (itemCount - 1) / spanCount) + 1); 81 | if (focusPos != 0) { 82 | row = (focusPos - 1) / spanCount + 2; 83 | } else { 84 | row = (focusPos - 1) / spanCount + 1; 85 | } 86 | span = (focusPos - 1) % spanCount + 1; 87 | } else { 88 | rowCount = (int) Math.ceil((double) itemCount / spanCount); 89 | row = focusPos / spanCount + 1; 90 | span = focusPos % spanCount + 1; 91 | } 92 | if (event.hasNoModifiers()) { 93 | switch (focusDirection) { 94 | case KeyEvent.KEYCODE_DPAD_DOWN: 95 | if (row == rowCount) { 96 | return borderListener.onKeyBottomDown(); 97 | } else { 98 | //处理长按焦点错误问题; 99 | View nextView = view.focusSearch(View.FOCUS_DOWN); 100 | if (nextView != null) { 101 | //过虑还没绘制完成的view和正在修改的view 102 | if (!nextView.willNotDraw() || nextView.isDirty()) { 103 | return true; 104 | } 105 | // 这个根据布局的需求进行调整,用于解决长按遥控器,焦点跑掉的问题 106 | if (nextView.getTop() > ScreenUtil.dip2px(getContext(), 680)) { 107 | return true; 108 | } 109 | } 110 | } 111 | break; 112 | case KeyEvent.KEYCODE_DPAD_UP: 113 | if (row == 1) { 114 | return borderListener.onKeyTopUp(); 115 | } else if (row == 2 && headerIsNull) { 116 | return borderListener.onKeyTopUp(); 117 | } else { 118 | //处理长按焦点错误问题 119 | View nextView = view.focusSearch(View.FOCUS_UP); 120 | if (nextView != null) { 121 | if (!nextView.willNotDraw()) { 122 | return true; 123 | } 124 | // boolean isChild = false; 125 | // for (ViewParent parent = nextView.getParent(); parent instanceof ViewGroup; 126 | // parent = parent.getParent()) { 127 | // if (parent == this) { 128 | // isChild = true; 129 | // break; 130 | // } 131 | // } 132 | // if (!isChild) { 133 | // if (row == 2) { 134 | // this.smoothScrollToPosition(1); 135 | // ((MyAppRecyclerAdapter) getAdapter()).headerViewPositionRequestFocus(focusPos - 1); 136 | // } 137 | // if (row > 2) { 138 | // int newPosition = focusPos - spanCount; 139 | // this.smoothScrollToPosition(newPosition); 140 | // ((MyAppRecyclerAdapter) getAdapter()).setRequestFocusPosition(newPosition - 1); 141 | // } 142 | // return true; 143 | // } 144 | } 145 | } 146 | break; 147 | case KeyEvent.KEYCODE_DPAD_RIGHT: 148 | if (span == spanCount) { 149 | return borderListener.onKeyRightEnd(); 150 | } 151 | break; 152 | case KeyEvent.KEYCODE_DPAD_LEFT: 153 | if (span == 1) { 154 | return borderListener.onKeyLeftEnd(); 155 | } 156 | break; 157 | } 158 | } 159 | } 160 | 161 | return super.dispatchKeyEvent(event); 162 | } 163 | 164 | public void setflingScale(double scale) { 165 | this.scale = scale; 166 | } 167 | 168 | public void setBorderListener(BorderListener borderListener) { 169 | this.borderListener = borderListener; 170 | } 171 | 172 | @Override 173 | public boolean fling(int velocityX, int velocityY) { 174 | velocityY *= scale; 175 | return super.fling(velocityX, velocityY); 176 | } 177 | 178 | 179 | public interface BorderListener { 180 | boolean onKeyBottomDown(); 181 | 182 | boolean onKeyTopUp(); 183 | 184 | boolean onKeyLeftEnd(); 185 | 186 | boolean onKeyRightEnd(); 187 | } 188 | } 189 | --------------------------------------------------------------------------------