Android 应用程序:如何使用 viewPager 访问不同的片段?
Android Applications: how do I access different fragments using a viewPager?
我正在尝试编写一个 android 应用程序,但我对此还很陌生,而且我在选择不同选项卡上的不同片段时遇到了问题。例如,当调用此代码块时,每个选项卡最终看起来像 "location_main.xml." 有没有办法区分所选选项卡?谢谢!
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//This is defining the text for the fragments, or tabs, at the given section number.
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
//This returns the View rootView
return rootView;
}
您正在参数包中传递一个整数值 (ARG_SECTION_NUMBER)。您可以使用它来区分您的选项卡。通常用于定义标签的位置。
您可以根据 getArguments().getInt(ARG_SECTION_NUMBER) 值或事件实例化另一个片段来更改文本视图的文本或展开不同的布局。
你可以查看这个问题的答案。有一个很好的例子解释。
How to implement a ViewPager with different Fragments / Layouts
我正在尝试编写一个 android 应用程序,但我对此还很陌生,而且我在选择不同选项卡上的不同片段时遇到了问题。例如,当调用此代码块时,每个选项卡最终看起来像 "location_main.xml." 有没有办法区分所选选项卡?谢谢!
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//This is defining the text for the fragments, or tabs, at the given section number.
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
//This returns the View rootView
return rootView;
}
您正在参数包中传递一个整数值 (ARG_SECTION_NUMBER)。您可以使用它来区分您的选项卡。通常用于定义标签的位置。
您可以根据 getArguments().getInt(ARG_SECTION_NUMBER) 值或事件实例化另一个片段来更改文本视图的文本或展开不同的布局。
你可以查看这个问题的答案。有一个很好的例子解释。
How to implement a ViewPager with different Fragments / Layouts