java的制糖,我可以避免几乎重复code在这里? [英] java sugaring, can I avoid almost-duplicate code here?

查看:133
本文介绍了java的制糖,我可以避免几乎重复code在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private class HSV extends HorizontalScrollView {
    public LinearLayout L;
    public AbsoluteLayout A;
    public HSV(Context context) {
        super(context);
        L = new LinearLayout(context);
        A = new AbsoluteLayout(context);
    }
    @Override public void addView(View child) {
        A.addView(child);
    }
    void update_scroll() {
        removeView(L);
        addView(L, 0);
        L.removeView(A);
        L.addView(A);
        A.invalidate();
        L.invalidate();
        invalidate();
        requestLayout();
    }
    int GetCurrentPos() {
        return getScrollX(); // <-- this line if HSV
        return getScrollY(); // <-- this line if VSV
    }
    ... few more methods skipped, they will not change at all in 'vertical' version
}

我有这样的类,它完美的我想要做什么。现在我需要新的类VSV将从(垂直)滚动型派生,并是完全一样的。我当然可以整块和变化的扩展Horizo​​ntalScrolView 只是复制为延伸滚动型,然后再(L,0)(0, L)(哎呀,出版于当如此,想必该行不会改变,这是一个错误,GetCurrentPos会)。

I have this class, it perfectly does what I want. Now I need new class VSV which will derive from (vertical)ScrollView and be just the same. I surely can just copy whole block and change extends HorizontalScrolView to extends ScrollView, and then (L, 0) to (0, L) (oops, this was a mistake when publishing on SO, surely that line will not change, the GetCurrentPos will).

我也可以加上bool的垂直属性。但是Java没有模板或宏,也不运行原型,那么有没有在Java中任何其他方式,以避免在这个例​​子中code复制?

or I can add "bool vertical" property. But Java has no templates or macros, nor runtime prototyping, so is there any other way in Java to avoid code duplication in this example?

推荐答案

看的的 android.widget.ScrollView android.widget.Horizo​​ntalScrollView ,两人似乎实施

Looking at the documentation for android.widget.ScrollView and android.widget.HorizontalScrollView, both of them seem to implement

void addView(View child, int index)

所以,你会不会有改变该行code,如果我不在这里缺少什么。此外,这两个类继承 android.view.ViewGroup 此方法。因此,如果两个类的实现是一样的,你可以做这样的事情。

So you would not have to change that line of code, if I am not missing anything here. Also, both classes inherit this method from android.view.ViewGroup. So, if the implementation of both classes is the same, you could do something like this

public abstract class ScrollViewDelegate<T extends FrameLayout> {
  private final T view;
  private LinearLayout L;
  private AbsoluteLayout A;

  public ScrollViewWrapper(T view) {
    this.view = view;
    L = new LinearLayout(view.getContext());   // or pass as parameter
    A = new AbsoluteLayout(view.getContext()); // or pass as parameter
  }

  void update_scroll() {
      view.removeView(L);
      view.addView(L, 0);
      L.removeView(A);
      L.addView(A);
      A.invalidate();
      L.invalidate();
      view.invalidate();
      view.requestLayout();
  }
  // ...
}

和HSV中/ VSV你可以委托给这个类(如有必要)。

and in HSV/VSV you can delegate to this class (if necessary).

public class HSV extends HorizontalScrollView {

  private final ScrollViewDelegate<HorizontalScrollView> delegate;

  public HSV(Context context) {
      super(context);
      this.delegate = new ScrollViewDelegate<HorizontalScrollView>(this);
  }
  // do stuff with this.delegate
}

public class VSV extends ScrollView {

  private final ScrollViewDelegate<ScrollView> delegate;

  public VSV(Context context) {
      super(context);
      this.delegate = new ScrollViewDelegate<ScrollView>(this);
  }
}

这篇关于java的制糖,我可以避免几乎重复code在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆