Lena Bru
Lena Bru

Reputation: 13947

Viewpager with fragments

I have a viewpager in my app with 7 fragments, each represent their own logic

however, there are 2 fragments that are mutually coupled, one cannot exist without the other.

I have 2 buttons, previous and next at the bottom of the screen which switch between the fragments.

I want to make it so the swiping in the viewpager cannot reach one of the coupled fragments

Right now I did this: I put the first fragment (of the coupled ones) to be at location 0, and the 2nd fragment at the last position, so that when you swipe left and right you reach the 2nd coupled fragment last.

I want to make it so you can't reach the last fragment with swiping

how to do that ?

EDIT:

Fragment Adapter

public class PurchaseFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

private List<BaseFragment> fragments;
private boolean shouldShowLastFragment;
@Inject
public PurchaseFragmentStatePagerAdapter(FragmentManager fm) {
    super(fm);
}

public void setFragments(List<BaseFragment> fragments) {
    this.fragments = fragments;
}

@Override
public BaseFragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments == null ? 0 : shouldShowLastFragment ? fragments.size() : fragments.size()-1;
}

public void enableItem(int i) {
    shouldShowLastFragment = true;
    notifyDataSetChanged();
}

public void disableItem(int i) {

    shouldShowLastFragment = false;
    notifyDataSetChanged();
}

}

Upvotes: 2

Views: 174

Answers (1)

cYrixmorten
cYrixmorten

Reputation: 7108

I assume you have either a FragmentStatePagerAdapter or a FragmentPagerAdapter.

In your adapter, setting the getCount() method to 6, will make sure that you cannot reach the last fragment.

@Override
public int getCount() {
    return 6;
}

Upvotes: 0

Related Questions