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

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

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

问题

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

cardStackView.getItemAnimator().setSupportsChangeAnimations(false);

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

cardStackView.setItemAnimator(new DefaultItemAnimator() {
    @Override
    public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
        super.setSupportsChangeAnimations(supportsChangeAnimations);
    }
});
英文:

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

cardStackView.itemAnimator.apply {
        if (this is DefaultItemAnimator) {
            supportsChangeAnimations = false
        }
    }

So far i have come up with something like this:

cardStackView.setItemAnimator(new DefaultItemAnimator(){

             @Override
             public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {
                                         super.setSupportsChangeAnimations(supportsChangeAnimations);

                   }
             }
    );

答案1

得分: 2

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

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:

确定