I am going to work with one of:
ViewAnimator
ViewFlipper
ViewSwitcher
All of them have a method setDisplayedChild
to change a view from one to another.
Only argument of that method is int whichChild
- a number of view in the view queue.
Is it possible to work with any sort of not hardcoded numbers ?
I want to be able to call:
setDisplayedChild(R.id.SettingsView)
instead of:
setDisplayedChild(3)
You can use the indexOfChild(View child) method:
viewAnimator.setDisplayedChild( viewAnimator.indexOfChild( findViewById(R.id.SettingsView) ) );
These classes are just ViewGroup extensions that keep the child list in and array of View.
You could easily extend whatever one you want to look through the list of views for the Id. (untested code follows)
class myFlipper extends ViewFlipper {
public myFlipper(Context context) {
super(context);
}
int getChildById(int id) {
int childindex = -1;
int i = 0;
while (i<getChildCount()) {
View v = getChildAt(i);
if (v.getId()==id) {
childindex = i;
break;
}
i++;
}
return childindex;
}
void setDisplayedChildById(int id) {
int i = getChildById(id);
if (i != -1) {
setDisplayedChild(i);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With