arshisaini
arshisaini

Reputation: 81

Display layout in fragment on main activity xamarin

I am not able to display layout on class SecondFragment : Fragment

MAIN ACTIVITY

$     [Activity (Label = "project", Theme = "@style/Tab")]
        public class TabActivity : Activity
        {
            ProductDB dbHelper;
            ICursor cursor;

            protected override void OnCreate (Bundle savedInstanceState)
            {
                base.OnCreate (savedInstanceState);

                // Set our view from the "main" layout resource
                SetContentView (Resource.Layout.MainTab);

                dbHelper = new ProductDB(this);

                cursor = dbHelper.ReadableDatabase.RawQuery ("select * from movie", null);
                StartManagingCursor (cursor);

                this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

                AddTab ("Products", new FirstFragment(this, cursor));
                AddTab ("User Profile", new SecondFragment());
                AddTab("User Order", new ThirdFragment());

                if (savedInstanceState != null)
                    this.ActionBar.SelectTab(this.ActionBar.GetTabAt(savedInstanceState.GetInt("tab")));
            }

            void AddTab (string tabText, Fragment view)
            {
                var tab = this.ActionBar.NewTab ();            
                tab.SetText (tabText);
                tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs ab)
                {
                    var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
                    if (fragment != null)
                    ab.FragmentTransaction.Remove(fragment);         
                    ab.FragmentTransaction.Add (Resource.Id.frameLayout1, view); };
                    tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs ab) {
                    ab.FragmentTransaction.Remove(view); };
                    this.ActionBar.AddTab (tab);
           }
        protected override void OnDestroy ()
            {
                StopManagingCursor (cursor);
                cursor.Close ();
                base.OnDestroy ();
            }

            class FirstFragment: Fragment
            {   
                ICursor cursor;
                ListView listView;
                Activity context;

                public FirstFragment(Activity context, ICursor cursor) {
                    this.cursor = cursor;
                    this.context = context;
                }

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

                    var view = inflater.Inflate (Resource.Layout.Tab1, container, false);
                    listView = view.FindViewById<ListView> (Resource.Id.mylist);
                    listView.Adapter = new ProductAdapter (context, cursor);
                    listView.ItemClick += OnItemListClick;

                    return view;
                }

                protected void OnItemListClick (object sender, AdapterView.ItemClickEventArgs ab)
                {
                    var curs = (ICursor)listView.Adapter.GetItem (ab.Position);
                    var movieName = curs.GetString (1);
                    Android.Widget.Toast.MakeText (context, movieName, Android.Widget.ToastLength.Short).Show();
                }
            }

            class SecondFragment : Fragment
            {
               public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    View view = inflater.Inflate(R.layout.your_fragment, container, false);

                    return view;
                }   

                    // Inflate the layout for this fragment
                    // return inflater.inflate(R.layout.article_view, container, false);
                    // base.OnCreateView(inflater, container, savedInstanceState);
                    // var view = inflater.Inflate(Resource.Layout.User, container, false);
                    // var layout = view.FindViewById<LinearLayout>(Resource.Id.linearLayoutmargin1);
                    // return view;
                }


        class ThirdFragment : Fragment

Upvotes: 1

Views: 525

Answers (1)

Manoj Kumar Pandit
Manoj Kumar Pandit

Reputation: 43

After observing above code, there might be some issue in onCreateView of SecondFragment, you missed below line, base.OnCreateView (inflater, container, savedInstanceState);

Confirm and let me know whether working or not?

:)GlbMP

Upvotes: 1

Related Questions