SRKS
SRKS

Reputation: 218

View Pager - based on condition check, not to add few items

I have an array list and I want to do this logic

In the pager Adapter -

     getCount() returns arrayList.size()

In the PagerAdapter - istantiateItem()

 if(condition met)

          add item to view

          return view
else 
    return null

When I do that - whenever condition is not met , I see a blank item on the View Pager. How to not add a blank item to View Pager.

Upvotes: 1

Views: 182

Answers (1)

Nabin Bhandari
Nabin Bhandari

Reputation: 16409

You need to override getCount() method as well as getItem() method with your condition check.

@Override
public int getCount() {
    int size = 0;
    for (Fragment fragment: fragmentList) {
        if (fragment.isOK()) {
            size++;
        }
    }
    return size;
}

@Override
public Fragment getItem(int position){
    int index = 0;
    for (Fragment fragment: fragmentList) {
        if (fragment.isOK()) {
            if(index == position){
                return fragment;
            }
            index++;
        }
    }
    return null;
}

Upvotes: 1

Related Questions