views:

480

answers:

3

hi sir

Hi I am using gallary layout my application is running when gallary moving from left ti right it is infinite that means elements are repeated again,Here is no problem,But when we move from right to left i am reached the first position after then no elements are coming.But i want to repeat elemts from this side also.Give me some suggestions

 Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setFocusable(true);
        g.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)% mImageIds.length);        
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
            {
                try {
                    imageid=position;
                    ((ImageView)findViewById(R.id.ImageViewlarge)).setImageResource(mImageIds[position]);
                    ((TextView)findViewById(R.id.TextViewImageName)).setText(imgNames[position]);
                     mp = MediaPlayer.create(SeaSpell.this,audioTrack[position]);

                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                }
            });


        }

    @Override
    protected void onStart() {
        super.onStart();
        imageid=getSharedPreferences("ResultInfo", 0).getInt("Result", 0);
        Log.v("Gallary","Imageid"+imageid);
        isUpdateUI = true;
        mRedrawHandler.handleMessage(new Message());

    } 
    @Override
    protected void onPause()
    {
        super.onPause();
        isUpdateUI = false;
    }

     class RefreshHandler extends Handler {


        @Override
        public void handleMessage(Message msg) {            
            SeaSpell.this.updateUI();
        }

        public void sleep(long delayMillis) {

            this.removeMessages(0);         
            if(isUpdateUI)
                sendMessageDelayed(obtainMessage(0), delayMillis);
        }
}
public void updateUI(){
    try
    {
     getImages();
        mRedrawHandler.sleep(3000);

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    }
      private void getImages() 
  { 
      if(imageid>=22)
          imageid=0;
      //((Gallery) findViewById(R.id.gallery));
       ((ImageView)findViewById(R.id.ImageViewlarge)).setImageResource(mImageIds[imageid]);
        ((TextView)findViewById(R.id.TextViewImageName)).setText(imgNames[imageid]);

            imageid++;

}  

screenshot frontgallery http://www.freeimagehosting.net/image.php?d816500473.png how can i make gallery view as circular.i am able to done left to right infinite when i drag right to left.it is showing end point i am new in android give me some suggestion

+4  A: 

In getView:

if(position>21){
    position=0;
}

this should be removed as it should be handled by the checkPosition function.

In checkPosition:

For the modulus operator (%), given a % b if 0 <= a < b then the result will be a. For b <= a < 2*b then the result will be a-b, so if b == a, then the result is 0. This continues for any positive integer, so the check should be:

if (position > 0)
    position = position % mImageIds.length;

Now, what you are missing from this is handling the case where position < 0.

a    a%3    a    a%3    f(a)
0    0      0    0      0
1    1     -1   -1      2
2    2     -2   -2      1
3    0     -3    0      0
4    1     -4   -1      2
5    2     -5   -2      1
6    0     -6    0      0

What we want in this case is for it to wrap back around to the end of the list -- the f(a) in the table above.

As can be seen in the table above, if a is negative then -b < a <= 0. Also, if we make f(a) = (a % b) + b we get our desired result.

This means that the logic in checkPosition becomes:

position = position % mImageIds.length;
if (position < 0)
    position = position + mImageIds.length;

which should work for all values of position irrespective of the value of mImageIds.length.

reece
thanku for responding reece i am placed this code last image adding continue images working fine but not adding first image 0 position added -21 -20 -19.... i am forward some screenshot first image front not added images (whitespace)http://www.freeimagehosting.net/image.php?d816500473.png
narasimha
I'm sorry, I don't understand what you mean -- did you add the position < 0 case to checkPosition?
reece
yes reece in gallery first image before not added into last imagesand its display empty space after first imagecyclic order i am take 4 images order(back...3 0 1 2 3 0 1 2 3 0 1.....front)this cyclic order front back front is working fine back is not working
narasimha
please forward some idea
narasimha
thanku very much reece this is working fine
narasimha
+1  A: 

If anyone wanted to make it also go backwards, you can implement this. All it really does it is start the gallery in the middle.

GalleryName.setSelection((int)( Integer.MAX_VALUE / 2 ) - ( Integer.MAX_VALUE / 2 ) % mImageIds.length);
gogothee
hi gogothee gallery in the perfectly working thanku but one problem is there image rotation one by one but gallery is not rotatiing i will paste save the code above please some solution this rotation i am apply to handler
narasimha
thanku very much gogothee its working fine
narasimha
+1  A: 

Hello, Where to put the if condition ?

check position position = position % mImageIds.length;
narasimha