Android 在主活动中调用片段
我为每个类别设置了不同的片段集,并希望在从抽屉中选择时显示它们Android 在主活动中调用片段,android,android-fragments,Android,Android Fragments,我为每个类别设置了不同的片段集,并希望在从抽屉中选择时显示它们 private static final String ARG_POSITION = "position"; private int position; private LinearLayout layout; private TextView icon; public static WizardUniversalFragment newInstance(int position) {
private static final String ARG_POSITION = "position";
private int position;
private LinearLayout layout;
private TextView icon;
public static WizardUniversalFragment newInstance(int position) {
WizardUniversalFragment f = new WizardUniversalFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
我怎样才能解决这个问题
您可以根据每个片段的位置来调用它们
从你的代码中,我理解了你的立场声明
参数使用它来调用每个片段
设定好了,你就可以开始了
欢迎来到SO,请在提问前花些时间搜索。你会发现许多博客和答案告诉你如何用片段实现导航抽屉。像这个。抱歉,我是新来的,所以下一次在问问题之前我会更加小心。谢谢你们的支持
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(your_pos);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_wizard_universal,
container, false);
layout = (LinearLayout) rootView
.findViewById(R.id.Your_fragment);
icon = (TextView) rootView
.findViewById(R.id.Your_fragment_icon);
if (position == 0) {
layout.setBackgroundColor(ContextCompat.getColor(getContext(),
R.color.#));
layout.invalidate();
icon.setText(R.string.#);
}
ViewCompat.setElevation(rootView, 50);
return rootView;
}