【问题标题】:Recyclerview click events on padding填充上的 Recyclerview 点击事件
【发布时间】:2018-02-19 10:14:35
【问题描述】:

我有一个水平的 Recyclerview。我想要对 Recyclerview 的填充进行点击操作。请检查我更新的波纹管代码和错误日志。

我已经检查了Recyclerview: listen to padding click events,但这里使用 Kotlin,但我需要 Java 代码。

我的代码:

 MyRecyclerView  extends RecyclerView {
    private boolean isValid;
    private int x;
    private int y;
    private final int delta;
    private HashMap _$_findViewCache;

    public boolean onTouchEvent(@Nullable MotionEvent e) {
        boolean onTouchEvent = super.onTouchEvent(e);
        Integer var3 = e != null?Integer.valueOf(e.getAction()):null;
        boolean var4 = false;
        if(var3 != null) {
            if(var3.intValue() == 0) {
                this.x = (int)e.getRawX();
                this.y = (int)e.getRawY();
                this.isValid = true;
                return onTouchEvent;
            }
        }

        byte var5 = 2;
        if(var3 != null) {
            if(var3.intValue() == var5) {
                if(Math.abs(e.getRawX() - (float)this.x) > (float)this.delta || Math.abs(e.getRawY() - (float)this.y) > (float)this.delta) {
                    this.isValid = false;
                }

                return onTouchEvent;
            }
        }

        var5 = 1;
        if(var3 != null) {
            if(var3.intValue() == var5 && this.isValid && Math.abs(e.getRawX() - (float)this.x) < (float)this.delta && Math.abs(e.getRawY() - (float)this.y) < (float)this.delta && this.isInRightArea(e)) {
                this.performClick();
            }
        }

        return onTouchEvent;
    }

    private final boolean isInRightArea(MotionEvent e) {
        Rect r = new Rect();
        this.getGlobalVisibleRect(r);
        r.left = this.getPaddingLeft();
        r.top += this.getPaddingTop();
        return !r.contains((int)e.getRawX(), (int)e.getRawY());
    }

    @JvmOverloads
    public MyRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        Intrinsics.checkParameterIsNotNull(context, "context");
        super(context, attrs, defStyleAttr);
        this.delta = ViewConfiguration.get(this.getContext()).getScaledTouchSlop();
    }

    // $FF: synthetic method
    @JvmOverloads
    public MyRecyclerView(Context var1, AttributeSet var2, int var3, int var4, DefaultConstructorMarker var5) {
        if((var4 & 2) != 0) {
            var2 = (AttributeSet)null;
        }

        if((var4 & 4) != 0) {
            var3 = 0;
        }

        this(var1, var2, var3);
    }

    @JvmOverloads
    public MyRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0, 4, (DefaultConstructorMarker)null);
    }

    @JvmOverloads
    public MyRecyclerView(@NotNull Context context) {
        this(context, (AttributeSet)null, 0, 6, (DefaultConstructorMarker)null);
    }

    public View _$_findCachedViewById(int var1) {
        if(this._$_findViewCache == null) {
            this._$_findViewCache = new HashMap();
        }

        View var2 = (View)this._$_findViewCache.get(Integer.valueOf(var1));
        if(var2 == null) {
            var2 = this.findViewById(var1);
            this._$_findViewCache.put(Integer.valueOf(var1), var2);
        }

        return var2;
    }

    public void _$_clearFindViewByIdCache() {
        if(this._$_findViewCache != null) {
            this._$_findViewCache.clear();
        }

    }
}

错误日志:

【问题讨论】:

标签: android android-recyclerview


【解决方案1】:

这是将 Kotlin 代码转换为 Java 的方法 -

  • 在 Android Studio 中打开 Kotlin 文件

  • 工具->Kotlin->显示 Kotlin 字节码

  • 点击反编译按钮,复制转换后的Java代码。

这是转换后的 Java 代码。

public final class MyRecyclerView extends RecyclerView {
private boolean isValid;
private int x;
private int y;
private final int delta;

public boolean onTouchEvent(@Nullable MotionEvent e) {
  boolean onTouchEvent = super.onTouchEvent(e);
  Integer var3 = e != null?Integer.valueOf(e.getAction()):null;
  boolean var4 = false;
  if(var3 != null) {
     if(var3.intValue() == 0) {
        this.x = (int)e.getRawX();
        this.y = (int)e.getRawY();
        this.isValid = true;
        return onTouchEvent;
     }
  }

  byte var5 = 2;
  if(var3 != null) {
     if(var3.intValue() == var5) {
        if(Math.abs(e.getRawX() - (float)this.x) > (float)this.delta || Math.abs(e.getRawY() - (float)this.y) > (float)this.delta) {
           this.isValid = false;
        }

        return onTouchEvent;
     }
  }

  var5 = 1;
  if(var3 != null) {
     if(var3.intValue() == var5 && this.isValid && Math.abs(e.getRawX() - (float)this.x) < (float)this.delta && Math.abs(e.getRawY() - (float)this.y) < (float)this.delta && this.isInRightArea(e)) {
        this.performClick();
     }
  }

  return onTouchEvent;
}

private final boolean isInRightArea(MotionEvent e) {
  Rect r = new Rect();
  this.getGlobalVisibleRect(r);
  r.left = this.getPaddingLeft();
  r.top += this.getPaddingTop();
  return !r.contains((int)e.getRawX(), (int)e.getRawY());
}

@JvmOverloads
public MyRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  Intrinsics.checkParameterIsNotNull(context, "context");
  super(context, attrs, defStyleAttr);
  ViewConfiguration var10001 = ViewConfiguration.get(this.getContext());
  Intrinsics.checkExpressionValueIsNotNull(var10001, "ViewConfiguration.get(getContext())");
  this.delta = var10001.getScaledTouchSlop();
}

// $FF: synthetic method
@JvmOverloads
public MyRecyclerView(Context var1, AttributeSet var2, int var3, int var4, DefaultConstructorMarker var5) {
  if((var4 & 2) != 0) {
     var2 = (AttributeSet)null;
  }

  if((var4 & 4) != 0) {
     var3 = 0;
  }

  this(var1, var2, var3);
}

@JvmOverloads
public MyRecyclerView(@NotNull Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0, 4, (DefaultConstructorMarker)null);
}

@JvmOverloads
public MyRecyclerView(@NotNull Context context) {
  this(context, (AttributeSet)null, 0, 6, (DefaultConstructorMarker)null);
}
}

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 2020-10-24
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多