gpt4 book ai didi

android - ViewPager addOnPageChangeListener 不能在同一个选项卡上工作单击

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:13 25 4
gpt4 key购买 nike

我有一个带有 addOnPageChangeListenerViewPagerViewPager 有 3 个标签 View (tab1、tab2、tab3)。当用户单击 tab2 时,它会加载一些数据(基本上是一个 RecyclerView)。此时,如果用户再次点击 tab2,我需要重新加载数据,但在这种情况下 addOnPageChangeListener 不会被触发。

我的代码:

customPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(customPagerAdapter);

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

public void onPageSelected(int position) {
Log.i("TAG", "position: " + position);
switch (position) {
case 0:
addingMarker(LocationData.find(LocationData.class, "Category = ? ", "1"));
break;
case 1:
addingMarker(LocationData.find(LocationData.class, "Category = ? ", "2"));
break;
case 2:
addingMarker(LocationData.find(LocationData.class, "Category = ? ", "3"));
break;
}
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);

最佳答案

原答案:

Android 团队对 TabLayout 和 ViewPager 之间的交互方式进行了一些更改。 Read the docs .但是事情并没有很好地解释。我在代码中包含了很多注释。希望对您有所帮助。

final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
Adapter adapter = new Adapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

// the tabs will get their titles from the adapter and get populated
tabLayout.setTabsFromPagerAdapter(adapter);

// this is done "so that the tab position is kept in sync"
// what it does is when you swipe the fragment in view pager, it updates the tabs
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

// without this listener the tabs would still get updated when fragments are swiped, but .... (read the next comment)
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "tabSelected: " + tab.getText(), Toast.LENGTH_SHORT).show();
// no where in the code it is defined what will happen when tab is tapped/selected by the user
// this is why the following line is necessary
// we need to manually set the correct fragment when a tab is selected/tapped
// and this is the problem in your code
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {
Toast.makeText(MainActivity.this, "tabReSelected: " + tab.getText(), Toast.LENGTH_SHORT).show();

// Reload your recyclerView here
}
});

看看这个 issue如果您有任何其他问题。

编辑 1:2015 年 12 月

不是这个问题的解决方案,但总的来说有帮助。

tabLayout.setupWithViewPager(viewPager);

这样您就无需担心在选择选项卡时自行设置 fragment 。 tabLayout.setOnTabSelectedListener(..) 在这种情况下不再需要。即处理under the hood .这在您不需要过多控制选项卡时很有用(例如在选择/点击相同选项卡时重新加载 fragment )。

更新:2018 年 5 月

tabLayout.setTabsFromPagerAdapter(adapter);
tabLayout.setOnTabSelectedListener(...);

以上两个函数均已弃用。初始化viewpager+tablayout如下图:

viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager); // this will automatically bind tab clicks to viewpager fragments
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))

// do additional tab clicks here
// no need to manually set viewpager item based on tab click
tabLayout.addOnTabSelectedListener(...);

关于android - ViewPager addOnPageChangeListener 不能在同一个选项卡上工作单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271137/

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