在共同的类下,在三个不同的片段中运行相同的代码。

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

Running the same code in three different fragments under the common class

问题

我在我的应用程序中使用数据绑定。
我有三个不同的片段,所有三个片段都有相同的工具栏ID。
我有相同的代码在三个工具栏中。

float radius = getResources().getDimension(R.dimen.default_corner_radius); //32dp
MaterialShapeDrawable materialShapeDrawable = (MaterialShapeDrawable) binding.toolbar.getBackground();
materialShapeDrawable.setShapeAppearanceModel(materialShapeDrawable.getShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build());

我想创建一个如下的通用类,并从这里运行相同的代码,但我无法弄清如何传递绑定结构。

public class MenuFragmentsToolbar<T> {

    public void applyRadiusCorner(T binding){

        float radius = getResources().getDimension(R.dimen.default_corner_radius); //32dp
        MaterialShapeDrawable materialShapeDrawable = (MaterialShapeDrawable) binding.toolbar.getBackground();
        materialShapeDrawable.setShapeAppearanceModel(materialShapeDrawable.getShapeAppearanceModel()
                .toBuilder()
                .setAllCorners(CornerFamily.ROUNDED, radius)
                .build());
    }
}
英文:

I am using databinding in my application.
I have three different fragments, all three have toolbars with the same id.
I have code that is the same in three toolbars.

        float radius = getResources().getDimension(R.dimen.default_corner_radius); //32dp
        MaterialShapeDrawable materialShapeDrawable = (MaterialShapeDrawable) binding.toolbar.getBackground();
        materialShapeDrawable.setShapeAppearanceModel(materialShapeDrawable.getShapeAppearanceModel()
                .toBuilder()
                .setAllCorners(CornerFamily.ROUNDED,radius)
                .build());

I want to create a common class as below and run the same code from here, but I couldn't figure out how to transfer the binding structure.

public class MenuFragmentsToolbar&lt;T&gt; {

    public void applyRadiusCorner(T binding){

        float radius = getResources().getDimension(R.dimen.default_corner_radius); //32dp
        MaterialShapeDrawable materialShapeDrawable = (MaterialShapeDrawable) binding.toolbar.getBackground();
        materialShapeDrawable.setShapeAppearanceModel(materialShapeDrawable.getShapeAppearanceModel()
                .toBuilder()
                .setAllCorners(CornerFamily.ROUNDED,radius)
                .build());
    }
}

答案1

得分: 0

Couldn't you just pass in the View or MaterialShapeDrawable instead of the binding?

public static void applyRadiusCorner(View v) {
   if(!(v.getBackground() instanceof MaterialShapeDrawable))
     throw new IllegalArgumentException();
   applyRadiusCorner((MaterialShapeDrawable) v.getBackground());
}

public static void applyRadiusCorner(MaterialShapeDrawable drawable) {
  final float radius = getResources().getDimension(R.dimen.default_corner_radius);
  drawable.setShapeAppearanceModel(drawable.getShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(ROUNDED,radius)
    .build());
}

And use it as applyRadiusCorner(binding.toolbar).

Edit:

Wrapping this in a class:

class StyleableToolbar {

  private final Toolbar;
  private final MaterialShapeDrawable background;
  private final Resources res;
  
  private StyleableToolbar(Toolbar toolbar) {
     this.toolbar = toolbar;
     this.background = (MaterialShapeDrawable) toolbar.getBackground();
     this.res = toolbar.getResources();
  }
 
  static Optional<StyleableToolbar> from(Toolbar toolbar) {
    return (toolbar.getBackground() instanceof MaterialShapeDrawable) 
      ? Optional.of(new StyleableToolbar(toolbar)) 
      : Optional.empty()
  }

  void applyRadiusCorner() {
    final float radius = res.getDimension(R.dimen.default_corner_radius); 
    final ShapeAppearanceModel sam = background
      .getShapeAppearanceModel()
      .toBuilder()
      .setAllCorners(ROUNDED,radius)
      .build();
    background.setShapeAppearanceModel(sam);
  }

}

With usage StyleableToolbar.from(binding.toolbar).ifPresent(StyleableToolbar::applyRadiusCorner);

英文:

Couldn't you just pass in the View or MaterialShapeDrawable instead of the binding?

public static void applyRadiusCorner(View v) {
   if(!(v.getBackground() instanceof MaterialShapeDrawable)
     throw new IllegalArgumentException();
   applyRadiusCorner((MaterialShapeDrawable) v.getBackground());
}

public static void applyRadiusCorner(MaterialShapeDrawable drawable) {
  final float radius = getResources().getDimension(R.dimen.default_corner_radius);
  drawable.setShapeAppearanceModel(drawable.getShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(ROUNDED,radius)
    .build());
}

And use it as applyRadiusCorner(binding.toolbar).

Edit:

Wrapping this in a class:

class StyleableToolbar {

  private final Toolbar;
  private final MaterialShapeDrawable background;
  private final Resources res;
  
  private StyleableToolbar(Toolbar toolbar) {
     this.toolbar = toolbar;
     this.background = (MaterialShapeDrawable) toolbar.getBackground();
     this.res = toolbar.getResources();
  }
 
  static Optional&lt;StyleableToolbar&gt; from(Toolbar toolbar) {
    return (toolbar.getBackground() instanceof MaterialShapeDrawable) 
      ? Optional.of(new StyleableToolbar(toolbar)) 
      : Optional.empty()
  }

  void applyRadiusCorner() {
    final float radius = res.getDimension(R.dimen.default_corner_radius); 
    final ShapeAppearanceModel sam = background
      .getShapeAppearanceModel()
      .toBuilder()
      .setAllCorners(ROUNDED,radius)
      .build();
    background.setShapeAppearanceModel(sam);
  }

}

With usage StyleableToolbar.from(binding.toolbar).ifPresent(StyleableToolbar::applyRadiusCorner);

huangapple
  • 本文由 发表于 2023年6月26日 19:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556275.html
匿名

发表评论

匿名网友

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

确定