标签片段-Tab更改为片段

时间:2017-04-12 10:16:22

标签: android android-fragments tabs android-viewpager viewpagerindicator

我正在使用TabsPagerAdapter处理ViewPager,我已经成功创建了两个标签,如a和b。如果我点击按钮,我的按钮就会放在一个标签中,它必须是b标签。

主要活动是 -

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;
}

}

我的按钮位于Top Rated选项卡,如下所示 -

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;
}

}

当我点击上面的按钮时,它必须打开游戏标签。提前谢谢。

3 个答案:

答案 0 :(得分:2)

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

  1. public中写一个MainActivity.java方法,如下所示:

    @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;
    }
    

答案 1 :(得分:0)

您可以使用viewPager.setCurrentItem(position);选择viewPager目标网页

所以将此方法放在Activity

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

然后你可以从任何像这样的片段中调用它

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

只需传递目标标签位置

答案 2 :(得分: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