在主要活动xamarin上显示片段中的布局

时间:2016-08-01 03:43:17

标签: android visual-studio xamarin

我无法在类SecondFragment:Fragment

上显示布局

主要活动

$     [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

1 个答案:

答案 0 :(得分:1)

观察上面的代码后,在SecondFragment的onCreateView上可能会出现一些问题,你错过了下面的行, base.OnCreateView(inflater,container,savedInstanceState);

确认并告诉我是否工作?

:) GlbMP