[英]Access object of one custom view to another Customview, created on Activty class
My actual problem: I have an array in CustomView1
class. 我的实际问题:
CustomView1
类中有一个数组。 And I want to access it in CustomView2
class. 我想在
CustomView2
类中访问它。 And when it will fill, I have to call view2.invalidate()
. 当它填满时,我必须调用
view2.invalidate()
。
This is my activity class: 这是我的活动课:
public class TestApp extends Activity {
CustomView1 view1;
CustomView2 view2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view1 = (CustomView1) findViewbyId(R.id.CustomViewID1);
view2 = (CustomView2) findViewbyID(R.id.CustomViewID2);
}
}
This is my first CustomView
class . 这是我的第一个
CustomView
类。 Here I want to call view2.invalidate()
. 在这里我想调用
view2.invalidate()
。
public class CustomView1 extends View {
byte[] bytearray = new byte[200];
public CustomView1(Context context, AttributeSet attrs) {
super(context, attrs);
}
@override
onDraw() {
view2.invalidate();
}
}
And this is my second CustomView
class. 这是我的第二个
CustomView
类。 Here I want to access the same object of CustomView1
(ie view1
object ) class that I have created in Activity class. 在这里,我想访问在Activity类中创建的
CustomView1
类的同一对象(即view1
object)。
public class CustomView2 extends View {
public CustomView2(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Is it possible to access like this? 是否可以这样访问? Any other idea how to do that?
还有其他想法怎么做?
You can just have a CustomView2 view2;
您可以只拥有一个
CustomView2 view2;
member inside your CustomView1
class. CustomView1
类中的成员。 At the layout time, just tell view1 what is its view2. 在布局时,只需告诉view1其view2是什么。
This may look like this: 可能看起来像这样:
view2 = (CustomView2)findViewById(R.id.view2);
view1 = (CustomView1)findViewById(R.id.view1);
view1.setView2(view2);
For this to work you need to have a reference of those views inside your custom views classes. 为此,您需要在自定义视图类中引用这些视图。 Something like this in
CustomView1
(same logic is applied for CustomView2
): 在
CustomView1
这样的CustomView1
(对CustomView2
应用相同的逻辑):
public class CustomView1 extends View
{
byte[] bytearray = new byte[200];
private CustomView2 view2;
public CustomView1(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setCustomView2(CustomView2 view2)
{
this.view2 = view2;
}
@override
onDraw()
{
if(view2 != null) //If view2 is not already set, we do not want a null pointer exception to occur
{
view2.invalidate();
}
}
}
To use it, you need something like this: 要使用它,您需要这样的东西:
public class TestApp extends Activity
{
CustomView1 view1 ;
CustomView2 view2 ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view1 = (CustomView1) findViewbyId(R.id.CustomViewID1);
view2 = (CustomView2) findViewbyID(R.id.CustomViewID2);
view1.setCustomView2(view2);
view2.setCustomView1(view1);
}
}
EDIT: You can create another custom constructor for your CustomView1
class in addition to the one you already have. 编辑:您可以为您的
CustomView1
类创建另一个自定义构造函数。 Something like this: 像这样:
public CustomView1(Context context)
{
super(context);
}
This way you avoid passing a null AttributeSet
to your view and you call your custom view like this: CustomView1 view1 = new CustomView1(context)
. 这样,您可以避免将空
AttributeSet
传递给视图,并这样调用自定义视图: CustomView1 view1 = new CustomView1(context)
。
The public CustomView1(Context context, AttributeSet attrs)
constructor is used to handle the situation that you declare you custom view through a layout xml file and not programmatically. public CustomView1(Context context, AttributeSet attrs)
构造函数用于处理通过布局xml文件而不是通过编程方式声明自定义视图的情况。 You will need to set the layout properties of your view programmatically that way. 您将需要以这种方式以编程方式设置视图的布局属性。 Check this for help in doing so.
检查此操作以获取帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.