简体   繁体   中英

How can I show a button when all views are viewed in viewpager?

Right now I have a save button that I want to show only if all the views inside a viewpager are shown. That means that when the user swipes between views and have seen all views inside a viewpager, then show a save button.

I want to show the save button on every view when they have seen all views hereby after.

The trouble I am having is how to set up the logic. I started out with setting the save button invisible until the last view of the viewpager. On the last view of the viewpager, show the save button. But the problem is when the user goes to the last view (there's a save button) and then goes back to a previous view, the save button is gone.

So, I was wondering how can I show the save button permanently on all views after the user has seen all views?

Here's what I have so far:

I have this snippet inside my InstantiateItem() :

if(isViewed)
{
 save_button.setVisibility(Button.VISIBLE);
 System.out.println("Is this called? isViewed = true");


 }else if (position == numberOfPages.size()-1) {

 isViewed = true;
 save_button.setVisibility(Button.VISIBLE);
 }

where

@Override
public void onPageSelected(int position) {
isViewed = true;
}

EDIT:

I tried the following solutions but with no luck.

Button save_button = (Button) findViewById(R.id.save);
                    if(isViewed[position])
                    {
                        save_button.setVisibility(Button.VISIBLE);
                    }
                    if (position == numberOfPages.length-1 && !isViewed[position]) {
                        isViewed[position] = true;
                        save_button.setVisibility(Button.VISIBLE);
                    }
                    isViewed[position] =true;

And

isViewed[position] = true;
                    if (isViewed[position] == isViewed[numberOfPages.length-1]) {
                        save_button.setVisibility(Button.VISIBLE);
                    }
                        if (isViewed[position]) {
                            save_button.setVisibility(Button.VISIBLE);
                        } else {
                            save_button.setVisibility(Button.INVISIBLE);
                        }

boolean []isViewed = new boolean[noOfPages.size()];

 @Override
 public void onPageSelected(int position) {
        if(isViewed[position])
            {
               save_button.setVisibility(Button.VISIBLE); 
            }
        else  {
               save_button.setVisibility(Button.GONE); 
            }
      isViewed[position] = true;
 } 

In your onPageSelected , do the following

if(isViewed)
{
 save_button.setVisibility(Button.VISIBLE);
 }
 if (position == numberOfPages.size()-1) {
 isViewed = true;
 save_button.setVisibility(Button.VISIBLE);
 }

Note the above are two seperate if statements.

Make your isViewed global and default to false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM