0

I'm trying to have a button appear on the last page of a viewPager (which contains images in fragments). In these fragments, I'm creating a button that is invisible until the last page is shown. So far, I've achieved this behavior, but I'm having a bug where in page 2 the button appears, even though I'm "showing" (with setVisibility(View.VISIBLE)) the button only when the current page is greater or equal than 3 (there are 5 pages total, and also, greater or equal than 3 because the viewPager.getCurrentItem() behaves oddly and won't count the last or first page as an index).

My custom fragment's onCreateView() looks like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.imageview, container, false);
    ivImage = (ImageView) root.findViewById(R.id.ivImageView);

    btn_register = (Button) root.findViewById(R.id.button_register);

    if( ((WelcomeActivity) this.getActivity()).test()>=3){
        btn_register.setVisibility(View.VISIBLE);
    } 

    setImageInViewPager();
    return root;
}

In my main activity, the test() method simply returns the current page the viewPager is in:

public int test(){
    return(viewPage.getCurrentItem());
}

How could I better detect the last page of a viewpager and use it so that I only display the button when the user is in the last page of the viewpager? thanks!

EDIT: Here is a video of the problem. HEre are the contents of my src folder: WelcomeActivity.java, FragmentPagerAdapter.java, Images.java, FragmentImageView.java.

jjcastil
  • 189
  • 1
  • 15

2 Answers2

0

In onPageSelected(), check the position : if(position==5) then show the button. Here I used 5 because you mention that there are 5 pages.

@Override
public void onPageSelected(int position) {
    if(position == 5)
    btn_register.setVisibility(View.VISIBLE);
}

Try this hope it will help you :)

Biswajit
  • 1,829
  • 1
  • 18
  • 33
  • Hmm I control the button on the Fragment class rather than on the Activity, so I don't have access to the button from the Activity. – jjcastil Jan 02 '15 at 08:13
  • Means when the fragment opens then the button will be visible, then why do you need to hide the button? I cant understand what is your problem. – Biswajit Jan 02 '15 at 08:19
  • Well, I have this button that I decide to create on the fragment (so that it will follow the swipe, [for visuals](http://i.stack.imgur.com/OQdjm.gif)), and in the layout I define it so that it is invisible at first, until I reach the last page and I set it to visible (this is how I could do it). All is good but when i swipe back, the button appears on page 2, and it shouldn't! I'm sure there's a better way of doing this, but I don;t know it haha – jjcastil Jan 02 '15 at 08:25
  • Ok its good. Can you please show me a screenshot of your application then I can imagine what is the best way to achieve your goal. :) – Biswajit Jan 02 '15 at 09:47
  • Yes I got that problem. – Biswajit Jan 02 '15 at 09:49
0

You need to somehow tie the count of fragments to the individual fragments. So that each fragment knows its index. Can you add your code on how and when you are creating/adding the fragments to the view-pager?
Basing on that you can add a param to the arguments bundle while initialising the fragment there by fragment knows it's index in the view-pager.
Or else try this,

private int mCurrentPageSelected;   
// In your activity    
@Override
public void onPageSelected(int position) {
    mCurrentPageSelected = position;
}

public int getSelectedPagePosition(){
    return mCurrentPageSelected;
}

// In your fragment
public void onAttach(){
    Log.d(TAG, "Fragment attached and current selected position is" + etActivity().getSelectedPagePosition());
    if(getActivity().getSelectedPagePosition() >= 3){
        // show the btn
        btn_register.setVisibility(View.VISIBLE);
    }else{
        //hide the button
       btn_register.setVisibility(View.GONE);
    }
}
Sriram
  • 55
  • 6
  • I tried this and with onResume the app crashes at start and it doesn't do anything with onAttach (the button never appears). I'll post my code in a bit – jjcastil Jan 02 '15 at 10:25
  • I added some logs, can you try with that to understand the values. You can have "btn_register" as class member and initialize in onCreateView() . – Sriram Jan 02 '15 at 15:58
  • Just tried it and no log gets output on logcat, suggesting onAttach() never gets executed :( – jjcastil Jan 02 '15 at 23:04