Reputation: 1854
I have three nested fragments (inside a main fragment) that I implement using TabLayout
with ViewPager
. In my main fragment i have a Spinner
view that filters the content of the first two fragments. However, i do not want this Spinner
view to appear in my third fragment. To implement it I use addOnPageChangeListener()
as shown below:
mallDropDown
is my Spinner
view.
However, this implementation works only if i select the fragments by clicking on the title. If i scroll through the fragments by swiping then it is very unstable i.e. sometimes the Spinner
appears when it shouldn't have and so on.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
mallDropDown.setVisibility(View.VISIBLE);
break;
case 1:
mallDropDown.setVisibility(View.VISIBLE);
break;
case 2:
mallDropDown.setVisibility(View.GONE);
break;
default:
mallDropDown.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Upvotes: 0
Views: 720
Reputation: 403
Check this onPageScrollStateChanged, it may work
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
if(state == ViewPager.SCROLL_STATE_IDLE) {
// hide if not
}
}
Upvotes: 1