Recyclerview在填充上单击事件

时间:2018-02-19 10:14:36

标签: android android-recyclerview

我有一个水平的Recyclerview。我希望在Recyclerview的填充上获得点击操作。请检查我更新的波纹管代码和错误日志。

enter image description here

我已经检查了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();
        }

    }
}

错误日志:enter image description here

1 个答案:

答案 0 :(得分:0)

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

  • 在Android Studio中打开Kotlin文件

  • 工具 - &gt; Kotlin-&gt;显示Kotlin字典

  • 点击Decompile按钮,复制转换后的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);
}
}