xamarin-android片段重叠

xamarin-android片段重叠,android,actionbarsherlock,xamarin.android,android-fragmentactivity,Android,Actionbarsherlock,Xamarin.android,Android Fragmentactivity,我知道已经有很多问题在讨论这个问题了,但我仍然无法找出我的实现有什么问题 我将SherlockFragmentActivity与ActionBar一起使用,它有4个选项卡,每个选项卡用不同的片段填充框架布局 到目前为止,我没有问题,标签在片段之间切换非常完美 但是在“购买”选项卡上,当单击searchButton时,我使用Activity1 NavigateTo方法将框架布局替换为另一个ListFragment,这会导致当前片段重叠,并且无论我从现在开始做什么都不会消失 请告知,我已经阅读了所有

我知道已经有很多问题在讨论这个问题了,但我仍然无法找出我的实现有什么问题

我将SherlockFragmentActivity与ActionBar一起使用,它有4个选项卡,每个选项卡用不同的片段填充框架布局

到目前为止,我没有问题,标签在片段之间切换非常完美

但是在“购买”选项卡上,当单击searchButton时,我使用Activity1 NavigateTo方法将框架布局替换为另一个ListFragment,这会导致当前片段重叠,并且无论我从现在开始做什么都不会消失

请告知,我已经阅读了所有的解决方案,但找不到问题,如果需要代码的另一部分,请告诉我

这是我的密码: 活动1:

public class Activity1 : SherlockFragmentActivity, ActionBar_Sherlock.App.ActionBar.ITabListener
    {
protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            userid = 2;

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            SupportActionBar.NavigationMode = (int)ActionBarNavigationMode.Tabs;
            SupportActionBar.SetDisplayShowTitleEnabled(true);
            //Set the mobile service Sale table adapter
            //this.adapter = new SalesAdapter(this.MobileServiceContext.SaleTable, Resource.Layout.OffersListItem, this);

            //Initialize tabs
            Tab tab = SupportActionBar.NewTab();
            tab.SetTag("MyProfile");
            tab.SetText("My Profile");
            tab.SetTabListener(this);
            SupportActionBar.AddTab(tab);

            tab = SupportActionBar.NewTab();
            tab.SetTag("Buy");
            tab.SetText("Buy");
            tab.SetTabListener(this);
            SupportActionBar.AddTab(tab);

            tab = SupportActionBar.NewTab();
            tab.SetTag("Sell");
            tab.SetText("Sell");
            tab.SetTabListener(this);
            SupportActionBar.AddTab(tab);

            tab = SupportActionBar.NewTab();
            tab.SetTag("Rates");
            tab.SetText("Rates");
            tab.SetTabListener(this);
            SupportActionBar.AddTab(tab);

        }

        public void OnTabSelected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        {
            string tag = tab.Tag.ToString();

            Android.Support.V4.App.Fragment f = SupportFragmentManager.FindFragmentByTag(tag);
            if (f != null)
            {
                ft.Show(f);
                return;
            }

            if (tag == "MyProfile")
                f = new Fragments.MyProfileFragment();
            else if (tag == "Buy")
                f = new Fragments.BuyFragment();
            else if (tag == "Sell")
                f = new Fragments.SaleFragment();
            else if (tag == "Rates")
                f = new Fragments.BuyFragment();



            ft.Add(Resource.Id.fragmentContainer, f, tag);
        }


        public void OnTabUnselected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        {
            string tag = tab.Tag.ToString();

            Android.Support.V4.App.Fragment f = SupportFragmentManager.FindFragmentByTag(tag);
            if (f != null)
            {
                ft.Hide(f);
                return;
            }
        }

        public void OnTabReselected(Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        {
            //Do nothing
        }

        public void NavigateTo(Android.Support.V4.App.Fragment newFragment)
        {
            Android.Support.V4.App.FragmentManager manager = SupportFragmentManager;
            Android.Support.V4.App.FragmentTransaction ft = manager.BeginTransaction();

            ft.Replace(Resource.Id.fragmentContainer, newFragment);
            ft.SetTransition((int)FragmentTransit.FragmentFade);
            // Add this trnasaction to the back stack, so when the user press back,
            // it rollbacks.
            ft.AddToBackStack(null);
            ft.Commit();
        }

        public override void OnBackPressed()
        {
            Android.Support.V4.App.FragmentManager manager = SupportFragmentManager;
            if (manager.BackStackEntryCount > 0)
            {
                base.OnBackPressed();
            }
            else
            {

            }
        }
}
这是主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
      android:id="@+id/fragmentContainer"
      android:layout_width="match_parent"
      android:layout_height="0dip"
      android:layout_weight="1" />
</LinearLayout>

这是BuyFragment:

public class BuyFragment : Android.Support.V4.App.Fragment
    {
        private Activity1 myActivity;
        private string currency;
        private int amount;
        private EditText buyAmount;
        private Button searchButton;
        private Spinner currencySpinner;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            myActivity = (Activity1)this.Activity;
        }

        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            //Init the currency spinner
            currencySpinner = this.Activity.FindViewById<Spinner>(Resource.Id.spinnerBuy);
            myActivity.InitCurrencySpinner(currencySpinner);

            //Parse the amount text to int
            buyAmount = this.Activity.FindViewById<EditText>(Resource.Id.buyEdittext);
            buyAmount.AfterTextChanged += (sender, args) =>
            {
                //
                int result;
                int.TryParse(buyAmount.Text, out result);

                amount = result;
            };

            searchButton = this.Activity.FindViewById<Button>(Resource.Id.buyButton);
            searchButton.Click += (sender, e) =>
            {
                OnClickSearch(sender, e);
            };

            //Disable all buttons if the adapter is updating
            myActivity.SalesAdapter.IsUpdatingChanged += (s, e) =>
            {
                this.buyAmount.Enabled =
                this.currencySpinner.Enabled =
                this.searchButton.Enabled =
                    !myActivity.SalesAdapter.IsUpdating;
            };
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
        {
            return inflater.Inflate(Resource.Layout.BuyLayout, container, false);
        }

        private void OnClickSearch(object sender, EventArgs eventArgs)
        {
            try
            {
                if (amount > 0)
                {
                    currency = currencySpinner.SelectedItem.ToString();
                    SearchOffers();
                    this.buyAmount.Text = null;
                }
                else
                {
                    Toast.MakeText(this.Activity, "Amount must be a number larger than 0", ToastLength.Short).Show();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("SaleFragment.OnClickPutForSale: {0} Exception caught.", e.InnerException);
            }
        }

        private void SearchOffers()
        {
            //Check what fragment is shown, replace if needed.
            var fragmentContainer = FragmentManager.FindFragmentById(Resource.Id.fragmentContainer) as SearchListFragment;
            if (fragmentContainer == null)
            {
                // Make new fragment to show this selection.
                fragmentContainer = SearchListFragment.NewInstance(amount, currency);

                // Execute a transaction, replacing any existing
                // fragment with this one inside the frame.
                myActivity.NavigateTo(fragmentContainer);
            }
            //else
            //{
            //    fragmentContainer.Arguments.PutString("currency", currency);
            //    fragmentContainer.Arguments.PutInt("amount", amount);
            //    myActivity.NavigateTo(fragmentContainer);
            //}
        }
公共类BuyFragment:Android.Support.V4.App.Fragment
{
私人活动1我的活动;
私人字符串货币;
私人整数金额;
私人编辑文本购买量;
私人按钮搜索按钮;
私人微调器电流微调器;
创建时公共覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
myActivity=(Activity1)this.Activity;
}
已创建ActivityState(Bundle savedInstanceState)上的公共覆盖无效
{
创建的基本活动(savedInstanceState);
//初始化货币微调器
currencySpinner=this.Activity.FindViewById(Resource.Id.spinnerBuy);
myActivity.InitCurrencySpinner(currencySpinner);
//将金额文本解析为int
buyAmount=this.Activity.FindViewById(Resource.Id.buyEdittext);
buyAmount.AfterTextChanged+=(发件人,参数)=>
{
//
int结果;
int.TryParse(buyAmount.Text,输出结果);
金额=结果;
};
searchButton=this.Activity.FindViewById(Resource.Id.buyButton);
搜索按钮。单击+=(发件人,e)=>
{
OnClickSearch(发送者,e);
};
//如果适配器正在更新,请禁用所有按钮
myActivity.SalesAdapter.IsUpdatengChanged+=(s,e)=>
{
此.buyAmount.Enabled已启用=
此.currencySpinner.Enabled已启用=
此.searchButton.Enabled已启用=
!myActivity.SalesAdapter.IsUpdate;
};
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包)
{
返回充气机。充气(Resource.Layout.BuyLayout,container,false);
}
私有void OnClickSearch(对象发送方,EventArgs EventArgs)
{
尝试
{
如果(金额>0)
{
currency=currencySpinner.SelectedItem.ToString();
搜索报价();
this.buyamum.Text=null;
}
其他的
{
Toast.MakeText(this.Activity,“数量必须大于0”,ToastLength.Short).Show();
}
}
捕获(例外e)
{
WriteLine(“SaleFragment.OnClickPutForSale:{0}捕获异常。”,e.InnerException);
}
}
私人无效搜索报价()
{
//检查显示的碎片,必要时更换。
var fragmentContainer=FragmentManager.FindFragmentById(Resource.Id.fragmentContainer)作为SearchListFragment;
if(fragmentContainer==null)
{
//创建新片段以显示此选择。
fragmentContainer=SearchListFragment.NewInstance(金额、货币);
//执行事务,替换任何现有的
//框架内的碎片。
myActivity.NavigateTo(碎片容器);
}
//否则
//{
//fragmentContainer.Arguments.PutString(“货币”,货币);
//fragmentContainer.Arguments.PutInt(“金额”,amount);
//myActivity.NavigateTo(碎片容器);
//}
}
购买布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/currency_prompt"
        android:layout_marginBottom="10dp" />
    <Spinner
        android:id="@+id/spinnerBuy"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/currency_prompt"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10.0dp" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/amount_prompt"
        android:layout_marginBottom="10dp" />
    <EditText
        android:id="@+id/buyEdittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:inputType="number" />
    <Button
        android:text="@string/BuyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buyButton"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp" />
</LinearLayout>

更换:

ft.Add(Resource.Id.fragmentContainer, f, tag);
与:


我和你一样有一个类似的问题。你使用哪个版本的ActionBarSherlock?@balint我不知道,如何检查?@balint我使用的是最新的4.3.1version@NirSchachter你找到解决办法了吗?我遇到了同样的问题
ft.Replace(Resource.Id.fragmentContainer, f, tag);