Android: ViewPager's onPageSeletected() method logic is slowing down the swipe

Viewed 58

I'm new to programming and implemented a ViewPager for the first time.

The ViewPager contains 2 Fragments. When someone swipes back from Fragment 2 to Fragment 1, I need to create some logic to check for changes. Here's what I have so far:

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

        }

        @Override
        public void onPageSelected(int position) {


            if (mPager.getCurrentItem() == 0) {

            incentiveWindowReference.deActivateDeleteMode();
            TextView bubbleText = (TextView) findViewById(R.id.bubble_text_xml);

            boolean hasActiveItems = false;
            for (int i = 0; i < incentiveList.size(); i++) {
                if (incentiveList.get(i).getActiveOrInactive()) {
                    hasActiveItems = true;
                }
            }
            if (hasActiveItems) {

                bubbleText.setText("Finish Pomodoros to unlock Incentives!");
            } else bubbleText.setText("No active Incentives!");

            if (!timerWindowReference.getCurrentlyBreaking()) {
                timerWindowReference.pause();
                timerRunning = false;
                Button startStopButton = (Button) findViewById(R.id.button);
                startStopButton.setText("START");

                timerWindowReference.activateResetSkip();}
            }



        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

The problem is, this part is making the ViewPager laggy. If I comment it out, it is smooth. But I don't know how else I could do it. If someone swipes from Fragment 2 back to Fragment 1 (or 1 to 0) and did some changes in Fragment 2, I need the bubbleText in Fragment 1 to change.

EDIT:

So i narrowed it down. Calling the method "deactivateDeleteMode" is causing the lag. This is the content of this method:

public void deActivateDeleteMode() {
    delete.setText("DELETE");
    addIncentiveButton.setVisibility(View.VISIBLE);
    mDeleteMode = false;
    setAdapter();
}

The problem seems to be "setAdapter()"

The reason i call setAdapter, is, that it fills the List with a different images when mDeleteMode == false. If mDeleteMode == true, it shows a trashbin icon and deletes the item on click. If mDeleteMode == false, it shows the normal image.

I want the deleteMode to be deactivated, if someone swipes away from the Fragment.

0 Answers
Related