0

我正在用TabsPagerAdapter處理ViewPager,我已成功創建了兩個選項卡,如a和b。我的按鈕被放置在一個標籤,如果我點擊按鈕,它必須b選項卡。Tab片段-Tab隨片段邊按鈕動作改變

主要活動是 -

private ViewPager viewPager; 
private TabsPagerAdapter mAdapter; 
private ActionBar actionBar; 
// Tab titles 
private String[] tabs = { "Top Rated", "Games" }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Initilization 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getActionBar(); 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

    viewPager.setAdapter(mAdapter); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);   

    // Adding Tabs 
    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 

    /** 
    * on swiping the viewpager make respective tab selected 
    * */ 
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      // on changing the page 
      // make respected tab selected 
      actionBar.setSelectedNavigationItem(position); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
     } 
    }); 
} 

@Override 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
} 

@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
    // on tab selected 
    // show respected fragment view 
    viewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
}} 

Tabpageadaper是 -

public class TabsPagerAdapter extends FragmentPagerAdapter { 

public TabsPagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int index) { 

    switch (index) { 
    case 0: 
     // Top Rated fragment activity 
     return new TopRatedFragment(); 
    case 1: 
     // Games fragment activity 
     return new GamesFragment(); 
    case 2: 
     // Movies fragment activity 
     return new MoviesFragment(); 
    } 

    return null; 
} 

@Override 
public int getCount() { 
    // get item count - equal to number of tabs 
    return 3; 
} 

}

我的按鈕被置於頂級標籤,如下所示 -

public class TopRatedFragment extends Fragment { 
private int currentPage; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false); 
    Button b1=(Button)rootView.findViewById(R.id.button); 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     } 
    }); 
    return rootView; 
} 

}

當我點擊上面的按鈕它必須打開遊戲tab.Thanks提前。

回答

0

您可以使用viewPager.setCurrentItem(position);選擇viewPager目標頁面

所以把這個方法在你Activity

public void chooseTab(int position){ 
    viewPager.setCurrentItem(position); 
} 

,然後你可以從任何片段這樣

((MainActivity) getActivity()).chooseTab(0) 

調用它只是通過目標標籤位置

0

在您的MainActivity添加一個公共方法 -

public void selectFragment(int position){ 
    viewPager.setCurrentItem(position, true); 
    // true is to animate the transaction 
} 

那麼你TopRatedFragment b1.setOnClickListener組下面這段代碼 -

((MainActivity)getActivity()).selectFragment(1); //1 is second position 
0

要更改Tab/Fragment可以使用viewPager.setCurrentItem()方法。

  1. MainActivity.javapublic方法如下圖所示:

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        ................ 
        ........................ 
    } 
    
    public void selectTab(int position) { 
        viewPager.setCurrentItem(position); 
    } 
    
  2. 使用selectTab()TopRatedFragment.java選擇GamesFragment方法。

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
    
        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false); 
        Button b1=(Button)rootView.findViewById(R.id.button); 
        b1.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          ((MainActivity)getActivity()).selectTab(1); // 1 for GamesFragment 
         } 
        }); 
    
        return rootView; 
    }