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

Android 在xamarin主活动的片段中显示布局,android,visual-studio,xamarin,Android,Visual Studio,Xamarin,我无法在类SecondFragment:Fragment上显示布局 主要活动 $ [Activity (Label = "project", Theme = "@style/Tab")] public class TabActivity : Activity { ProductDB dbHelper; ICursor cursor; protected override void O

我无法在类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
$[活动(Label=“project”,Theme=“@style/Tab”)]
公共课堂活动:活动
{
ProductDB-dbHelper;
ICursor光标;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.MainTab);
dbHelper=newproductdb(this);
cursor=dbHelper.ReadableDatabase.RawQuery(“从电影中选择*项”,null);
开始管理游标(游标);
this.ActionBar.NavigationMode=ActionBarNavigationMode.Tabs;
AddTab(“产品”,新的第一个片段(这个,游标));
AddTab(“用户配置文件”,新的SecondFragment());
AddTab(“用户订单”,新的ThirdFragment());
如果(savedInstanceState!=null)
this.ActionBar.SelectTab(this.ActionBar.GetTabAt(savedInstanceState.GetInt(“tab”));
}
void AddTab(字符串选项卡文本,片段视图)
{
var tab=this.ActionBar.NewTab();
tab.SetText(tabText);
tab.TabSelected+=委托(对象发送者,ActionBar.TabEventArgs ab)
{
var fragment=this.FragmentManager.findframentbyid(Resource.Id.frameLayout1);
if(片段!=null)
ab.FragmentTransaction.Remove(片段);
ab.FragmentTransaction.Add(Resource.Id.frameLayout1,view);};
tab.TabUnselected+=委托(对象发送者,ActionBar.TabEventArgs ab){
ab.FragmentTransaction.Remove(view);};
this.ActionBar.AddTab(tab);
}
受保护的覆盖无效OnDestroy()
{
停止管理游标(游标);
Close();
base.ondestory();
}
类FirstFragment:Fragment
{   
ICursor光标;
列表视图列表视图;
活动语境;
public FirstFragment(活动上下文、ICursor游标){
this.cursor=游标;
this.context=上下文;
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
base.OnCreateView(充气机、容器、保存状态);
var视图=充气机。充气(Resource.Layout.Tab1,容器,false);
listView=view.findviewbyd(Resource.Id.mylist);
Adapter=newproductadapter(上下文,游标);
listView.ItemClick+=OnItemListClick;
返回视图;
}
受保护的无效OnItemListClick(对象发送者,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();
}
}
类SecondFragment:Fragment
{
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图=充气机。充气(R.layout.your_碎片,容器,false);
返回视图;
}   
//为该碎片膨胀布局
//返回充气机。充气(右布局。物品视图,容器,假);
//base.OnCreateView(充气机、容器、保存状态);
//var视图=充气机。充气(Resource.Layout.User,container,false);
//var layout=view.findviewbyd(Resource.Id.linearLayoutmargin1);
//返回视图;
}
类ThirdFragment:Fragment

观察上述代码后,SecondFragment的onCreateView中可能存在一些问题,您遗漏了下面的行, base.OnCreateView(充气机、容器、保存状态)

确认并让我知道是否工作


:)GlbMP

$USER axml/android“android:orientation=“vertical”android:layout\u width=“match\u parent”android:layou height=“match\u parent”android:background=“#e2e2e2e2”>在axml文件中存在问题,它显示一切正常,代码工作正常,但当我给出其他一些axml文件ref时,它工作正常