我正在为我的应用程序创建一个图库部分,用于展示X张图片。这些图片是从外部服务器加载的。用户会看到一个包含缩略图的
一个
目前为止,这个方法运作良好。但是当用户选择不同于
显然,
以下是我用于此的代码: PhotoFullView(图库和图库适配器)
自定义图库类(用于重载onFling,以便每次仅显示1个图像)
GridView
,在点击这些缩略图后,将打开Gallery
-view并以全屏模式显示该图片。一个
ArrayList
用来保存包含这些图片链接的String
对象。在我Adapter
的getView
方法中,使用这个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)
就可以从特定位置开始。如果所有问题都这么容易解决就好了。感谢您的阅读!