Reputation: 104
I have to build view pager with endless auto scroll. Also I need to show a page indicator below view pager which should respond as per the scroll events. I have applied following logic for endless auto scroll currently:
public void setupAutoPager(final int size) { final Handler handler = new Handler();
final Runnable update = new Runnable() {
public void run() {
promotionViewPager.setCurrentItem(currentPage);
if (currentPage >= size - 1) {
currentPage = 0;
} else {
++currentPage;
}
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(update);
}
}, 500, 2500);
}
Problem: When 1st item comes again from last item on auto scroll, animation of view pager is in backward direction as position is reached again (looks like flicker as going back), whereas when auto scroll from 1st to 2nd element then animation comes in forward direction (going to next element). I want animation should always be in forward direction. Backward animation comes as I am setting current item of view pager to 0 position when it reaches to end. How to implement that.
Upvotes: 1
Views: 535
Reputation: 2977
I know this post is few months old but i am a beginner at building android applications & I am facing the same problem. I would try the following approach even if it absolutely does not seem to be optimal....
ViewPager
has done its changes.getItem(int position)
and getCount()
with your array : return yourFragmentArray.get(position);
& return yourFragmentArray.size();
ViewPager
with the same fragments over & over again (+ new ones if needed) and delete the one you used to populate the VP so you never go back.
private int size = yourFragmentArray.size();
public void setupAutoPager(final int size) {
final Handler handler = new Handler();
currentPage = promotionViewPager.getCurrentItem();
final Runnable update = new Runnable() {
public void run() {
if (currentPage == size - 1) {
// Add the fragment to your fragmentArray
// Only the rank 0 needs to be taking care, works like in queue
yourFragmentArray.add(yourFragmentArray.get(0));
// Notify the changes made
yourAdapter.notifyDataSetChanged();
// Set the next item
promotionViewPager.setCurrentItem(currentPage + 1);
yourFragmentArray.remove(0);
// Update the size
size += 1;
}
else {
promotionViewPager.setCurrentItem(currentPage + 1);
}
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(update);
}
}, 500, 2500);
}
The logic is somewhere here for me, again it's probably not the best way to do it but i can't see another for now.
Good luck ! I will update this post as soon as i can test it myself.
UPDATE :
A much better way to do it is to add in this method :
firstMaxSize
the size of the ViewPager before the method is called.Use a counter (if it's the first time you loop through the adapter fragments, we won't remove them), but if(counter != 0)
we will loop through the yourFragmentArray()
and remove from 0 to firstMaxSize
.
Call yourAdapter.notifyDataSetChanged()
Then add all over again the new fragments. If the problem of an existing fragment is already added happens, create a new fragment based on the content of this one.
Call again yourAdapter.notifyDataSetChanged()
It is indeed very heavy & i am pretty sure another easiest way exists but have not found it yet.
Upvotes: 1