我的应用活动之一称为 DayGallery 活动(无限图片库),
当我打开图库时,它会随机显示第一张图片,而不是从我在 DayGallery 活动代码中指定的第一张图片开始。
我想要实现的是:
FIRST:从 DayGallery 活动代码中指定的第一张图片开始,如下所示:
打开 Day1 画廊时,出现的第一张图片是:
R.drawable.day_one_1
当打开 Day2 画廊时,出现的第一张图片是:
R.drawable.day_two_1
就像所有天画廊一样,两边也保持无限滚动。
第二:如果我在画廊中停止在名为day_one_7的图像上,然后按返回以转到上一个活动并再次返回到画廊,我想看到我在离开画廊之前看到的相同图像,但如果我退出应用程序然后打开再次画廊,它必须重置以显示我在 DayGallery 活动代码中指定的第一张图片,如下图所示。
实际上我为此目的搜索了谷歌,但我找不到任何有用的东西,
任何帮助将不胜感激。
DayGallery.java:
@SuppressWarnings("deprecation")
public class DayGallery extends Activity {
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// Set the layout to use
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
tv = (TextView) findViewById(R.id.title_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
}
InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
galleryOne.setAdapter(initializeImages());
galleryOne.setSelection(galleryOne.getCount()/2);
}
private InfiniteGalleryAdapter initializeImages() {
InfiniteGalleryAdapter galleryAdapter = null;
String day = getIntent().getStringExtra("dayname");
if(day.equalsIgnoreCase("Day1")){
int[] tempimages = { R.drawable.day_one_1, R.drawable.day_one_2,R.drawable.day_one_3,
R.drawable.day_one_4, R.drawable.day_one_5,R.drawable.day_one_6,R.drawable.day_one_7,
R.drawable.day_one_8, R.drawable.day_one_9,R.drawable.day_one_10,R.drawable.day_one_11,
R.drawable.day_one_12
};
String[] name = { "00:35","00:35","00:35","1:07","2:00","2:01","2:09",
"2:12","2:15","6:13","6:13","6:13"
};
tv.setText("Day one pictures");
galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
}
else if(day.equalsIgnoreCase("Day2")){
int[] tempimages = { R.drawable.day_two_1, R.drawable.day_two_2,R.drawable.day_two_3,
R.drawable.day_two_4, R.drawable.day_two_5,R.drawable.day_two_6,R.drawable.day_two_7,
R.drawable.day_two_8, R.drawable.day_two_9,R.drawable.day_two_10,R.drawable.day_two_11,
R.drawable.day_two_12
};
String[] name = { "12:04","12:04", "12:04","12:05","12:06", "12:07",
"12:07","12:07","12:08","12:10","12:10","12:10"
};
tv.setText("Day two pictures");
galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
}
// AND THE SAME FOR REST OF DAYS TILL Day10//
return galleryAdapter;
}
}
class InfiniteGalleryAdapter extends BaseAdapter {
private Context mContext;
private int[] images;
private String[] name;
public InfiniteGalleryAdapter(Context c, int[] imageIds,String[] names) {
this.mContext = c;
images = imageIds;
name=names;
inflater = (LayoutInflater)mContext.getSystemService ( Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
private LayoutInflater inflater=null;
public class ViewHolder{
public TextView text;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = getImageView();
int itemPos = (position % images.length);
try { i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable()).setAntiAlias(true);
}
catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of memory creating imageview. Using empty view.", e);
}
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else holder=(ViewHolder)vi.getTag();
holder.text.setText(name[itemPos]);
final int stub_id=images[itemPos];
holder.image.setImageResource(stub_id);
return vi;
}
private ImageView getImageView() {
ImageView i = new ImageView(mContext);
return i;
}
}
@SuppressWarnings("deprecation")
class InfiniteGallery extends Gallery {
public InfiniteGallery(Context context) {
super(context);
init();
}
public InfiniteGallery(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
// These are just to make it look pretty
setSpacing(25);
setHorizontalFadingEdgeEnabled(false);
}
}
更新:
我添加这行代码:
galleryOne.setSelection(0);
在此行之后:
galleryOne.setSelection(galleryOne.getCount()/2);
在我的代码中,它会导致显示在 DayGallery 活动中指定的第一张图像,但它会导致单向无限滚动到左侧而不是两侧,
我们如何通过显示 DayGallery 活动中指定的第一个图像来获得我的画廊图像的双向无限滚动?
非常感谢任何帮助,谢谢。