我有一个标签SomeProject
,我有三个activity
中的三个fragments
。
在一个标签的某些按钮的tabs
上,另一个片段将替换旧的onclick
。我在主fragment
中创建了一个容器<linearlayout>
,将其替换为新的activity
。
事情正常,但当我动态创建要替换的容器时,所有三个fragment
都被替换为第一个选项卡中托管的fragments
。我想所有人都在共享同一个容器。是我必须创建三个独立的LinearLayouts并在其中托管不同的fragemnts并链接每个。
任何人都可以提供一个非常简单的示例,其中一个选项卡中有三个选项卡和一个按钮,用一个新片段替换其中一个片段。真的很感激。
答案 0 :(得分:0)
如果您使用的是Android Studio生成的TabbedActivity,则只需在片段中的onCreateView()
中执行此类操作。如果你已经创建了自己的Activity&amp;片段,你仍然使用它,但你需要确保在创建/替换片段时设置参数。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = inflater.inflate(R.layout.fragment_1, container, false);
Button button = (Button) rootView.findViewById(R.id.my_button);
// other stuff for this layout here
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_2, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.my_text_view);
// other stuff for this layout here
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_3, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.my_image_view);
// other stuff for this layout here
break;
}
return rootView;
}