android TV app适配遥控器思路,recycleview选中放大

背景:

1、当遥控器遥控盒子,app内是有一套机制,响应遥控器的操作;

2、要实现遥控器选中的效果,必须要设置setOnFocusChangeListener方法,另外一个就是设置view的setOnClickListener方法;设置完之后,就可以直接有遥控器选中的状态;

需要做的就是:

1、activity中,普通view的处理:

直接监听该view的"setOnFocusChangeListener"方法,如下:

复制代码
 imgTitle.setOnFocusChangeListener(this);



实现方法处理:

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            //获焦后放大1.2倍
            ViewCompat.animate(view).scaleX(1.1f).scaleY(1.1f).translationZ(1.1f).start();
        } else {
            //丢失焦点后缩回正常
            ViewCompat.animate(view).scaleX(1.0f).scaleY(1.0f).translationZ(1.0f).start();
        }
    }

这样就可以响应遥控器的操作了,并且还有失去、获取焦点,view放大缩小的动画;

view的背景,都添加一个draw的xml,如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape>
            <corners android:radius="3dp" />
            <stroke android:width="3dp" android:color="@color/white" />
            <solid android:color="@color/transparent" />
            <padding android:top="3dp"
                android:bottom="3dp"
                android:left="3dp"
                android:right="3dp"/>
        </shape>
    </item>
    <item android:state_focused="false">
        <shape>
            <corners android:radius="3dp" />
            <stroke android:width="1dp" android:color="@color/transparent" />
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

2、activity中,是recycleview时,应该如下处理:

先设置监听,

复制代码
        recyclerViewTop.setOnFocusChangeListener(this);

再将adapter中的item布局中,只设置一个view的focusable是true,其他的都设置成false,这样,遥控器就可以进行选择了,同时遥控器中的ok键,就是click事件;

布局如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="200dp">

    <ImageView
        android:id="@+id/item_img_left_line"
        android:layout_width="15dp"
        android:focusable="false"
        android:layout_height="1dp"/>
    <ImageView
        android:id="@+id/item_img_top_line"
        android:layout_width="1dp"
        android:layout_height="15dp"
        android:focusable="false"
        />

    <RelativeLayout
        android:id="@+id/item_layout_main"
        android:layout_width="wrap_content"
        android:layout_height="105dp"
        android:layout_toRightOf="@id/item_img_left_line"
        android:layout_below="@id/item_img_top_line"
        android:focusable="false"
        android:background="@color/green">
        <ImageView
            android:id="@+id/item_img_background"
            android:layout_width="match_parent"
            android:layout_height="105dp"
            android:background="@drawable/btn_blue_round_line_selector"
            android:focusable="true"
            />

        <TextView
            android:id="@+id/item_tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/app_name"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"

            android:focusable="false"
            />
    </RelativeLayout>

</RelativeLayout>

在activity中,需要对遥控器,做特殊监听的,以下:

复制代码
@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        int action = event.getAction();
        return handleKeyEvent(action, keyCode)||super.dispatchKeyEvent(event);
    }
    private boolean handleKeyEvent(int action, int keyCode) {
        if (action != KeyEvent.ACTION_DOWN)
            return false;
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:

            case KeyEvent.KEYCODE_DPAD_CENTER:
              	 //确定键enter
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
            	//向下键
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
            	//向上键
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
            	//向左键
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            	//向右键
                break;
            default:
                break;
        }
        return false;
    }

Recycleview中的itemview,也可以设置放大缩小的动态图

相关推荐
studyForMokey1 小时前
【Android面试】RecylerView专题
android·spring·面试
android_cai_niao3 小时前
Android中有什么技术过时了可以直接用新技术的
android·过时技术
lingggggaaaa3 小时前
PHP原生开发篇&文件安全&上传监控&功能定位&关键搜索&1day挖掘
android·学习·安全·web安全·php
我是场4 小时前
我的NPI项目 - OTA upgrade是什么?
android·人工智能
zh_xuan4 小时前
Android 实现fragment导航
android·fragment导航
java资料站4 小时前
MySQL 增量同步脚本
android·数据库·mysql
穷人小水滴4 小时前
手机安装 Cross Profile Test App (Android)
android·手机·一加
zh_xuan4 小时前
Android Paging 3实现分页加载
android·git·github·paging 3
天***88525 小时前
安卓KMPlayer安卓版播放器,支持AC-3、WMA、MP3、AAC
android·aac
jinanwuhuaguo6 小时前
OpenClaw 2026.4.5 深度解读
android·开发语言·人工智能·kotlin·openclaw