开发者

Android refer to custom views in a loop?

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 Layo开发者_开发技巧ut
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).


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.


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();
}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜