I have a gallery viewer for an Android application.
I need to show, first, a big picture, and then below it, the thumbnails of other pictures. After 5 seconds, the thumbnails disappear, and when the user touches the screen, the thumbnails should appears again.
The problem here is that, if the thumbnails appear again, they won't disappear anymore, thus this only works the first time.
How could I fix this issue?
This is my layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/GalleryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
</ImageView>
<Gallery
android:id="@+id/GalleryThumbnails"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/GalleryImage" />
</RelativeLayout>
This is my implementation:
public class GalleryView extends Activity {
Integer[] pics = { R.drawable.antartica1, R.drawable.antartica2, R.drawable.antartica3, R.drawable.antartica4, R.drawable.antartica5, R.drawable.antartica6, R.drawable.antartica7,
R.drawable.antartica8, R.drawable.antartica9, R.drawable.antartica10 };
ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_view_layout);
final Gallery ga = (Gallery) findViewById(R.id.GalleryThumbnails);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.GalleryImage);
// We show the first image when creating the view gallery
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(pics[0]);
// Hiding the thumbnails after 5 seconds
final View v = new View(this);
v.postDelayed(new Runnable() {
public void run() {
ga.setVisibility(View.GONE);
}
}, (long) 5000);
// Showing the thumbnails again when touching the screen
// TODO: Not working yet
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ga.setVisibility(View.VISIBLE);
}
return false;
}
});
ga.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(pics[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return pics.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
// Thumbnails gallery
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(200, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Thanks a lot in advance!
This is strange code
final View v = new View(this);
v.postDelayed(new Runnable() {
public void run() {
ga.setVisibility(View.GONE);
}
}, (long) 5000);
You are creating a dangling view in order to schedule a delayed call to ga.
I guess you meant
private Gallery ga; // make ga to a member
private void scheduleHide ()
{
ga.postDelayed(new Runnable() {
public void run() {
ga.setVisibility(View.GONE);
}
}, (long) 5000);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_view_layout);
ga = (Gallery) findViewById(R.id.GalleryThumbnails);
...
scheduleHide ();
...
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ga.setVisibility(View.VISIBLE);
scheduleHide ();
}
return false;
}
});
...