Created
September 20, 2018 19:56
-
-
Save Yundin/1e7f89b8f179895bd68c004c76a7c32c to your computer and use it in GitHub Desktop.
InfinitePhotosPagerAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import java.util.List; | |
/** | |
* @author Yundin Vladislav | |
*/ | |
public class InfinitePhotosPagerAdapter extends PhotosPagerAdapter { | |
FragmentManager fragmentManager; | |
public InfinitePhotosPagerAdapter(FragmentManager fm) { | |
super(fm); | |
this.fragmentManager = fm; | |
} | |
public InfinitePhotosPagerAdapter(FragmentManager fm, List<PhotoFragment> fragments) { | |
super(fm, fragments); | |
this.fragmentManager = fm; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return PhotoFragment.newInstance(fragments.get(position % fragments.size()).getImageUrl()); | |
} | |
@Override | |
public int getCount() { | |
return Integer.MAX_VALUE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.annotation.NonNull; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* @author Yundin Vladislav | |
*/ | |
public class PhotosPagerAdapter extends FragmentPagerAdapter { | |
List<PhotoFragment> fragments = new ArrayList<>(); | |
public PhotosPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
public PhotosPagerAdapter(FragmentManager fm, List<PhotoFragment> fragments) { | |
super(fm); | |
this.fragments = fragments; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return fragments.get(position); | |
} | |
// notifyDataSetChanged doesn't work correctly without this code | |
public int getItemPosition(@NonNull Object object) { | |
return POSITION_NONE; | |
} | |
@Override | |
public int getCount() { | |
return fragments.size(); | |
} | |
public void replacePhotos(List<PhotoFragment> fragments) { | |
this.fragments.clear(); | |
this.fragments.addAll(fragments); | |
notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment