Android Gallery: Infinite Loop

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Android Gallery: Infinite Loop

Postby james.liu » Tue May 03, 2011 5:38 pm

This is my first tutorial about the android gallery. Actually, I already mentioned it before, in a reply posting. But I think it's necessary to post as a new topic.

The default android gallery can provide a common gallery function. For the common usage, you can easily get the example code from android develop website. But when u want to use some advanced usage, it's not enough for us. Here, I will discuss with u about Infinite Loop;

There is no sample way to make the default gallery infinite loop. If there is, can u tell me how, because I also want to get a very easy way to fix it :p

My way is to create a gallery extends from adapterView. And copy all original android gallery code inside. But there are several code we need to rewrite. Here I just list the most important three functions, the whole explain you can check my blog: Android Infinite Loop Gallery

Code: Select all
   @Override
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
         float velocityY) {
      // TODO Auto-generated method stub

      if (!mShouldCallbackDuringFling) {
         // We want to suppress selection changes
         // Remove any future code to set mSuppressSelectionChanged = false
         removeCallbacks(mDisableSuppressSelectionChangedRunnable);
         
         // This will get reset once we scroll into slots
         if (!mSuppressSelectionChanged) {
            mSuppressSelectionChanged = true;
         }
      }

      // Fling the gallery!
      if(velocityX < 0) {
         flingLeft = 1;
      } else if(velocityX == 0) {
         flingLeft = 0;
      } else {
         flingLeft = -1;
      }
      
      this.moveByVelocity((int) -velocityX);

      return true;
   }


   private void fillToGalleryLeft(boolean doLeft) {
      int itemSpacing = mSpacing;
      int galleryLeft = 0;

      // Set state for initial iteration
      View prevIterationView = getChildAt(0);
      int curPosition;
      int curRightEdge;

      if (prevIterationView != null) {
         curPosition = mFirstPosition - 1;
         curRightEdge = prevIterationView.getLeft() - itemSpacing;
      } else {
         // No children available!
         curPosition = 0;
         curRightEdge = getRight() - getLeft();
         mShouldStopFling = true;
      }

      // if the gallery reach the first, loop to the last one
      if (doLeft && curPosition == -1 && mSelectedPosition == 0) {
         int numChildren = getChildCount();
         int numItems = mItemCount;

         prevIterationView = getChildAt(numChildren - 1);

         if (prevIterationView != null) {
            mFirstPosition = numItems;
            curPosition = mFirstPosition - 1;
            // curRightEdge = prevIterationView.getLeft() - itemSpacing;
            curRightEdge = 0;
         }
      }

      while (curRightEdge > galleryLeft && curPosition >= 0) {
         prevIterationView = makeAndAddView(curPosition, curPosition
               - mSelectedPosition, curRightEdge, false);

         // Remember some state
         mFirstPosition = curPosition;

         // Set state for next iteration
         curRightEdge = prevIterationView.getLeft() - itemSpacing;
         curPosition--;
      }
   }

   private void fillToGalleryRight(boolean toRight) {
      int itemSpacing = mSpacing;
      int galleryRight = getRight() - getLeft();
      int numChildren = getChildCount();
      int numItems = mItemCount;

      // Set state for initial iteration
      View prevIterationView = getChildAt(numChildren - 1);
      int curPosition;
      int curLeftEdge;

      if (prevIterationView != null) {
         if (mFirstPosition == mItemCount - 1) {
            curPosition = 0;
         } else {
            curPosition = mFirstPosition + numChildren;
         }

         curLeftEdge = prevIterationView.getRight() + itemSpacing;
      } else {
         mFirstPosition = curPosition = mItemCount - 1;
         curLeftEdge = 0;
         mShouldStopFling = true;
      }

      while (curLeftEdge < galleryRight && curPosition < numItems) {
         prevIterationView = makeAndAddView(curPosition, curPosition
               - mSelectedPosition, curLeftEdge, true);

         // Set state for next iteration
         curLeftEdge = prevIterationView.getRight() + itemSpacing;
         curPosition++;
      }
   }


I also create an open source project, you can access it when u want to use it, or improve it. Details is here: Android Infinite Loop Gallery
james.liu
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 20, 2011 9:09 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests