繁体   English   中英

获取画廊从特定位置开始

[英]Get gallery to start at specific position

我正在为我的应用创建一个图库部分,用于显示X个图像。 这些图像是从外部服务器加载的。 向用户显示包含图像缩略图的GridView ,点击这些缩略图后,将打开Gallery -view,以全屏模式显示图像。 ArrayList用于保存包含指向这些图像的链接的String对象。 在我的AdaptergetView方法中,这个ArrayList用于查看需要显示的图像。

到目前为止这个工作正常,但是当用户选择不同的图像然后是GridView的第一个图像时,我遇到了一个问题。 例如,当用户单击第7个图像时, Gallery显示第7个图像。 但是当用户向右翻转以查看之前的图像(第6张图像以及之前的图像)时,没有任何反应。 当他向左边看到即将到来的图像时,用户会看到列表中的第2项,然后是第3项,然后是第4项等。

显然, Gallery认为无论我首先提供的是什么图像,实际上是列表中的第一个项目,并且开始显示之后的所有内容,如果图像实际上是第一个,它应该具有。 但是, Gallery应该将图像显示为GridView上的实际位置。 我怎样才能实现这样的目标? 我已经尝试了解单击GridView时获得的position ,但这只会导致我上面描述的行为。

这是我用于此的代码:

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;
        }

    }
}

自定义库类(用于重载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);
    }

}

我刚刚设法在使用后选择自定义起始图像后进一步滚动时如何正确保持位置

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

其中pos是在GridView单击的图像的位置。 但是,这仍然没有让我有机会在单击图像之前滚动回图像。


嗯,这比我想象的容易。 在再次阅读文档后,我发现我可以在Gallery对象上使用setSelection(position)从特定位置开始。 如果只有所有问题都很容易解决。 谢谢你的阅读!

嗯,这比我想象的容易。 在再次阅读文档后,我发现我可以在Gallery对象上使用setSelection(position)从特定位置开始。 如果只有所有问题都很容易解决。 谢谢你的阅读!

暂无
暂无

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

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