Search code examples
androidandroid-fragmentsandroid-viewpager

Go to next page in ViewPager


I have a ViewPager in my MainActivity. Every page in it is a Fragment. So, everytime I swipe right or left, a new instance of the fragment is created and the fragment view is updated accordingly.

I also have two buttons: LEFT and RIGHT for navigation. These buttons are inside the Fragment, not in the Activity. A user can either swipe or alternatively press the relevant button to navigate between the pages.

Here's the problem: Since I'm changing the views through my MainActivity, how do I detect the onClick events of those buttons in the Activity in order to update the fragment?

Here's the PagerAdapter class (Removed all the irrelevant code from everywhere):

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {

        // logic part for the views.

        return PlaceholderFragment.newInstance(sectionNumber, questionStatus, questionOrder, showNext, showPrevious);
    }

And here's the PlaceHolderFragment class:

public class PlaceholderFragment extends Fragment{

    //some global variables

    public static PlaceholderFragment newInstance(int sectionNumber, int questionStatus, String questionOrder, boolean showNext, boolean showPrevious) {
        PlaceholderFragment fragment = new PlaceholderFragment();

        //setting up the arguments
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.explanation_fragment, container, false);

        //code for filling up all the views

        RightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //can I do something here?
            }
        });

        return rootView;
    }
}

MORE INFO: I have to keep the navigation buttons in the fragment itself and not in the activity.


Solution

  • In your fragment write one interface like:

    public class PlaceholderFragment extends Fragment{
            private OnButtonClickListener mOnButtonClickListener;
    
            interface OnButtonClickListener{
            void onButtonClicked(View view);
            }
    
            @Override
            public void onAttach(Context context) {
                super.onAttach(context);
                try {
                    mOnButtonClickListener = (OnButtonClickListener) context;
                } catch (ClassCastException e) {
                    throw new ClassCastException(((Activity) context).getLocalClassName()
                                + " must implement OnButtonClickListener");
                }
            }
    
            yourButtons.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnButtonClickListener.onButtonClicked(v);
                }
            });
         }   
    

    And in your mainactivity:

    class MainActivity extends AppCompatActivity implements   OnButtonClickListener{
    
        @Override
        void onButtonClicked(View view){
            int currPos=yourPager.getCurrentItem();
    
            switch(view.getId()){
    
                case R.id.leftNavigation:
                //handle currPos is zero
                yourPager.setCurrentItem(currPos-1);
                break;
    
                case R.id.rightNavigation:
                //handle currPos is reached last item
                yourPager.setCurrentItem(currPos+1);
                break;
            }
        }
    }