Prescott Chartier
Prescott Chartier

Reputation: 1685

Close Android ViewPager from the adapter

I have a view pager set up and running exactly how I want it to. Once the user is done swiping through the images and data, I need to close/exit the view/viewpager. I put a button on the View that is contained in the viewpager and when I access (click it) it executes the code in the adapter, but I can find no method to close the view/viewpager. My Adapter code:

public class ImageSliderAdapter : PagerAdapter
{
    Context _context;
    DataSet _MyImages = new DataSet();
    public ImageSliderAdapter(Context context, DataSet MyImages)
    {
        _MyImages = MyImages;
        _context = context;
    }

    public override bool IsViewFromObject(Android.Views.View view, Java.Lang.Object @object)
    {
        return view == ((LinearLayout)@object);
    }

    public override int Count
    {
        get
        {
            return _MyImages.Tables[0].Rows.Count;
        }
    }

    public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object objectValue)
    {
    }
    public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
    {
        View view = container;
        var inflater = _context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
        view = inflater.Inflate(Resource.Layout.image_slider_item, null);
        var child = view.FindViewById<ImageView>(Resource.Id.image_slider_item);
        child.SetScaleType(ImageView.ScaleType.CenterCrop);
        TextView Name = view.FindViewById<TextView>(Resource.Id.Name);
        TextView CellPhone = view.FindViewById<TextView>(Resource.Id.CellPhone);
        TextView HomePhone = view.FindViewById<TextView>(Resource.Id.HomePhone);
        Button MyExit = view.FindViewById<Button>(Resource.Id.button_Exit);
        MyExit.Click += (o, e) =>
        {
            // need to exit here/
        };
        Bitmap image = null;
        string Name = "";
        string HomePhoneNumber = "";
        string CellPhoneNumber = "";
        Task.Run(() =>
        {
            //URL url = new URL(_imageUrls[position]);
            byte[] MyPhoto = (byte[])_MyImages.Tables[0].Rows[position]["photo"];
            image = BitmapFactory.DecodeByteArray(MyPhoto, 0, MyPhoto.Length); //(Bitmap)_MyImages.Tables[0].Rows[position]["Photo"]; //BitmapFactory.DecodeStream(url.OpenConnection().InputStream);
            Name = _MyImages.Tables[0].Rows[position]["name"].ToString();
            HomePhoneNumber = _MyImages.Tables[0].Rows[position]["HomePhoneNumber"].ToString();
            CellPhoneNumber = _MyImages.Tables[0].Rows[position]["CellPhoneNumber"].ToString();
        }).ContinueWith(t =>
        {
            (_context as Swipe).RunOnUiThread(() =>
            {
                child.SetImageBitmap(image);
                Name.Text = Name;
                Home.Text = HomePhoneNumber;
                Away.Text = CellPhoneNumber;
            });
        });
        container.AddView(view);
        return view;
    }
}

How would I close the view/viewpager and return to the last view which is the parent?

Upvotes: 0

Views: 49

Answers (2)

Tu&#226;n
Tu&#226;n

Reputation: 11

The action to close view pager is handled by the parent activity or fragment that contains the ViewPager. If you are using it inside an activity, use finish(); and if it's inside a fragment, call requireActivity().getSupportFragmentManager().popBackStack();.

Upvotes: 0

Prescott Chartier
Prescott Chartier

Reputation: 1685

I couldn't find any method to "close" or "exit" the ViewPager and return to the parent view. It occurred to me that I could just go to the activity rather than try to close or exit. Putting the following code in the MyExit.Click function does what I need to do:

First I added two properties to my adapter class, this was required because I needed to rebuild some objects when I get to the called activity and I need the logged on username and password for that. I set the properties when I instantiate the adapter:

public string Username = "";
public string Password = "";


MyExit.Click += (o, e) =>
{
    Intent intent = new Intent(_context, typeof(ContactInfo));
    Bundle bundlee = new Bundle();
    intent.PutExtra("Username",Username);
    intent.PutExtra("Password", Password);
    intent.PutExtra("TheBundle", bundlee);
    intent.SetFlags(ActivityFlags.NewTask);
    _context.StartActivity(intent);
};

Works for me.

Upvotes: 0

Related Questions