Search code examples
c#visual-studioxamarinandroid-activityfragment

C# Xamarin Fragment won't launch


I'm struggling with a problem!

I created a Fragment to fill a page with text, so here is the full OnCreateView:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.Inflate(Resource.Layout.ProductInfo, container, false);

    List<products> SelectedCoinList = (from productname in coinList
                                       where productname.ProductName.Contains(SelectedProductName, StringComparison.OrdinalIgnoreCase)
                                       select productname).ToList<products>();

    ImageView ProductLogo = view.FindViewById<ImageView>(Resource.Id.ProductInfoLogo);

    var imageBitmap = GetImageBitmapFromUrl("http://******/thumbnails/" + SelectedProductList[position].Symbol + ".png");
    ProductLogo.SetImageBitmap(imageBitmap);

    TextView txtProductInfoName = view.FindViewById<TextView>(Resource.Id.txtProductInfoName);
    txtProductInfoName.Text = SelectedProductList[position].FullName;

    TextView txtProductInfoPrice = view.FindViewById<TextView>(Resource.Id.txtProductInfoPrice);
    txtProductInfoPrice.Text = "€ " + SelectedProductList[position].Price;

    return view;
}

And in the MainActivity.cs I have this code that (is supposed to) start the Fragment:

public void ListViewProducts_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {
    //Give variables the right value to share with other classes
    ProductName = productsList[e.Position].ProductNameName;
    position = e.Position;

    // Set view to Info 
    SetContentView(Resource.Layout.ProductInfo);
}

public string GetProductName() {
    return ProductName; //return your mainActivity list
}

The problem is that the Fragment doesn't start or something, because it loads the ProductInfo page, but with the default text. I figured this out by placing breakpoints in the OnCreateView, but they weren't used.

Any suggestions?


Solution

  • you need to create an instance of your fragment and insert it into a view in your Activity layout. for example you can use a FrameLayout. in activities layout add a frame layout

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    use the attribute android:visibility="gone"

    if you want the frameLayout to be invisible and take up no space when your activity starts.

    then in your activity have a method like so

    private YourFragment _fragment;
    private void DisplayFragment()
    {
        var fragmentContainer = FindViewById<FrameLayout> 
           (Resource.Id.fragmentContainer);
        _fragment= new YourFragment();
        var fragmentManager = FragmentManager.BeginTransaction();
        fragmentManager.Add(Resource.Id.fragmentContainer, _fragment);
        fragmentManager.Commit();
        fragmentContainer .Visibility = ViewStates.Visible; //Makes your fragment container visible if you set visibility=gone"
    
    }