我有 2 个选项卡,名为 tab1(主页)和 tab2(配置文件),使用TabPageIndicator
和FragmentPagerAdapter
库。
在 tab2 中,我有一个名为“登录”(tab2 inflates abc.xml
)的按钮。
当用户单击“登录”按钮时,应用程序将在新活动中显示登录表单。登录后,活动结束,tab2 的内容视图将更改(它显示用户的个人资料,因为它会膨胀xyz.xml
)。我怎样才能做到这一点?
public class MainActivity extends SherlockFragmentActivity {
private static String[] TAB_NAMES = null;
private Context context = this;
private static FragmentPagerAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (TAB_NAMES == null) {
TAB_NAMES = new String[] {
getResources().getString(R.string.tab_home),
getResources().getString(R.string.tab_profile) };
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new TabsAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infalter = getSupportMenuInflater();
infalter.inflate(R.menu.main_menu, menu);
return true;
}
class TabsAdapter extends FragmentPagerAdapter {
public TabsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HomeFragment.getInstance();
case 1:
return new ProfileFragment();
default:
return HomeFragment.getInstance();
}
}
}