Android 在循环中引用自定义视图?
我需要递归地浏览我的一堆扩展视图类的自定义视图。
例如,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设所有视图都已添加到布局中,您可以以编程方式迭代 ViewGroup(即布局)中的所有子视图,如下所示:
此外,如果所有自定义视图都实现
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:
Additionally, if all of your custom views implement
start()
, I would push that method into an interface so you can simplify theif
block above.如果你想这样做,我会制作一个视图列表和通过该列表的循环。例如:
If you want to do that I would make a list of views and the loop trough this list. For example:
因为您试图推迟视图的构建,所以我建议您使用构建器模式。根据您的示例,创建一个接口 Builder 并派生三个子类:
Builder 接口有一个 start 方法,该方法创建适当的视图,仅在需要时加载这些资源。然后,您可以这样创建视图构建器数组:
现在您可以通过其索引
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: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: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 astop ()
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).