如何在片段中获取ViewPager的选定页面索引

时间:2015-01-02 12:13:59

标签: android android-fragments android-viewpager viewpagerindicator

我正在使用ViewPagerIndicator,我的适配器定义如下:

public class MyPagerAdapter extends FragmentPagerAdapter implements
    IconPagerAdapter {

private final static String TAB_ITEMS[] = { "Tab 1", "Tab 2", "Tab 3", "Tab 4"};

private int count = TAB_ITEMS.length;

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

@Override
public int getIconResId(int index) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = new BaseFragment();

    return fragment;
}

@Override
public int getCount() {
    return count;
}

这是我的活动,其中包含我的片段:

public class MyActivity extends FragmentActivity implements
    OnPageChangeListener {

private int tabPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    MyPagerAdapter adapter = new MyPagerAdapter (
            getSupportFragmentManager());
    pager.setAdapter(adapter);

    UnderlinePageIndicator indicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
    indicator.setFades(false);
    indicator.setViewPager(pager);
    indicator.setOnPageChangeListener(this);
}

@Override
public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    setTabPosition(position);

    Toast.makeText(MyActivity.this,
            "Changed to page: " + position, Toast.LENGTH_SHORT).show();
}

public void setTabPosition(int position) {
    this.tabPosition = position;
}

public int getTabPosition() {
    return this.tabPosition;
}

}

最后定义BaseFragment

public class BaseFragment extends Fragment implements OnItemClickListener {
private final static String TAG = "BaseFragment";

private int tabPosition;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_base, container,
            false);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    tabPosition = ((MyActivity) getActivity()).getTabPosition();

    Toast.makeText(getActivity(), "page " + tabPosition, Toast.LENGTH_SHORT)
            .show();

}

现在,这是我的问题:

当选择或刷新新页面时,活动的onPageSelected中的toast总是显示正确的页面。

但是,我的片段中的toast只在您向第1页或第2页滑动时显示正确的页面。只有在第0页提供第0页时才会启动应用程序。之后,当用户滑动到第0页或第4页时,根本不会显示吐司,甚至不会显示空的吐司。

我编写了getTabPosition()setTabPosition()方法,目的是获取片段中当前选中的标签位置,但它显然无法正常工作,因此我不会在第0页上看到toast和4.

我的问题是:如何在BaseFragment中获取所选页面位置,因为我需要该位置来加载相应的数据?

希望我足够清楚?

2 个答案:

答案 0 :(得分:0)

             pager.getCurrentItem();

它返回当前片段索引

答案 1 :(得分:0)

每个片段将显示一组不同的数据,然后为每个片段分配一个片段可能是个好主意。以下是我在 FragmentPagerAdapter 中为不同数据显示不同片段所做的工作:

@Override
    public Fragment getItem(int index) {

        Fragment fragment = null;

        // Define the page for each index
        switch(index){

            case 0:
                fragment = new Fragment1();
                break;
            case 1:
                fragment = new Fragment2();
                break;
            case 2:
                fragment = new Fragment3();
                break;

        }

这可能不是最好的方法,但在您的活动中持有对ViewPager的引用,请尝试以下方法:

public class TabbedViewActivity extends FragmentActivity {

    ViewPager viewPager;
    public static int tabIndex = 0;
    @Override
    protected void onCreate(Bundle bundle) {
        ......
        viewPager = (ViewPager)findViewById(R.id.pager);
        TabbedViewActivity.tabIndex = 0; // in the start index is always 0 unless coded otherwise
        viewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                         TabbedViewActivity.tabIndex = position;

                    }
                });

}

然后你可以在Fragment TabbedViewActivity.tabIndex

中静态访问这个变量