Android 在循环中引用自定义视图?

发布于 2024-10-03 02:04:43 字数 1032 浏览 3 评论 0原文

我需要递归地浏览我的一堆扩展视图类的自定义视图。

例如,

ViewOne.java
ViewTwo.java
ViewThree.java

我在 MainClass.java 中创建了每个视图的实例,

ViewOne vOne;
ViewTwo vTwo;
ViewThree vThree;

这些视图都实现了一个名为 start() 的函数。

我希望能够以某种方式循环遍历它们:

for(int i=0; i<= 2:i++)
{
  views[i].start();
}

我将如何去做呢?

以上只是一个例子。我需要能够以数字和编程方式移动它们的真正原因是因为我希望能够在单击按钮(上一个和下一个)时按数字顺序向布局添加和删除视图。 (我不希望它们一开始就全部添加到布局中,因为它们是资源密集型视图)。

那么需要的是这样的:

点击下一步->添加下一个视图 ->删除当前视图。 点击上一个->添加上一个视图->删除当前视图。

例如

currView = 1

Current View is currView (1)
Click Next
Add View currView+1 (2) to Layout
Switch to View currView+1 (2)
Remove View currView (1)

or

currView = 2

Current View is currView (2)
Click Previous
Add View currView-1 (1) to Layout
Switch to currView-1 (1)
remove View currView (2)

,请注意,这些视图都是它们自己独特的类型,并且实际上是扩展视图的单独类。我不能简单地将它们类型转换为“View”,因为这是错误的,它们的类型分别是 ViewOne、ViewTwo 和 ViewThree(例如)。

I need to recursively move through a bunch of custom views of mine that extend the view class.

E.g.

ViewOne.java
ViewTwo.java
ViewThree.java

I have created instances of each view in my MainClass.java

ViewOne vOne;
ViewTwo vTwo;
ViewThree vThree;

these views all implement a function called start().

and I want to be able to loop through them somehow:

for(int i=0; i<= 2:i++)
{
  views[i].start();
}

How would I go about doing this?

The above is only an example. The real reason I need to be able to move through them numerically and programatically is because I want to be able to add and remove views to a layout in their numeric order as button (previous and next) are clicked. (I don't want them all added to the layout at the start because they are heavily resource intensive views).

So what is required is as such:

Click Next -> add next view -> remove current view.
Click Previous -> add previous view -> remove current view.

e.g.

currView = 1

Current View is currView (1)
Click Next
Add View currView+1 (2) to Layout
Switch to View currView+1 (2)
Remove View currView (1)

or

currView = 2

Current View is currView (2)
Click Previous
Add View currView-1 (1) to Layout
Switch to currView-1 (1)
remove View currView (2)

Note, this views are all of their own unique type and are infact individual classes that extend View. I can't simply typecast them to "View" because that's wrong, their types are ViewOne, ViewTwo and ViewThree respectively (for example).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2024-10-10 02:04:43

假设所有视图都已添加到布局中,您可以以编程方式迭代 ViewGroup(即布局)中的所有子视图,如下所示:

ViewGroup group = findViewById(R.id.root); // The name of your layout
int children = group.getChildCount();
for (int i = 0; i < children; i++) {

    View child = group.getChildAt(i);
    if (child instanceof ViewOne) {        
       ... 
    } else if (child instanceof ViewTwo) {
       ...
    }
}

此外,如果所有自定义视图都实现 start() ,我会将该方法推送到一个接口中,以便您可以简化上面的 if 块。

Assuming that all the views have been added to a layout, you can programatically iterate over all the children in a ViewGroup (i.e a layout) like so:

ViewGroup group = findViewById(R.id.root); // The name of your layout
int children = group.getChildCount();
for (int i = 0; i < children; i++) {

    View child = group.getChildAt(i);
    if (child instanceof ViewOne) {        
       ... 
    } else if (child instanceof ViewTwo) {
       ...
    }
}

Additionally, if all of your custom views implement start(), I would push that method into an interface so you can simplify the if block above.

你穿错了嫁妆 2024-10-10 02:04:43

如果你想这样做,我会制作一个视图列表和通过该列表的循环。例如:

List views = new ArrayList();
views.add(vOne);
views.add(vTwo);
views.add(vThree);

for (View view: views) {
view.start();
}

If you want to do that I would make a list of views and the loop trough this list. For example:

List views = new ArrayList();
views.add(vOne);
views.add(vTwo);
views.add(vThree);

for (View view: views) {
view.start();
}
白日梦 2024-10-10 02:04:43

因为您试图推迟视图的构建,所以我建议您使用构建器模式。根据您的示例,创建一个接口 Builder 并派生三个子类:

public class BuildViewOne implements Builder { 
    ... 
    public View start () { return new ViewOne (); }
}

public class BuildViewTwo implements Builder { 
    ... 
    public View start () { return new ViewTwo (); }
}

public class BuildViewThree implements Builder { 
    ... 
    public View start () { return new ViewThree (); }
}

Builder 接口有一个 start 方法,该方法创建适当的视图,仅在需要时加载这些资源。然后,您可以这样创建视图构建器数组:

Builder builders[] = new Builder[] { 
    new BuildViewOne (), 
    new BuildViewTwo (), 
    new BuildViewThree ()
}

现在您可以通过其索引 builders[2].start () 创建一个类的实例。根据您的问题,您还可以使用构建器缓存创建的视图,以便还可以实现会销毁视图的 stop () 方法。因此,当您从布局中删除视图时,您可以释放资源(我假设您需要给定视图的一个且仅一个实例)。

注意:上面不是真正的代码(即我没有编译或运行它)。

Because you are trying to defer the construction of your views I suggest you use the Builder pattern. As per your example create an interface Builder and derive three sub classes:

public class BuildViewOne implements Builder { 
    ... 
    public View start () { return new ViewOne (); }
}

public class BuildViewTwo implements Builder { 
    ... 
    public View start () { return new ViewTwo (); }
}

public class BuildViewThree implements Builder { 
    ... 
    public View start () { return new ViewThree (); }
}

The Builder interface has a start method which creates the appropriate view which will load those resources only when needed. Then you create your array of view builders thusly:

Builder builders[] = new Builder[] { 
    new BuildViewOne (), 
    new BuildViewTwo (), 
    new BuildViewThree ()
}

Now you can create an instance a class via its index builders[2].start (). Based on your question you could also cache the created view with the builder to allow you to also implement a stop () method that would destroy the view. As such you could free resources when you remove a view from your layout (I am assuming you need one and only one instance of a given view).

Note: above is not real code (i.e. I did not compile or run it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文