我有ViewPager
addOnPageChangeListener
。 ViewPager
有3个标签视图(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);
答案 0 :(得分:8)
原始答案:
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);
这样,在选择选项卡时,您无需担心自己设置片段。在这种情况下不再需要tabLayout.setOnTabSelectedListener(..)
。这是处理under the hood。当您不需要对选项卡进行过多控制时(例如,在选择/点击相同选项卡时重新加载片段),这非常有用。
更新日期: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(...);
答案 1 :(得分:1)
听起来你的$collection = Mage::helper('catalogsearch')->getQuery()->getSearchCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('description')
->addSearchFilter($this->getQuery())
->setCurPage($this->getStart())
->setPageSize($this->getLimit())
->load();
回调没有被调用,因为页面实际上没有变化。我想如果你编写onPageSelected
的自定义子类,你可以使用TabLayout.ViewPagerOnTabSelectedListener
回调来触发刷新,即使那些选定的页面索引没有改变。
您应该可以在 之后使用onTabReselected
安装自定义OnTabSelectedListener
,并致电TabLayout::setOnTabSelectedListener
。