gpt4 book ai didi

android - 在主要 Activity xamarin 的 fragment 中显示布局

转载 作者:行者123 更新时间:2023-11-30 01:08:29 25 4
gpt4 key购买 nike

我无法在类 SecondFragment 上显示布局: fragment

主要 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

最佳答案

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

确认并告诉我是否工作?

:)GlbMP

关于android - 在主要 Activity xamarin 的 fragment 中显示布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com