0

I had a problem with my ViewPager, I had an OnClickListener all tabs click on my emulator on my computer, however on my phone its a little weird, only the middle tab clicks, the other 2 don't at all. I even put logs in there and they didn't log anything. Even worse, there is no error, it just doesn't do anything.

Small Part
    vpPager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (vpPager.getCurrentItem() == 0) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 1) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 2) {
                zmanimListPopUp();
            }

        }


    });

Please help me!

Thanks!

UPDATES

Large Part

 vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            prefs.edit().putInt("currentFragmentItem", position).commit();
            Log.d("MainActivity", "Position: " + prefs.getInt("currentFragmentItem", 1));
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    vpPager.setCurrentItem(prefs.getInt("currentFragmentItem", 1));
    Log.d("MainActivity", "Position on launch: " + prefs.getInt("currentFragmentItem", 1));


    DotsIndicator dotsIndicator = (DotsIndicator) findViewById(R.id.dots_indicator);
    dotsIndicator.setViewPager(vpPager);
    dotsIndicator.setPointsColor(Color.rgb(142, 96, 97));

    onClickListener();

}

private void onClickListener() {


    vpPager.setOnTouchListener(
            new View.OnTouchListener() {
                private boolean moved;

                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        moved = false;
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                        moved = true;
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        if (!moved) {
                            view.performClick();
                        }
                    }

                    return false;
                }
            }
    );

    vpPager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (vpPager.getCurrentItem() == 0) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 1) {
                zmanimListPopUp();
            } else if (vpPager.getCurrentItem() == 2) {
                zmanimListPopUp();
            }

        }


    });


}

Changes Made:

    vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {


        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            vpPager.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (vpPager.getCurrentItem() == 0) {
                        zmanimListPopUp();
                    } else if (vpPager.getCurrentItem() == 1) {
                        zmanimListPopUp();
                    } else if (vpPager.getCurrentItem() == 2) {
                        zmanimListPopUp();
                    }

                }


            });
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });
Edon Freiner
  • 338
  • 2
  • 17

2 Answers2

1

try vpPager.setOnPageChangeListener..... and public void onPageSelected(int position) {} from inside to listen to pages selected and return the position of page and accordingly take action, like you code.

 tvPager.setOnPageChangeListener(new OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                    zmanimListPopUp();
                } else if (position == 1) {
                    zmanimListPopUp();
                } else if (position == 2) {
                    zmanimListPopUp();
                }

        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // Code goes here
        }

        // Called when the scroll state changes: 
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });
ams73
  • 146
  • 5
  • Im sorry I am a little confused, I am new to android what exactly will both those things be doing? I have updated the question with more code to shed some light on the issue. – Edon Freiner Dec 14 '17 at 14:19
  • the onClick will fire whenever a click take place, if this is what you want than fine, but if you want to run code based on a selected page and than use the following: I have added to my answer. – ams73 Dec 14 '17 at 14:22
  • Thanks! I made some changes, I am not sure if they are right, but it works for the middle one only again. I am not sure where the onClick goes and how it works with the `onPageScrolled` and the `onPageScrollStateChanged` – Edon Freiner Dec 14 '17 at 14:36
  • I thought you should put it under the onPageSeleted (the way I added to my answer). Let me understand what you want to achieve here, in other words what [zmanimListPopUp(); ] should do? – ams73 Dec 14 '17 at 14:51
  • if you do it as mentioned, your code should run whenever you navigate a page and the variable position will pass in the page number, you use it to take the right decision (as I could understand from your old code) – ams73 Dec 14 '17 at 15:00
  • What I am trying to achieve is a 3 tab page viewer and everytime I click on a tab, a new activity will open, here is shows the same activity, however, I have different ones to switch out. Your answer does this only when you swipe, however I want to do it when I click. – Edon Freiner Dec 14 '17 at 16:42
  • Also, the original code I wrote works, and it worked on a mobile device as well. just now, it doesn't work anymore, not sue why. It only works on an emulator. – Edon Freiner Dec 14 '17 at 16:51
  • check this old question https://stackoverflow.com/questions/10243690/onclick-on-viewpager-not-triggered especially answer by bhekman – ams73 Dec 14 '17 at 19:17
  • this explains that some events can stop others, for example touch listener on pager stop click event to reach click listener, you can solve it by returning false as explained. Personally I did not check it before and hope it works with you. – ams73 Dec 14 '17 at 19:28
0

We have faced automatically load previous/next tab’s data automatically issue during implementing tabs in our android applications. But we can stop this for Material Tabs or PageSlidingTabStrip Tabs by adding “setUserVisibleHint” method. Add This method to all your tab's fragment and load all operation using this method. this method load fragment data only when its tab selected so it load one tab at each time.

// initialize boolean to know tab is already loaded or load first time
private boolean isFragmentLoaded=false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser && !isFragmentLoaded ) {
        // Load your data here or do network operations here
        isFragmentLoaded = true;
    }
}
Ramesh R
  • 7,009
  • 4
  • 25
  • 38