简体   繁体   English

获取画廊从特定位置开始

[英]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. 我正在为我的应用创建一个图库部分,用于显示X个图像。 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. 向用户显示包含图像缩略图的GridView ,点击这些缩略图后,将打开Gallery -view,以全屏模式显示图像。 An ArrayList is used to hold the String objects containing the links to these images. ArrayList用于保存包含指向这些图像的链接的String对象。 In the getView method of my Adapter this ArrayList is used to see which image needs to be shown. 在我的AdaptergetView方法中,这个ArrayList用于查看需要显示的图像。

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 . 到目前为止这个工作正常,但是当用户选择不同的图像然后是GridView的第一个图像时,我遇到了一个问题。 When the user clicks the 7th image for example, the Gallery shows the 7th image. 例如,当用户单击第7个图像时, Gallery显示第7个图像。 But when the user flings to the right to see the previous images (6th image and before), nothing happens. 但是当用户向右翻转以查看之前的图像(第6张图像以及之前的图像)时,没有任何反应。 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. 当他向左边看到即将到来的图像时,用户会看到列表中的第2项,然后是第3项,然后是第4项等。

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. 显然, Gallery认为无论我首先提供的是什么图像,实际上是列表中的第一个项目,并且开始显示之后的所有内容,如果图像实际上是第一个,它应该具有。 But, the Gallery should show the image as the actual position it was on the GridView . 但是, Gallery应该将图像显示为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. 我已经尝试了解单击GridView时获得的position ,但这只会导致我上面描述的行为。

Here's the code I used for this: 这是我用于此的代码:

PhotoFullView (the gallery and adapter for the gallery) PhotoFullView(图库的库和适配器)

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) 自定义库类(用于重载onFling,因此它一次只显示1个图像)

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 . 其中pos是在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. 在再次阅读文档后,我发现我可以在Gallery对象上使用setSelection(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. 在再次阅读文档后,我发现我可以在Gallery对象上使用setSelection(position)从特定位置开始。 If only all problems were this easy to fix. 如果只有所有问题都很容易解决。 Thanks for reading though! 谢谢你的阅读!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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