androidxamarin.androidandroid-viewpagerandroidimageslider

ImageSlider using ViewPager not showing image which gets removed in destroyitem


I am trying to make a mix image/video slider using viewpager, I have made an adapter for that and in instantiateItem overridden by pagerAdapter I assign a view to imageView or videoView.

below is the code of that

    public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
    {
        View view = new View(_context);
        var plItem = this._viewSources[position];

        try
        {
            if (!plItem.Contains("mp4"))
            {
                view = new ImageView(_context);
                view.Tag = position;
                ((ImageView)view).SetImageURI(Uri.Parse(plItem));
                ((ImageView)view).SetScaleType(ImageView.ScaleType.FitXy);
            }
            else
            {
                view = new VideoView(_context);
                view.Tag = position;

                RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);

                ((VideoView)view).LayoutParameters = layoutparams;
                ((VideoView)view).SetVideoPath(plItem);
                ((VideoView)view).Completion += View_Completion;
                ((VideoView)view).Start();
            }

            ((ViewPager)container).AddView(view, 0);
        }
        catch (Exception ex)
        {

        }

        return view;

        //((Activity)_context).RunOnUiThread(() =>
        //{
        //    container.AddView(view, 0);
        //});
        //return view;
    }

and I have also overridden destroyItem in which I delete the view from container

    public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
    {
        try
        {
            ((ViewPager)container).RemoveView((View)@object);
            //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        }
        catch (Exception ex)
        {
        }
    }

i am calling my viewpager in another custom control using this code

        _viewPager = new ViewPager(_context);
        _viewPager.OffscreenPageLimit = 2;
        _viewPager.SetBackgroundColor(Android.Graphics.Color.Transparent);
        _viewPager.Adapter = new ImageVideoSliderAdapter(_context, _viewPager);
        _viewPager.AddOnPageChangeListener((IOnPageChangeListener)_viewPager.Adapter);

The main problem I am facing is when an item is removed from the container it shows an empty imageview in that iteration with no image


Solution

  • The problem was on destroy item it was throwing exception that only I must delete view in ui thread that's why it was not getting properly deleted.

                ((Activity)_context).RunOnUiThread(() =>
                {
                    ((ViewPager)container).RemoveView((View)@object);
                });
    

    after doing this the problem was fixed.