Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For your first question, are you calling <code>setCurrentItem(int, false)</code>? If you just call setCurrentItem(int), the ViewPager will animate the transition if it has already been through its first layout with its current adapter. If that's not a solution, maybe consider initializing the <code>ViewPager</code> with <code>visibility = "invisible"</code> in your layout file (or alternatively make a call to <code>setVisibility(View.INVISIBLE)</code> prior to passing the fragment list to the <code>PagerAdapter</code>.) Then, after you call <code>setCurrentItem</code>, you can set the ViewPager back to visible using <code>setVisibility(View.VISIBLE)</code>. I would think that setting the ViewPager up in onCreate and calling <code>setCurrentItem(int, false)</code> should prevent any animations from appearing, but I don't have time to test right now.</p> <p>For the second problem, I think the part that is tripping you up is that the <code>run</code> method is going to block the entire UI thread while it's running, which means that the UI won't be able to update itself while you are looping through the items in the <code>ViewPager</code>.</p> <p>In essence, you are calling <code>Thread.sleep()</code> on the UI thread. That may not be totally clear since it's inside the Runnable's run method unless you realize that you have posted a message to run on the Handler's Thread, which will be the UI thread if you created it in an Activity life cycle method using the default Handler() constructor. Instead, you want your posted Runnable to postDelayed a new message for the next time you want to change the current item in the ViewPager.</p> <p>What I would do is something like the following (caveat - I haven't tested this):</p> <p>Subclass <code>Handler</code> to add a method to continuously loop (I am assuming you are doing this as an inner class on the Activity that holds the ViewPager so you get access to the member variable containing the ViewPager. I'm calling that member variable mPager in the code below):</p> <pre><code>private Runnable mLoopingRunnable = new Runnable(){ public void run() { int currentItem = mPager.getCurrentItem(); int totalItems = mPager.getAdapter().getCount(); int nextItem = (currentItem + 1) % totalItems; mPager.setCurrentItem(nextItem, true); handler.postDelayed(self, 500); } }; handler.postDelayed(mLoopingRunnable, 500); </code></pre> <p>If/when you want to stop looping (note that you'll need to keep a handle to the Runnable if you want to cancel it at some point):</p> <pre><code>handler.removeCallbacks(mLoopingRunnable); </code></pre> <p>Note that one advantage of changing calling <code>setCurrentItem</code> as I have above is that it won't interfere with the user swiping the ViewPager to move from one view to the next. If you just set the currentItem to some integer like you did in your loop, the behavior will appear somewhat strange when the user has interacted with the pager.</p> <p>I would have guessed that there was be something to handle the looping automatically similar to ViewFlipper, but it's not readily apparent that there is from a cursory look at the ViewPager API.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload