Reputation: 3439
Hello fellow developers!
i'll keep this as simple and short as I can, I have a fragment, and in my onCreateView I am sometimes giving it different layouts depend on my status.
is there any way of knowing on which layout I am?
code below:
my onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
if (programState) {
view = inflater.inflate(R.layout.fragment_home_program_on, container, false);
} else {
view = inflater.inflate(R.layout.fragment_home, container, false);
}
findViews(view);
setupViews();
return view;
}
basically, my "findViews" method gets that View, and I was wondering if there's a way to know which layout i am on with a switch statement for example or something like that.. like this :
private void findViews(View mainView) {
switch(mainView) {
case R.layout.fragment_home:
//do work
break;
case R.layout.fragment_home_program_on:
//do work
break;
}
}
I am trying to do something like that with no success.. all i get is errors and such, is there a way to do that?
thanks a lot in advance
Upvotes: 0
Views: 12719
Reputation: 6781
If you have various Views and you need to differentiate them - try adding tags to them.
1 - If you are going to have more then one tag type - create keys for them.
And use mainView.setTag(TAG_TYPE_ID_1, 10011)
(many tags for a view) rather than mainView.setTag(10011)
(only one tag for a view).
In this case TAG_TYPE_ID_1 may be your constant field. It must be initialized with id of some Resource. For example public static final int TAG_TYPE_ID_1 = R.layout.my_frame_layout;
Also consider placing int viewId = (int) mainView.getTag()
into try/catch because you can get ClassCastException if you will put there a String
for example but will try to get int
.
But for this example we assume you don't need lots of tags and you know that you have only int
as a tag.
2 - Set tag into the view in onCreateview() method in your fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view;
if (programState) {
view = inflater.inflate(R.layout.fragment_home_program_on, container, false);
view.setTag(1011);
} else {
view = inflater.inflate(R.layout.fragment_home, container, false);
view.setTag(1012);
}
findViews(view);
setupViews();
return view;
}
3 - Then read the tag value in your view and decide what to do
private void findViews(View mainView) {
if(mainView.getTag() == null)
return;
switch((int) mainView.getTag()) {
case 1011:
//do work
break;
case 1012:
//do work
break;
}
}
NOTE: don't forget that here switch((int) mainView.getTag())
you can get ClassCastException or NullPointerException if tag will be different type or null
Also int
codes like 1011
, 1012
etc. is better to store as a constant fields
Upvotes: 0
Reputation: 1007658
is there any way of knowing on which layout I am?
Sure. Check programState
. You are already doing that to determine which layout to load, so use that to determine which layout you loaded.
I was wondering if there's a way to know which layout i am
Use if (programState)
, the same way you did in onCreateView()
.
Beyond that, a widget has no idea what layout resource it came from. So, somewhere, you need to track that information yourself, and you seem to be doing so already in programState
.
Upvotes: 2
Reputation: 9187
Change your method to something like this:
private void findViews(View mainView) {
int viewId = mainView.getId();
switch(viewId) {
case R.id.firstViewId:
//do work
break;
case R.id.secondViewId:
//do work
break;
}
}
Upvotes: 0