我创建将被用于显示图像的X量我的应用程序画廊部件开始。这些图像正从外部服务器加载。用户被显示为包含图像缩略图的GridView
,并且在点击这些缩略图时,打开Gallery
-视图,其以全屏模式显示图像。 ArrayList
用于保存包含这些图像链接的String
对象。在我的Adapter
的getView
方法中,此ArrayList
用于查看需要显示哪个图像。获取画廊在特定位置
到目前为止,这工作得很好,但我已经当用户选择一个不同的图像,然后在GridView
第一图像遇到了问题。例如,当用户点击第7张图像时,Gallery
显示第7张图像。但是当用户向右滑动以查看先前的图像(第6张图像和之前)时,没有任何反应。当他向左转动以查看即将到来的图像时,向用户呈现列表的第二项,然后是第三项,然后是第四项等。
很明显,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
点击自定义启动图像后进一步滚动时保持正确的位置。但是,这仍然不能让我有机会在点击图像之前回滚到图像。
那么,那是比较容易,然后我想。 阅读文档后,再次我发现我可以只使用setSelection(position)
我Gallery
对象在特定的位置开始。如果只有所有问题都很容易解决。尽管感谢阅读!
感谢您的回答家伙......否则,我可能失去了一些宝贵的时间.. – 2012-04-30 13:12:35