I have use support_v7 Toolbar as ActionBar, but i getting problem to add tabs in my activity. Is there any example to add tab with toolbar. I took a look at http://blog.ostebaronen.dk/2013/11/getting-support-v7-working-with.html
Asked
Active
Viewed 4,402 times
0
-
http://stackoverflow.com/questions/26540078/use-tab-with-new-toolbar-appcompat-v7-21/26543020#26543020 – Gabriele Mariotti Jun 10 '15 at 20:29
-
@GabrieleMariotti I using xamarin and there is SlidingTabLayout.java file. So i confuse how to do add tab in my app. Is there any reference or example of mvvmcross droid ? – Uddhao Pachrne Jun 11 '15 at 12:31
1 Answers
2
I works just the same as normal Android apps. Use the layouts that Gabriele Mariotti's link has. To make the viewpager work with bindings in MvvmCross you need to use something like this: https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/Cirrious.MvvmCross.Droid.Support.V4/MvxFragmentStatePagerAdapter.cs
For better readability: https://gist.github.com/martijn00/23b00172af9e2c798f3d
Here is some code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
app:tabGravity="center"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
public class SomeFragment : MvxFragment<SomeViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
var viewPager = view.FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
var fragments = new List<MvxViewPagerFragmentAdapter.FragmentInfo>
{
new MvxViewPagerFragmentAdapter.FragmentInfo
{
FragmentType = typeof(SomeFragment1),
Title = "",
ViewModel = ViewModel.ViewModelSomething1
},
new MvxViewPagerFragmentAdapter.FragmentInfo
{
FragmentType = typeof(SomeFragment2),
Title = ViewModel.ViewModelPopular.TextSource.GetText("Title"),
ViewModel = ViewModel.ViewModelSomething2
},
new MvxViewPagerFragmentAdapter.FragmentInfo
{
FragmentType = typeof(SomeFragment3),
Title = ViewModel.ViewModelContributors.TextSource.GetText("Title"),
ViewModel = ViewModel.ViewModelSomething3
}
};
viewPager.Adapter = new MvxViewPagerFragmentAdapter(Activity, Activity.SupportFragmentManager, fragments);
}
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
return view;
}
}
public class MvxFragmentStatePagerAdapter
: FragmentStatePagerAdapter
{
private readonly Context _context;
public IEnumerable<FragmentInfo> Fragments { get; private set; }
public override int Count
{
get { return Fragments.Count(); }
}
protected MvxFragmentStatePagerAdapter(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public MvxFragmentStatePagerAdapter(
Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments)
: base(fragmentManager)
{
_context = context;
Fragments = fragments;
}
public override Fragment GetItem(int position)
{
var fragmentInfo = Fragments.ElementAt(position);
var fragment = Fragment.Instantiate(_context,
FragmentJavaName(fragmentInfo.FragmentType));
((MvxFragment)fragment).ViewModel = fragmentInfo.ViewModel;
return fragment;
}
protected static string FragmentJavaName(Type fragmentType)
{
var namespaceText = fragmentType.Namespace ?? "";
if (namespaceText.Length > 0)
namespaceText = namespaceText.ToLowerInvariant() + ".";
return namespaceText + fragmentType.Name;
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(Fragments.ElementAt(position).Title);
}
public class FragmentInfo
{
public string Title { get; set; }
public Type FragmentType { get; set; }
public IMvxViewModel ViewModel { get; set; }
}
}

Martijn00
- 3,569
- 3
- 22
- 39
-
thank you for response. Does it support for backward compatibility(API 15 or upper level)? – Uddhao Pachrne Jun 16 '15 at 14:02
-
Yes. this supports everything that is supported in the support library. – Martijn00 Jun 16 '15 at 14:09
-
Thanks for the answer @Martijn00 ! Could i get a look at your View+ ViewModel for "SomeFragment2". I keep seeing this exception "Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public" – AhmedW Jul 20 '15 at 11:53
-
Yes: the full example is available here: https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/Samples/Example.Droid/Fragments/ExamplesFragment.cs – Martijn00 Jul 20 '15 at 11:56
-
@Martijn00 Hi martin, the code link that you gave here doesnt work anymore. I've tried looking myself but couldnt find it..If there's a new link for this, can you possibly give it here? Will be very gratful..Thanks ! – shahbaz Oct 28 '15 at 11:33
-
1All the fragment examples are here: https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples/Example.Droid/Fragments – Martijn00 Oct 28 '15 at 11:34
-
-
@Martijn00 Is there a reason why you use a main fragment that holds the tablayout etc, instead of a Activity? Generally the fragments are shown on a Activity not a Fragment. Thanks – cfl Jan 17 '17 at 09:15
-
The sample off course uses an `Activity` to show the fragment. Using a fragment to hold the viewpager is just a matter of how you develop your app. – Martijn00 Jan 17 '17 at 10:40
-
-
1Samples are now here: https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects – Martijn00 Mar 22 '17 at 15:32