context: 
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Threading;
  using System.Threading.Tasks;
  
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Com.Bumptech.Glide;
  
  using Android.Support.V4.View;
  using Android.Support.V4.Widget;
  using Android.Support.Design.Widget;
  using Android.Support.V7.Widget;
  using Android.Support.V7.View;
  using PhotoView = Com.Github.Chrisbanes.Photoview;
  using Toolbar = Android.Support.V7.Widget.Toolbar;
  using Android.Support.V7.App;
  using Java.Lang;
  using Emmaus.Widget;
  using Com.Bumptech.Glide.Request;
  using Com.Bumptech.Glide.Request.Target;
  using NLog;
  
  namespace HappyPandaXDroid
  {
      [Activity(Label = "GalleryViewer", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
      public class GalleryViewer : AppCompatActivity
      {
          
          Toolbar toolbar;
          public TextView page_number;
          public RecyclerViewPager galleryPager;
          bool overlayVisible = true;
          public  ImageAdapter adapter;
          public RequestOptions options;
          FrameLayout lay;
          private static Logger logger = LogManager.GetCurrentClassLogger();
          /*List<string> ImageList =
              new List<string>();*/
          List<Core.Gallery.Page> PageList =
              new List<Core.Gallery.Page>();
          SeekBar seekbar;
          UICountdown countDown;
  
          Custom_Views.ImageViewHolder imageView;
          bool doubl_click = false;
          public int activityID;
          protected override void OnCreate(Bundle savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
  
              // Create your application here
              SetContentView(Resource.Layout.GalleryLayout);
              
                  string data = Intent.GetStringExtra("page");
                  PageList = Core.JSON.Serializer.SimpleSerializer.Deserialize<List<Core.Gallery.Page>>(data);
              logger.Info("Initializing Gallery Viewer");
              //InitPageGen();
  
              options = new RequestOptions()
                  .Override(Target.SizeOriginal, Target.SizeOriginal);
              toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
              SetSupportActionBar(toolbar);
              lay = FindViewById<FrameLayout>(Resource.Id.frame);
              galleryPager = FindViewById<RecyclerViewPager>(Resource.Id.galleryViewPager);
              var layout = new ExtraLayoutManager(this, LinearLayoutManager.Horizontal, false);    
              galleryPager.SetLayoutManager(layout);
  
              adapter = new ImageAdapter(PageList,this);
              galleryPager.SetAdapter(new RecyclerViewPagerAdapter(galleryPager, adapter));
              SupportActionBar.SetDisplayHomeAsUpEnabled(true);
              countDown = new UICountdown(2000, 10, this);
  
              seekbar = FindViewById<SeekBar>(Resource.Id.progress_seekbar);
              seekbar.Max = PageList.Count;
              seekbar.Progress = galleryPager.CurrentPosition + 1;
              galleryPager.GetAdapter().NotifyDataSetChanged();
              page_number = FindViewById<TextView>(Resource.Id.page_number);
              page_number.Text = seekbar.Progress.ToString();
              galleryPager.AddOnPageChangedListener(new PageChangeListener(this));
              seekbar.SetOnSeekBarChangeListener(new SeekBarChangeListener(this));
              logger.Info("Gallery Viewer Initialized");
              /*int pos = list.IndexOf(data);
              if (pos < 0)
                  pos = 0;
              imageHandler.StartReader(galleryPager, this, pos, galleryPager);*/
  
           }
  
          protected override void OnResume()
          {
              base.OnResume();
          }
  
          protected override void OnDestroy()
          {
              logger.Info("Closing Gallery Viewer");
              base.OnDestroy();
          }
  
          public override bool OnTouchEvent(MotionEvent e)
          {
              {
                  ToggleOverlay(true);
              }
              return base.OnTouchEvent(e);
          }
  
          public override bool OnCreateOptionsMenu(IMenu menu)
          {
              MenuInflater.Inflate(Resource.Menu.viewer_menu, menu);
              return base.OnCreateOptionsMenu(menu);
          }
  
          public override bool DispatchTouchEvent(MotionEvent ev)
          {
              
              bool res = base.DispatchTouchEvent(ev);
              return res;
          }
  
  
          public class PhotoImageVIew: PhotoView.PhotoView
          {
              GalleryViewer mactivity;
              public PhotoImageVIew(Context context) : base(context)
              {
                  if (context is GalleryViewer)
                      mactivity = (GalleryViewer)context;
              }
              
          }
          
  
          public class SeekBarChangeListener : Java.Lang.Object, SeekBar.IOnSeekBarChangeListener
          {
              GalleryViewer activity;
              private static Logger logger = LogManager.GetCurrentClassLogger();
              public SeekBarChangeListener(GalleryViewer activity)
              {
                  this.activity = activity;
              }   
              public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
              {
                  //throw new NotImplementedException();
              }
  
              public void OnStartTrackingTouch(SeekBar seekBar)
              {
                  //throw new NotImplementedException();
              }
  
              public void OnStopTrackingTouch(SeekBar seekBar)
              {
                  int new_position = seekBar.Progress;
                  if (new_position < 1)
                      new_position = 1;
                  seekBar.Progress = new_position;
                  activity.galleryPager.ScrollToPosition(new_position - 1);
                  activity.page_number.Text = seekBar.Progress.ToString();
  
              }
          }
  
          public class PageChangeListener : RecyclerViewPager.OnPageChangedListener
          {
              GalleryViewer mactivity;
              private static Logger logger = LogManager.GetCurrentClassLogger();
              public PageChangeListener(GalleryViewer activity)
              {
                  mactivity = activity;
              }
              public void OnPageChanged(int oldPosition, int newPosition)
              {
                  mactivity.seekbar.Progress = newPosition + 1;
                  mactivity.page_number.Text = (newPosition + 1).ToString() ;
                  mactivity.toolbar.Title = mactivity.adapter.PageList[newPosition].name;
              }
          }
  
          public class ExtraLayoutManager : LinearLayoutManager
          {
              private static readonly int DEFAULT_EXTRA_LAYOUT_SPACE = 800;
              private int extraLayoutSpace = -1;
              private Context context;
              
  
              public ExtraLayoutManager(Context context) : base(context)
              {
                  this.context = context;
              }
  
              public ExtraLayoutManager(Context context, int extraLayoutSpace) : base(context)
              {
                  this.context = context;
                  this.extraLayoutSpace = extraLayoutSpace;
              }
  
              
  
              public ExtraLayoutManager(Context context, int orientation, bool reverseLayout) 
                  : base(context,orientation,reverseLayout)
              {
                  this.context = context;
              }
  
              public void SetExtraLayoutSpace(int extraLayoutSpace)
              {
                  this.extraLayoutSpace = extraLayoutSpace;
              }
  
              protected override int GetExtraLayoutSpace(RecyclerView.State state)
              {
                  if (extraLayoutSpace > 0)
                  {
                      return extraLayoutSpace;
                  }
                  else
                  return DEFAULT_EXTRA_LAYOUT_SPACE;
              }
          }
  
          public List<string> GetPictureList(string imagefile)
          {
              List<string> imgList = new List<string>();
              string folder = Directory.GetParent(imagefile).FullName;
              var list = Directory.EnumerateFiles(folder, "*", SearchOption.TopDirectoryOnly);
              foreach(string file in list)
              {
                  if(file.Contains(".jpg")|| file.Contains(".png")|| file.Contains(".gif"))
                  {
                      imgList.Add(file);
                  }
              }
              return imgList;
          }
  
          public void InitPageGen()
          {
  
              Core.Gallery.Page[] pages = PageList.ToArray();
              int[] ids = new int[pages.Length];
              for (int i = 0; i < ids.Length; i++)
              {
                  ids[i] = pages[i].id;
              }
              Core.Gallery.InitiateImageGeneration(ids, "page","original");
              
          }
  
          public async void ToggleOverlay(bool toggle)
          {
              RunOnUiThread(() =>
              {
                  if (!toggle)
                  {
                      overlayVisible = false;
                      toolbar.Visibility = ViewStates.Gone;
                      seekbar.Visibility = ViewStates.Gone;
                  }
                  else
                  {
                      overlayVisible = true;
                      toolbar.Visibility = ViewStates.Visible;
                      seekbar.Visibility = ViewStates.Visible;
                      countDown.Start();
                      
                  }
              });
  
              
          }
  
          public void NextPage()
          {
              int pos = galleryPager.CurrentPosition;
              if (pos < galleryPager.ItemCount-1)
                  galleryPager.SmoothScrollToPosition(pos + 1);
  
          }
  
          public void PreviousPage()
          {
              int pos = galleryPager.CurrentPosition;
              if (pos > 0)
                  galleryPager.SmoothScrollToPosition(pos - 1);
          }
  
          /*1public override bool DispatchKeyEvent(KeyEvent e)
          {
             
                  int pos = galleryPager.CurrentPosition;
                  var keyCode = e.KeyCode;
                  if(e.Action == KeyEventActions.Up)
                  switch (keyCode)
                  {
                      case Keycode.VolumeUp:
                          if (e.Action == KeyEventActions.Up)
                          {
                              PreviousPage();
                          }
                          return true;
                      case Keycode.DpadLeft:
                          if (e.Action == KeyEventActions.Up)
                          {
                              PreviousPage();
                          }
                          return true;
                      //break;
                      case Keycode.VolumeDown:
                          if (e.Action == KeyEventActions.Up)
                          {
                              NextPage();
                          }
                          return true;
                      case Keycode.DpadRight:
  
gt:                         if (e.Action == KeyEventActions.Up)