갤러리가 끝나는 것이 아니라 계속 반복 하게 구현


/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.galley;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class Gallery1 extends Activity {
    ImageAdapter imageAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_1);

        Bitmap bms[] = new Bitmap[3];
        bms[0] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_1);
        bms[1] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_2);
        bms[2] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_3);
       
        bms[2] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_1);
        bms[0] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_2);
        bms[1] = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_photo_3);

       
       
        // Reference the Gallery view
        final Gallery g = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        imageAdapter = new ImageAdapter(this, bms);
        g.setAdapter(imageAdapter);

        // Set a item click listener, and just Toast the clicked position
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // We also want to show context menu for longpressed items in the
        // gallery
        // registerForContextMenu(g);
        g.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Log.d("sel", "=" + arg2);
                if(arg2 == 2){
//                    g.setAdapter(imageAdapter);
                    imageAdapter.setNext();
                    g.setSelection(1);
                    imageAdapter.notifyDataSetChanged();
//                    imageAdapter.notifyDataSetInvalidated();
                } else if(arg2 == 0){
                    imageAdapter.setPre();
                    g.setSelection(1);
                    imageAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        g.setSelection(1);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // menu.add(R.string.gallery_2_text);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
                .show();
        return true;
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        Bitmap bms[];

        public ImageAdapter(Context c, Bitmap bms[]) {
            this.bms = bms;
            mContext = c;
            // See res/values/attrs.xml for the <declare-styleable> that defines
            // Gallery1.
            // TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            // mGalleryItemBackground = a.getResourceId(
            // R.styleable.Gallery1_android_galleryItemBackground, 0);
            // a.recycle();
        }

       
       
        public void setPre() {
            Log.d("set", "next==");
//            bms[1] = BitmapFactory.decodeResource(getResources(), R.drawable.alert_dialog_icon);
            int max = 3;
            Bitmap tmp = null;
            for(int i = max - 1 ; i >= 0 ; i--){
                if(i == max - 1){
                    tmp = bms[i];
                } else {
                    Log.d("set", "==" + (i + 1) + "==" + i);
                    bms[i + 1] = bms[i];
                }
            }
            bms[0] = tmp;           
        }



        public void setNext() {
            Log.d("set", "next==");
//            bms[1] = BitmapFactory.decodeResource(getResources(), R.drawable.alert_dialog_icon);
            int max = 3;
            Bitmap tmp = null;
            for(int i = 0 ; i < max ; i++){
                if(i == 0){
                    tmp = bms[0];
                } else {
                    Log.d("set", "==" + (i - 1) + "==" + i);
                    bms[i - 1] = bms[i];
                }
            }
            bms[max - 1] = tmp;
        }



        public int getCount() {
            return bms.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageBitmap(bms[position]);
            // i.setImageResource(mImageIds[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(136, 88));

            // The preferred Gallery item background
            // i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }

        private Context mContext;

    }

}