gpt4 book ai didi

android - ViewPager - 从 setOnTabSelectedListener 启动 Activity

转载 作者:行者123 更新时间:2023-11-30 00:30:40 25 4
gpt4 key购买 nike

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

像这样:

  • 默认 fragment 1/标签 1

  • 转到 fragment 5/Tab 5

  • 点击tab 3,弹出 Activity

  • 返回,查看 Fragment 5/Tab 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);
}
}

我认为这是我的问题所在,但我无法对其进行编辑。有什么建议吗?

谢谢。

最佳答案

在 Activity 上实现 ViewPager.OnPageChangeListener。在 viewpager 上设置监听器

 view_pager.setOnPageChangeListener(this);

在 OnPageSelected() 方法中:

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

覆盖 Activity 的 onResume 方法并写入:

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

关于android - ViewPager - 从 setOnTabSelectedListener 启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44458943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com