Android.Support.V4.App.Fragment中的FindFragmentById

时间:2017-04-18 18:14:03

标签: android android-fragments

我使用了来自Joe Rock的2个导师来实施SlidingTabLayout 并在Xamarin中使用google maps

两者都很好,因为我按照教程,但我想要结合这些我得到错误。

我无法让FindFragmentById正常工作。

MainActivity

 public class MainActivity : FragmentActivity
{
    private ViewPager mViewPager;
    private SlidingTabScrollView mScrollView;

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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        mScrollView = FindViewById<SlidingTabScrollView>(Resource.Id.sliding_tabs);
        mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);

        mViewPager.Adapter = new SamplePagerAdapter(SupportFragmentManager);
        mScrollView.ViewPager = mViewPager;
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.actionbar_main, menu);
        return base.OnCreateOptionsMenu(menu);
    }
}

public class SamplePagerAdapter : FragmentPagerAdapter
{
    private List<Android.Support.V4.App.Fragment> mFragmentHolder;

    public SamplePagerAdapter(Android.Support.V4.App.FragmentManager fragManager) : base(fragManager)
    {
        mFragmentHolder = new List<Android.Support.V4.App.Fragment>();
        mFragmentHolder.Add(new GooglemapFragment());
        mFragmentHolder.Add(new ProfileFragment());
        mFragmentHolder.Add(new EventsFragment());
    }

    public override int Count
    {
        get { return mFragmentHolder.Count; }
    }

    public override Android.Support.V4.App.Fragment GetItem(int position)
    {
        return mFragmentHolder[position];
    }
}



public class ProfileFragment : Android.Support.V4.App.Fragment
{
    private EditText mTxt;
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.Frag2Layout, container, false);

        mTxt = view.FindViewById<EditText>(Resource.Id.editText1);
        mTxt.Text = "Fragment 2 Class :)";
        return view;
    }

    public override string ToString() //Called on line 156 in SlidingTabScrollView
    {
        return "Profile";
    }
}

public class EventsFragment : Android.Support.V4.App.Fragment
{
    private Button mButton;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.Frag3Layout, container, false);

        mButton = view.FindViewById<Button>(Resource.Id.button1);
        return view;
    }

    public override string ToString() //Called on line 156 in SlidingTabScrollView
    {
        return "Events";
    }
}

GoogleMapFragment

public class GooglemapFragment : Fragment,IOnMapReadyCallback
{
    private GoogleMap mMap;
    private MapView mView;
    private SupportMapFragment mFragment;
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
        View view = inflater.Inflate(Resource.Layout.GoogleMap,container,false);
       // mFragment = FragmentManager.FindFragmentByTag("map") as SupportMapFragment;
       // mFragment.GetMapAsync(this);

        FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(Activity); 
       // return view;

            return base.OnCreateView(inflater, container, savedInstanceState);
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        googleMap = mMap;
    }

我唯一调整的是我没有使用sepperate功能,但我在OnCreateView中输入了它。 有人可以帮助我,这很重要!

提前谢谢

0 个答案:

没有答案