ViewPager - 从setOnTabSelectedListener

时间:2017-06-09 13:20:18

标签: android android-fragments android-tablayout

我正在尝试在选择特定选项卡时启动活动,但是当我从该活动返回时,我希望先前显示而不是我单击的选项卡。就像你点击Instagram上的加号按钮一样。

像这样:

  • 默认片段1 /标签1

  • 转到片段5 /标签5

  • 点击标签3,活动弹出

  • 返回,参见片段5 /标签5

现在,我将最后访问过的标签索引存储在一个变量中,然后当单击标签3时,它就会转到上次访问过的标签页。

tabLayout.setOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                  //super.onTabSelected(tab); -- Should keep this or leave it commented out? 
                    int pos = tab.getPosition();
                    switch (pos) {
                        case 0:
                            viewPager.setCurrentItem(tab.getPosition(), false);
                            getSupportActionBar().setTitle("Home");
                            pre_pos = pos;
                            pre_title = "Home";
                            break;
                        case 1:
                            viewPager.setCurrentItem(tab.getPosition(), false);
                            getSupportActionBar().setTitle("Trending");
                            pre_pos = pos;
                            pre_title = "Trending";
                            break;
                        case 2:
                            viewPager.setCurrentItem(pre_pos, false);
                            getSupportActionBar().setTitle(pre_title);

                            Intent intent = new Intent(MainActivity.this, NewEvent_Activity.class);
                            startActivity(intent);
                            Toast.makeText(getApplicationContext(), 
                                    "Curr: "+ viewPager.getCurrentItem()+
                                    " Prev: " + pre_pos +
                                    " Clicked: " +tab.getPosition(), 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            viewPager.setCurrentItem(tab.getPosition(), false);
                            getSupportActionBar().setTitle("Notifications");
                            pre_pos = pos;
                            pre_title = "Notifications";
                            break;
                        case 4:
                            viewPager.setCurrentItem(tab.getPosition(), false);
                            getSupportActionBar().setTitle("Profile");
                            pre_pos = pos;
                            pre_title = "Profile";
                            break;
                    }
                }
            });

ViwPager.java中,在调用setCurrentItem()时调用此函数

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
    if (mAdapter == null || mAdapter.getCount() <= 0) {
        setScrollingCacheEnabled(false);
        return;
    }
    //I think my problem lies in the following line (mCurItem == item)
    if (!always && mCurItem == item && mItems.size() != 0) {
        setScrollingCacheEnabled(false);
        return;
    }

    if (item < 0) {
        item = 0;
    } else if (item >= mAdapter.getCount()) {
        item = mAdapter.getCount() - 1;
    }
    final int pageLimit = mOffscreenPageLimit;
    if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {
        // We are doing a jump by more than one page.  To avoid
        // glitches, we want to keep all current pages in the view
        // until the scroll ends.
        for (int i = 0; i < mItems.size(); i++) {
            mItems.get(i).scrolling = true;
        }
    }
    final boolean dispatchSelected = mCurItem != item;

    if (mFirstLayout) {
        // We don't have any idea how big we are yet and shouldn't have any pages either.
        // Just set things up and let the pending layout handle things.
        mCurItem = item;
        if (dispatchSelected) {
            dispatchOnPageSelected(item);
        }
        requestLayout();
    } else {
        populate(item);
        scrollToItem(item, smoothScroll, velocity, dispatchSelected);
    }
}

我认为这是我的问题所在但我无法编辑它。 有什么建议吗?

感谢。

2 个答案:

答案 0 :(得分:1)

在activity上实现ViewPager.OnPageChangeListener。在viewpager上设置列表器

 view_pager.setOnPageChangeListener(this);

在OnPageSelected()方法中:

@Override
public void onPageSelected(int position) {
    if (position == 3) {
        startActivity(new Intent(this,activity.class));
    }
}

覆盖活动onResume方法并写:

@Override
public void onResume() {
    super.onResume();
   view_pager.setCurrentItem(0);
}

答案 1 :(得分:0)

  

要执行此操作您必须遵循这些简单步骤

步骤:1在所有ViewPager Frgment中覆盖以下方法

 @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
    }

当您的片段可见时,将调用此方法。

步骤:2在Fragmentr3中放置条件以在片段可见时启动新活动

@Override
        public void setMenuVisibility(boolean menuVisible) {
            super.setMenuVisibility(menuVisible);
            if(menuVisible){
Intent intent = new Intent(MainActivity.this, NewEvent_Activity.class);
                            startActivity(intent);
          }
        }

现在它会打开你前面的活动。但是你的一个要求仍然是,如果用户从活动中退回,你必须显示第二个Last片段,所以你提到你正在存储Last片段的索引位置,所以按照下面的步骤。

步骤:3在ViewPager Actvity中覆盖onRestart()

@Override
    protected void onRestart() {
        super.onRestart();
        view_pager.setCurrentItem(Stored last Index);
    }

它将解决您的问题,它为我工作。