英文:
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<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());
}
}
答案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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论