如何在RecyclerView上增加当前焦点项目的大小?

Sky*_*ius 22 android android-layout android-tv

我正在尝试制作一个带有a的水平列表RecyclerView,当我将焦点放在一个项目上时,增加它的大小.我想这样做:

增加项目效果

你有什么想法来实现这个目标吗?

Seb*_*ano 18

我想象的是这样的:

  1. 创建水平RV
  2. 绑定ViewHolder时,请将a附加FocusChangeListener到项目的根视图
  3. 当项目获得焦点时,将其缩放以使其稍大; 当焦点丢失时,恢复动画.

static class ViewHolder extends RecyclerView.ViewHolder {

  public ViewHolder(View root) {
    // bind views
    // ...

    // bind focus listener
    root.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
        public void onFocusChange(View v, boolean hasFocus) {
          if (hasFocus) {
            // run scale animation and make it bigger
          } else {
            // run scale animation and make it smaller
          }
        }
     });
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案仅适用于Android TV,不适用于移动设备... :-( (2认同)
  • 我必须包含 `android:clickable="true"` 和 `android:focusableInTouchMode="true"` 才能使其正常工作 (2认同)

Sky*_*ius 14

感谢dextor的回答,我可以找出这个答案。

我已经使用FocusChangeListener和 添加的动画状态来更改视图的大小:

static class ViewHolder extends RecyclerView.ViewHolder {

public ViewHolder(final View root) {
    // bind views
    // ...

    // bind focus listener
    root.setOnFocusChangeListener(new View.OnFocusChangeListener() {
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
             if (hasFocus) {
                 // run scale animation and make it bigger
                 Animation anim = AnimationUtils.loadAnimation(context, R.anim.scale_in_tv);
                 root.startAnimation(anim);
                 anim.setFillAfter(true);
             } else {
                 // run scale animation and make it smaller
                 Animation anim = AnimationUtils.loadAnimation(context, R.anim.scale_out_tv);
                 root.startAnimation(anim);
                 anim.setFillAfter(true);
             }
         }
    });
}
Run Code Online (Sandbox Code Playgroud)

以及动画的代码:

scale_in_tv:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXScale="100%"
    android:fromYScale="100%"
    android:toXScale="110%"
    android:toYScale="110%"
    android:pivotX="50%"
    android:pivotY="50%">
</scale>
Run Code Online (Sandbox Code Playgroud)

scale_out_tv:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromXScale="110%"
    android:fromYScale="110%"
    android:toXScale="100%"
    android:toYScale="100%"
    android:pivotX="50%"
    android:pivotY="50%">
</scale>
Run Code Online (Sandbox Code Playgroud)

  • 这与我的解决方案有何不同,为什么在将近一年后已接受的答案(我的)被删除了? (12认同)