我有两个在这样的活动中创建的自定义视图对象。
public class Statistics extends Activity {
GraphWindow graph1;
GraphWindow graph2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
graph1 = (GraphWindow) findViewById(R.id.graph1);
graph2 = (GraphWindow) findViewById(R.id.graph2);
...
}
然而,它们似乎充当一个实例,因此graph1的公共方法也将在图2上执行。我是否需要以某种方式将每个图形视图作为新实例启动?我会在哪里这样做?
修改
这是(浓缩的)GraphWindow类:
public class GraphWindow extends View {
//draw data
public ArrayList<DataPoint> data = new ArrayList<DataPoint>();
//set height
public int graphHeight = 0;
public int indexStart = 0;
public int indexFinish = 0;
public boolean isTouched = false;
public boolean isDraggable = false;
public GraphWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public GraphWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GraphWindow(Context context) {
super(context);
}
public void setGraphHeight(int graphHeight) {
this.graphHeight = graphHeight;
}
public void isDraggable(boolean isDraggable) {
this.isDraggable = isDraggable;
}
public void panBox(MotionEvent event) {
rectX = (int)event.getX();
rectW = this.getWidth()/5 + rectX;
this.postInvalidate();
}
public void clearData() {
this.data.clear();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
}
}
特别是clear数据方法将在graph1和graph2上运行。