如何将此 Kotlin 代码中的 “apply” 转换为 Java?

huangapple go评论92阅读模式
英文:

How to convert "apply" in this kotlin code to java?

问题

我需要将这段 Kotlin 代码转换为 Java,以便为 RecyclerView 设置 itemAnimator:

  1. cardStackView.getItemAnimator().setSupportsChangeAnimations(false);

目前,我已经想到了以下类似的代码:

  1. cardStackView.setItemAnimator(new DefaultItemAnimator() {
  2. @Override
  3. public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
  4. super.setSupportsChangeAnimations(supportsChangeAnimations);
  5. }
  6. });
英文:

I need to convert this kotlin code to java to set the itemAnimator for a recylcer view

  1. cardStackView.itemAnimator.apply {
  2. if (this is DefaultItemAnimator) {
  3. supportsChangeAnimations = false
  4. }
  5. }

So far i have come up with something like this:

  1. cardStackView.setItemAnimator(new DefaultItemAnimator(){
  2. @Override
  3. public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
  4. super.setSupportsChangeAnimations(supportsChangeAnimations);
  5. }
  6. }
  7. );

答案1

得分: 2

  1. RecyclerView.ItemAnimator itemAnimator = cardStackView.getItemAnimator();
  2. if (itemAnimator instanceof DefaultItemAnimator) {
  3. DefaultItemAnimator di = (DefaultItemAnimator) itemAnimator;
  4. di.setSupportsChangeAnimations(false);
  5. }
英文:
  1. RecyclerView.ItemAnimator itemAnimator = cardStackView.getItemAnimator();
  2. if (itemAnimator instanceof DefaultItemAnimator) {
  3. DefaultItemAnimator di = (DefaultItemAnimator) itemAnimator;
  4. di.setSupportsChangeAnimations(false);
  5. }

huangapple
  • 本文由 发表于 2020年4月7日 15:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61074983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定