imbryk / loopingviewpager Goto Github PK
View Code? Open in Web Editor NEWAn android ViewPager extension allowing infinite scrolling
License: Apache License 2.0
An android ViewPager extension allowing infinite scrolling
License: Apache License 2.0
I am getting this exception
java.lang.StackOverflowError
at com.lykehq.ui.home.teasers.BannersAdapter.getCount(BannersAdapter.java:69)
at com.lykehq.views.loopviewpager.LoopPagerAdapterWrapper.getRealCount(LoopPagerAdapterWrapper.java:102)
at com.lykehq.views.loopviewpager.LoopPagerAdapterWrapper.toRealPosition(LoopPagerAdapterWrapper.java:69)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:151)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1769)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1748)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1670)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:576)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
at com.lykehq.views.loopviewpager.LoopViewPager.setCurrentItem(LoopViewPager.java:101)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:156)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1769)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1748)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1670)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:576)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
at com.lykehq.views.loopviewpager.LoopViewPager.setCurrentItem(LoopViewPager.java:101)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:156)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1769)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1748)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1670)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:576)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
at com.lykehq.views.loopviewpager.LoopViewPager.setCurrentItem(LoopViewPager.java:101)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:156)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1769)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1748)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1670)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:576)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
at com.lykehq.views.loopviewpager.LoopViewPager.setCurrentItem(LoopViewPager.java:101)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:156)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1769)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1748)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1670)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:576)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:505)
at com.lykehq.views.loopviewpager.LoopViewPager.setCurrentItem(LoopViewPager.java:101)
at com.lykehq.views.loopviewpager.LoopViewPager$1.onPageScrolled(LoopViewPager.java:156)
The LoopViewPager
is the first element inside a RecyclerView
which is inside a Fragment
which is inside a ViewPager
along with other 3 Fragment
.
So when my Activity
starts, I make a request to my backend, while I am waiting I swipe around the 4 fragments and then suddenly the app crashes. This doesn't happen always unfortunately.
If I use a normal ViewPager
everything looks fine.
Here is my adapter
public class BannersAdapter extends PagerAdapter {
private Context context;
private List<Teaser> teasers;
private AdapterListener listener;
public BannersAdapter(@NonNull Context context, @NonNull AdapterListener listener) {
this.context = context;
this.listener = listener;
}
public void setTeasers(@NonNull List<Teaser> teasers) {
this.teasers = teasers;
notifyDataSetChanged();
}
@Override
public Object instantiateItem(ViewGroup collection, final int position) {
ImageView imageView = new ImageView(context);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Teaser teaser = (Teaser) view.getTag(R.id.view_holder_tag);
listener.onTeaserClick(teaser);
}
});
collection.addView(imageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Teaser teaser = teasers.get(position);
imageView.setTag(R.id.view_holder_tag, teaser);
int width = DisplayUtils.getDisplaySize(context)[0];
String imageUrl = teaser.getImage().getUrl(width);
Glide.with(context).load(imageUrl).into(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
if (teasers == null) {
return 0;
}
LogUtils.d("TEST", String.valueOf(teasers.size()));
return teasers.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public interface AdapterListener {
void onTeaserClick(@NonNull Teaser teaser);
}
I am also using the page indicator from Jake Wharton:
bannersAdapter = new BannersAdapter(context, this);
bannersViewPager.setAdapter(bannersAdapter);
pageIndicator.setViewPager(bannersViewPager);
i implement picture preview using this, but white screen apprear when only has 2 picture , any idea?
public class SimpleGalleryAdapter extends PagerAdapter {
private List<T> mDatas;
private String imgFieldName;
private SparseArray<ImageView> mCaches = new SparseArray<>();
private String host;
private OnImageLoadListener<T> mListener;
@Override
public int getCount() {
return EmptyUtils.emptyOfList(mDatas) ? 0 : mDatas.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = container.getContext();
ImageView img_display = mCaches.get(position);
if (img_display == null) {
img_display = new ImageView(context);
img_display.setScaleType(ImageView.ScaleType.CENTER_CROP);
mCaches.put(position, img_display);
}
T mItem = mDatas.get(position);
if (mListener != null) {
mListener.onImageLoad(img_display, mItem, imgFieldName, host);
}
container.addView(img_display);
return img_display;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (mCaches.keyAt(position) != -1) {
container.removeView(mCaches.get(position));
mCaches.remove(position);
}
}
public SimpleGalleryAdapter(List<T> mDatas, String host, String imgFieldName, OnImageLoadListener<T> mListener) {
this.mDatas = mDatas;
this.imgFieldName = imgFieldName;
this.host = host;
this.mListener = mListener;
}
public SimpleGalleryAdapter(List<T> mDatas, String host, String imgFieldName) {
this.mDatas = mDatas;
this.imgFieldName = imgFieldName;
this.host = host;
this.mListener = new GlideImageLoader<T>();
}
}
that is my adapter , I user ViewPager no error , but use LoopViewPager throw this error
The specified child already has a parent. You must call removeView() on the child's parent first.
When I use addOnPageChangeListener instead setOnPageChangeListener, it will lead to some unpredictable problems. Add the following code to fix it in LoopViewPager::
【当我使用addOnPageChangeListener代替setOnPageChangeListener,这将导致一些不可预知的问题。在LoopViewPager中添加以下代码作简单的修复:】
@Deprecated
@Override
public void addOnPageChangeListener(OnPageChangeListener listener) {
// only support one listener, use setOnPageChangeListener instead
mOuterPageChangeListener = listener;
}
When use it my app do crash:
Here is my code :
tablayout.setupWithViewPager(mLoopingViewPager);
Here is my logcat:
App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.support.design.widget.TabLayout.getTabAt(TabLayout.java:518)
at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1986)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1867)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:629)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:609)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:562)
at com.imbryk.viewPager.LoopViewPager.setCurrentItem(LoopViewPager.java:110)
when i scroll to the second pager . throw java.lang.UnsupportedOperationException: Required method destroyItem was not overridden...
In my case, how to scroll first and last gridview when real pager gridview onScroll()?
Hi,
I'm wondering if there is a special way to notify the adapter with this lib ? The readme specifie two adapter, can we get access to the second one ?
i use this as circle view but when i scroll the first one to left,the last view can not visible!
i use support-v4-19.1.0
It is possible to use ViewPager to create a "carousel" effect, by having multiple items visible at the same time . This is typically done by setting e.g. ViewPager.setOffscreenPageLimit(3) and setting some appropriate margins.
In this mode, LoopingViewPager no longer works correctly at the transition between first and last items. This is because LoopViewPager has an internal fixed offset of 1 item, so if any more than one item is visible at once, these will not be shown correctly.
This can be fixed by allowing an arbitrary offset value to be set and changing the various hardcoded 1s to the new value, for example:
public static int toRealPosition( int position, int count ){
position = position-OFFSET;
if( position < 0 ){
position += count;
}else{
position = position%count;
}
return position;
}
I have this working in my case, can send patch if interested.
The problem is appears when you are using trackball, scrolling right skips some items, scrolling left doesn't work at all.
Hi,
I am trying to use your library but it is unclear what position to use in instantiateItem and in destroyItem
I need to use these methods to get hold of the fragments in the adapter, so I use your helper function
position = LoopViewPager.toRealPosition(position, getCount());
in getItem() how do I combine this with instantiateItem() and destroyItem(), thanks.
I have a listview with rows containing a viewpager. this viewpager has a set of images like gallery.I want to retain the viewpager item, as the user scrolls up or down the listview.
I have tried it many times to make it work by setting the onpagechangelistner but OnPageChangeListener always defaults to zero when scrolling the listview which has the viewpager.
I tried to do the same thing with android.support.v4.view.ViewPager and that one works fine but does not have the looping capability.
Is there a way to import the library into Android Studio? Seems like gradle is added but I don't know the compile.
IllegalStateException (java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.) when navigate from the last page to the first page or from the first page to the last page.
LoopViewPager.onPageScrolled(...) has some logic whereby if the user scrolls left from the first item (i.e. looping across the first-last item boundary), then onPageScrolled returns zeros.
if (realPosition != mAdapter.getRealCount() - 1) {
mOuterPageChangeListener.onPageScrolled(realPosition,
positionOffset, positionOffsetPixels);
} else {
if (positionOffset > .5) {
mOuterPageChangeListener.onPageScrolled(0, 0, 0);
} else {
mOuterPageChangeListener.onPageScrolled(realPosition,
0, 0);
}
}
In my case I am scaling items depending on scroll position, but the problem would occur when anyone wants to use the onPageScrolled values. Basically whatever code logic the developer implements in his own onPageScrolled, this logic will fail when scroll transitions between first and last item in the list.
I have fixed this by having LoopViewPager.onPageScrolled always calling
mOuterPageChangeListener.onPageScrolled(realPosition,
positionOffset, positionOffsetPixels);
(i.e. removing the "if"-logic).
Is there are reason for the "if"-logic? Seems better to me to always return scroll values here.
How to disable looping if there is one image view?
Hi there! Excellent component and thank you very much. It perfectly fits my requirement. I have only one issue, though and I thought I will post it to check you can throw some light on where I need to work on.
I have this Viewpager that contains 6 fragments. By implementing the LoopingViewPager, it perfectly navigates to the first fragment when I am on the last page and swipe to the left and the other way around too. I just noticed that when you do this multiple times (back and forth from the first to the last and the last to the first), there is a lag for a half second or so and then the new fragment comes into view. Is this because of the caching? I have set the boundary caching to 'true' and checked as well but the lag is still there. On the other hand, if I wait for a couple of seconds and try swiping, it is perfectly smooth.
Is there something that I need to address?
Thanks and congratulations on a great library!
Ram Iyer
The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 6, found: 2
Hi,
I've faced a problem, when if boundaryCaching is set to true and notifyDataSetChanged() is called, first and last elements of viewpager become unpredictable. In my situation, after updating the viewpager current item was replaced by first element, so duplicates appeared in view pager.
I've fixed it by forcing adapter to destroy all existing toDestroy objects while doing notifyDataSetChanged(). So maybe it should be included to library too.
LoopPagerAdapterWrapper:
public void notifyDataSetChanged() {
for(int i = 0; i < mToDestroy.size(); i++){
ToDestroy value = mToDestroy.valueAt(i);
mAdapter.destroyItem(value.container, value.position, value.object);
}
mToDestroy = new SparseArray<ToDestroy>();
super.notifyDataSetChanged();
}
The very first item shows when notifyDataSetChanged called, and there was 0 elements in the collection.
would you please add a sample demo to show how to use the class?
How can I disable the preloading in a viewpager?
OffscreenPageLimit(0) don't work because the minimum value of the parameters is 1.
Nevermind, didn't look closely enough at the samples.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.