[英]Get gallery to start at specific position
我正在為我的應用創建一個圖庫部分,用於顯示X個圖像。 這些圖像是從外部服務器加載的。 向用戶顯示包含圖像縮略圖的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)
從特定位置開始。 如果只有所有問題都很容易解決。 謝謝你的閱讀!
嗯,這比我想象的容易。 在再次閱讀文檔后,我發現我可以在Gallery
對象上使用setSelection(position)
從特定位置開始。 如果只有所有問題都很容易解決。 謝謝你的閱讀!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.