我使用PagerSlidingTabStrip在我的主布局中创建一个ViewPager。我想动态添加一些标签,每个片段都有不同的布局。 例如:
if (var == 1)
tab.add("tab1", R.layout.fragment1);
tab.add("tab2", R.layout.fragment2);
tab.add("tab3", R.layout.fragment3);
else if (var == 2)
tab.add("tab2", R.layout.fragment2);
else
// Other tabs
并且片段必须接收从主活动传递的字符串参数。
public class fragment1 extends Fragment
{
private static final String ARG_POSITION = "position";
private static final String ARG_TYPE = "type";
private int position;
private String type;
public static fragment1 newInstance(int position, String type)
{
fragment1 f = new fragment1();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
b.putString(ARG_TYPE, type);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
type = getArguments().getString(ARG_TYPE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup cointainer, Bundle savedInstanceState)
{
View myFragView = inflater.inflate(R.layout.frag_sinonimi, cointainer, false);
return myFragView;
}
}
如何使用在不同片段传递的参数动态添加一些标签?