我創建將被用於顯示圖像的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