Android Gallery: Infinite Loop

This is a discussion on Android Gallery: Infinite Loop within the Beginning Developers forums, part of the Android Development category; 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 ...

Results 1 to 1 of 1

Thread: Android Gallery: Infinite Loop

  1. #1
    Android Lurker jsliu111's Avatar
    Join Date
    Apr 2011
    Posts
    2

    Android Gallery: Infinite Loop

    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
    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:
     @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
    Last edited by jsliu111; 10-19-2011 at 05:03 AM.

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Droid X boot loop
    By Rangerwolf in forum Rescue Squad Help
    Replies: 38
    Last Post: 02-27-2011, 01:14 AM
  2. Droid X boot loop
    By Rangerwolf in forum Motorola Droid X
    Replies: 28
    Last Post: 02-26-2011, 01:36 AM
  3. Gallery Issues
    By Epic Forums User in forum Samsung Epic 4G
    Replies: 0
    Last Post: 12-20-2010, 01:00 AM

Search tags for this page

android advanced gallery
,
android dev gallery loop
,

android gallery

,
android gallery code example
,

android gallery example

,

android gallery infinite loop

,

android gallery loop

,

android infinite gallery

,
android loop example
,
gallery android example
,
gallery android example code
,
gallery in android example code
,

infinite gallery android

,
infinite scrolling gallery android
,
loop gallery android
Click on a term to search our sites for related topics.
Android Forum