简体   繁体   中英

Get gallery to start at specific position

I'm creating a gallery section for my app which will be used to show an X amount of images. These images are being loaded from an external server. The user is shown a GridView containing thumbnails of the images, and upon clicking on these thumbnails the Gallery -view is opened which shows the image in full-screen mode. An ArrayList is used to hold the String objects containing the links to these images. In the getView method of my Adapter this ArrayList is used to see which image needs to be shown.

So far this works fine, but I've run into a problem when a user picks a different image then the first image in the GridView . When the user clicks the 7th image for example, the Gallery shows the 7th image. But when the user flings to the right to see the previous images (6th image and before), nothing happens. When he flings to the left to see the upcoming images the user is presented with the 2nd items of the list, then the 3rd, then the 4th etc.

Obviously, the Gallery thinks that whatever image I provide it with first, is actually the first item on the list, and starts showing everything after that as it should have if the image was actually the first. But, the Gallery should show the image as the actual position it was on the GridView . How can I achieve something like that? I've tried messing around with the position I get when clicking my GridView , but this just results in the behavior I have described above.

Here's the code I used for this:

PhotoFullView (the gallery and adapter for the gallery)

package com.mobowski.appfrag.json.pictures;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.mobowski.appfrag.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class PhotoFullView extends Activity {
    /** Called when the activity is first created. */
    CustomGallery gallery;
    public ArrayList<String> links;
    private int pos;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.full_photo_gallery);

        Bundle bun = getIntent().getExtras();
        links = bun.getStringArrayList("links");
        pos = bun.getInt("pos");

        /*
         * Find the gallery defined in the main.xml Apply a new (custom)
         * ImageAdapter to it.
         */
        gallery = (CustomGallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this, links, pos));
        gallery.setSpacing(25);

    }

    public class ImageAdapter extends BaseAdapter {
        /** The parent context */
        private Context myContext;
        private ArrayList<String> links;
        int pos;

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        /**
         * All images to be displayed. Put some images to project-folder:
         * '/res/drawable/uvw.xyz' .
         */

        /** Simple Constructor saving the 'parent' context. */
        public ImageAdapter(Context c, ArrayList<String> links, int pos) {
            this.myContext = c;
            this.links = links;
            this.pos = pos;
        }

        /** Returns the amount of images we have defined. */
        public int getCount() {
            return this.links.size();
        }

        /* Use the array-Positions as unique IDs */
        public Object getItem(int position) {
            return position;
        }

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

        /**
         * Returns a new ImageView to be displayed, depending on the position
         * passed.
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);

            String imageURL = links.get(position);
            if (position == 0) {
                imageURL = links.get(pos);
            }
            try {
                URL aURL = new URL(imageURL);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                /* Buffered is always good for a performance plus. */
                BufferedInputStream bis = new BufferedInputStream(is);
                /* Decode url-data to a bitmap. */
                Bitmap bm = BitmapFactory.decodeStream(bis);

                bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                i.setImageBitmap(bm);
            } catch (Exception e) {
                e.printStackTrace();
            }

            /* Image should be scaled as width/height are set. */
            // i.setScaleType(ImageView.ScaleType.FIT_XY);
            // /* Set the Width/Height of the ImageView. */
            // i.setLayoutParams(new Gallery.LayoutParams());
            return i;
        }

    }
}

Custom gallery class (Used to overload onFling so it only shows 1 image at a time)

package com.mobowski.appfrag.json.pictures;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

public class CustomGallery extends Gallery{

    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      return super.onFling(e1, e2, 0, velocityY);
    }

}

I just managed to find out how to keep the position correctly when scrolling further after picking a custom starting image using

String imageURL = links.get(pos+position);

where pos is the position of the image clicked in the GridView . However, this still does not give me the chance to scroll back to the images BEFORE the image clicked.


Well, that was way easier then I thought. After reading the documentation again I found that I can just use setSelection(position) on my Gallery object to start at a specific position. If only all problems were this easy to fix. Thanks for reading though!

Well, that was way easier then I thought. After reading the documentation again I found that I can just use setSelection(position) on my Gallery object to start at a specific position. If only all problems were this easy to fix. Thanks for reading though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM