简体   繁体   English

将一个自定义视图的对象访问到另一个在Activty类上创建的Customview

[英]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.

相关问题 无法从另一个类访问我的自定义视图对象 - Not being able to access my custom view object from another class 通过Android中的其他活动访问上下文 - Access Context from another activty in Android 从一个Java类中激活一个活动 - activates an activty from one of a java class 如何将数据更新为从自定义视图类创建的对象,以便可以在自定义视图上绘制? - How to update data to an object created from a custom view class ,so that it can be drawn on custom view? 如何在另一个活动中显示基本活动的视图 - How to show base activity's view in another activty 如何将数据从一个活动多次获取到另一个 Android - how to get data from one activty to another several times Android 如何从一个活动转到Android中的另一个片段? - How to go from one activty to another fragment in android? 将Firebase数据从一项活动传递到另一项活动,即ListView的onClick - Passing firebase data from one activty to another, onClick of ListView GRIDVIEW-通过单击事件从一项活动通过图像到另一项? - GRIDVIEW- PASS IMAGE FROM ONE ACTIVTY TO ANOTHER ON ONCLICK EVENT? 如何将对象从导入的包发送到另一个活动 - How to send an object from imported package to another Activty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM