如果点击抽屉菜单上的项目,如何更改 HomeActivity 上的当前标签?
在 SlidingTabsBasicFragment.java 上创建的视图。它呼吁 HomeActivity.java HomeActivity 中的 DrawerMenu ,因此当点击抽屉菜单上的项目时,我需要更改 HomeActivity 上的当前标签。
PS:对不起我的英文:)
HomeActivity.java
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.tagwishes.fc.R.layout.activity_home);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
...
我需要这样的东西:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.tagwishes.fc.R.layout.activity_home);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
btn = (Button) findViewById(R.id.btn);
OnClickListener oclBtn = new OnClickListener() {
@Override
public void onClick(View v) {
//HOW TO GET mViewPager
mViewPager.setCurrentItem(3);// 3 FOR EXAMPLE
}
};
btn.setOnClickListener(oclBtn);
在 SlidingTabsBasicFragment.class 中调用mViewPager
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// BEGIN_INCLUDE (setup_viewpager)
// Get the ViewPager and set it's PagerAdapter so that it can display items
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter());
// END_INCLUDE (setup_viewpager)
// BEGIN_INCLUDE (setup_slidingtablayout)
// Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
// it's PagerAdapter set.
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setCustomTabView(R.layout.custom_tab, 0);
//mSlidingTabLayout.setSelectedIndicatorColors(R.color.bg_register);
mSlidingTabLayout.setViewPager(mViewPager);
// END_INCLUDE (setup_slidingtablayout)
}
答案 0 :(得分:0)
添加监听器NavigationDrawer
布局。并与ActionBarDrawerToggle
:
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout /* your drawer layout reference */,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
/* do what you want with View Pager */
mViewPager.setCurrentItem(item_id);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
});
ViewPager可以在onCreate()
中初始化:
ViewPager mViewPager = findViewById(R.id.pagerId);
// add adapter
mViewPager.setAdapter(mAdapter /* Your adapter */);
答案 1 :(得分:0)
我解决了我的问题:
所以我将 SlidingTabsBasicFragment.class OnViewCreated()代码复制到 HomeActivity.class 并删除 SlidingTabsBasicFragment.class 。最终的HomeActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.tagwishes.fc.R.layout.activity_home);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter());
// END_INCLUDE (setup_viewpager)
// BEGIN_INCLUDE (setup_slidingtablayout)
// Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
// it's PagerAdapter set.
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setCustomTabView(R.layout.custom_tab, 0);
//mSlidingTabLayout.setSelectedIndicatorColors(R.color.bg_register);
mSlidingTabLayout.setViewPager(mViewPager);
// END_INCLUDE (setup_slidingtablayout)
现在我可以实现oncliklistener并使用mViewPager.setCurrentItem(position)更改所选选项卡;