是否可以更改片段定义的i xml?
<fragment
android:id="@+id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
A有很多屏幕要显示和切换。右侧有一个类似标签的菜单。每个标签都有另一个标签。我想一个一个地改变这个。也许有人有更好的主意?
解决
我使用了ViewPager和FragmentPagerAdapter
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_content);
pager = (ViewPager) findViewById(R.id.pager);
ControllerFragment cf = new ControllerFragment(getSupportFragmentManager(), getFragment());
pager.setAdapter(cf);
}
private List<Fragment> getFragments() {
List<Fragment> list = new ArrayList<Fragment>();
FirstFragment fragment = new FirstFragment();
list.add(fragment);
return list;
}
public void onTabSelected(View v) {
switch (v.getId()) {
case R.id.ivProfile:
pager.setCurrentItem(0);
}
}
public class ControllerFragment extends FragmentPagerAdapter {
List<Fragment> fragments;
public ControllerFragment(FragmentManager fm, List<Fragment> fragments){
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0){
return this.fragments.get(arg0);
}
@Override
public int getCount(){
return this.fragments.size();
}
}