How to Launch a new Fragment from a Fragment with a ListView via an ItemClick event?

Hello everyone,

I've run into a dilemma and I don't seem to be able to find the resources in order to resolve my issue and was hoping I could find some help here.

My app currently has a single Activity which has an ActionBar that inserts/removes fragments into a container. That works peachy. The issue I'm running into is that I have a ListView in two of those Fragments that get inserted via the ActionBar. When one of the items in those ListViews is selected I want to be able to replace the current fragment with a new one to display more information about the item selected. How do I go about doing this? I don't seem to be able to initiate a FragmentTransaction the way I was able to in the original Activity.

Here is the code for Fragment in which contains the ListView that I want to be replaced by another fragment once an item on that ListView is clicked

public class EventListFragment : Fragment
 {
    ListView _eventListView;
    EventAdapter _eventAdapter;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        System.Diagnostics.Trace.WriteLine("EventListFragment OnCreateView - Got Here!");
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = inflater.Inflate(Resource.Layout.EventList, container, false);
        _eventListView = view.FindViewById<ListView>(Resource.Id.eventListView);
        _eventAdapter = new EventAdapter(this.Activity);
        _eventListView.Adapter = _eventAdapter;
        _eventListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
        {
            Event thisEvent = _eventAdapter.GetEventByPosition(e.Position);
            Toast.MakeText(this.Activity, thisEvent.TagLine, ToastLength.Short);
        // Here is where I want to create the new Fragment and dispose of the current one, or place in the background for use of a back button later
        };
        return view;
    }

    public override void OnStart()
    {
        base.OnStart();
        _eventAdapter.RefreshEvents();
    }

}

Best Answer

Answers

  • Ahhh @rzee7, Your hint was indeed most helpful!!! EventHandlers, d'oh! I shall begin work on this shortly :smile:

  • rzee7rzee7 INUniversity ✭✭✭✭✭

    Great!! @foosyePhillipWolf

    All the best :smile:

Sign In or Register to comment.