onPageListener Is Not Working

172 views Asked by At

I need some help with setting up the position of the Fragments in order for it to work with the the onPageListener so I can set the visibility of certain views. As I am new, I am rather unsure on how to do this. Thank you for your time and effort.

MainActivity.java

public class MainActivity extends FragmentActivity {

    ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);


        drawerGrid = (GridView) findViewById(R.id.content);

        viewpager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(1);

        viewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {


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

            }

            @Override
            public void onPageSelected(int position) {
                                                            //need some help here//
                   if(position == 1) {
                       drawerGrid.setVisibility(View.VISIBLE);
                   }  else {
                         drawerGrid.setVisibility(View.GONE);
                       }

                }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });

    }

Fragments.java

public class Fragments extends FragmentPagerAdapter {

    public Fragments(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:                   //need some help here//

                return new Fragment1();

            case 1:

                return new Fragment2();

            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

}
1

There are 1 answers

5
rafsanahmad007 On BEST ANSWER

try this:

 ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrollStateChanged(int arg0) { }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    @Override
    public void onPageSelected(int position) {

        switch (position) {
        case 0: 
             drawerGrid.setVisibility(View.GONE);
            break;

        case 1:
            drawerGrid.setVisibility(View.VISIBLE);
            break;
        default:
            break;
        }
    }
});

use it like:

   ViewPager.addOnPageChangeListener(pageChangeListener);

   // do this in a runnable to make sure the viewPager's views are already instantiated before triggering the onPageSelected call
   viewpager.post(new Runnable()
   {
       @Override
       public void run() 
       {
           pageChangeListener .onPageSelected(viewPager.getCurrentItem());
       }
   });