简体   繁体   中英

Xamarin - Android Tablayout in each item of Recycler view

I'm Trying to create a Recycler view whose each item will contain a Tab layout with View pager. The Problem I'm facing right now is only first item contains Tab layout rest of items are empty. This is what I'm trying to create

Below are the codes for Layouts, service and Adapter.

Tablayout.xml

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:foregroundGravity="center"
        local:tabGravity="center"
        android:layout_gravity="center_vertical"
        android:paddingVertical = "22dp"
        local:tabPaddingBottom = "0dp"
        local:tabIndicatorFullWidth="true"
        local:tabIndicatorGravity="bottom"
        local:tabTextAppearance="@style/AppTabTextAppearance"
        local:tabMode="fixed"
        local:tabIndicatorHeight="2dp"
        style="@style/CustomTabLayout"
        android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
        android:background="@android:color/transparent"
        android:id="@+id/pager"
        android:layout_below = "@id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

In Recycler view Tablayout will pass as a mvxItemTemplate

RecyclerView.xml

  <MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
     card_view:MvxItemTemplate="@layout/Tablayout
     card_view:MvxBind="ItemsSource Routes"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/recycler_view"
    android:scrollbars="vertical"        
    />

This Service will be call for each item of recycler view and initializes view pager and Tablayout

TablayoutService.cs

public class TablayoutService
{ 
  public void ShowReadOutExpandedSection() 
    {
        var top = Mvx.IoCProvider.Resolve<IMvxAndroidCurrentTopActivity>();
        var act = top.Activity as MvxAppCompatActivity;
        ViewPager viewPager = act.FindViewById<ViewPager>(Resource.Id.pager);
        viewPager.Adapter = new TabViewPagerAdapter(act.SupportFragmentManager) ;
        var tabLayout = act.FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewPager);
    }
 }

This Adpater will attach 3 fragments with tabtiles

TabViewPagerAdapter.cs

public class ExpandedTabViewPagerAdapter : MvxCachingFragmentPagerAdapter
{
    const int PAGE_COUNT = 3;
    // Tab Titles
    private string[] tabtitles = Android.App.Application.Context.Resources.GetStringArray(Resource.Array.Top_Tab_Elements);

    public ExpandedTabViewPagerAdapter(FragmentManager fm) : base(fm) { }
    public override int Count
    {
        get { return PAGE_COUNT; }
    }
    public override Java.Lang.ICharSequence GetPageTitleFormatted(int position)
    {
        return new Java.Lang.String(tabtitles[position]);
    }

    public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
    {
        switch (position)
        {
            case 0:
                ExpandedEstateFragment fragmenttab1 = (ExpandedEstateFragment)Activator.CreateInstance(typeof(ExpandedEstateFragment));
                var viewModelLoader = Mvx.IoCProvider;
                fragmenttab1.ViewModel = viewModelLoader.Resolve<IMvxViewModelLoader>().LoadViewModel(MvxViewModelRequest.GetDefaultRequest(typeof(ExpandedEstateViewModel)), null, null);
                return fragmenttab1;
            case 1:
                ExpandedEstateFragment fragmenttab2 = (ExpandedEstateFragment)Activator.CreateInstance(typeof(ExpandedEstateFragment));
                viewModelLoader = Mvx.IoCProvider;
                fragmenttab2.ViewModel = viewModelLoader.Resolve<IMvxViewModelLoader>().LoadViewModel(MvxViewModelRequest.GetDefaultRequest(typeof(ExpandedEstateViewModel)), null, null);
                return fragmenttab2;
            case 2:
                ExpandedEstateFragment fragmenttab3 = (ExpandedEstateFragment)Activator.CreateInstance(typeof(ExpandedEstateFragment));
                viewModelLoader = Mvx.IoCProvider;
                fragmenttab3.ViewModel = viewModelLoader.Resolve<IMvxViewModelLoader>().LoadViewModel(MvxViewModelRequest.GetDefaultRequest(typeof(ExpandedEstateViewModel)), null, null);
                return fragmenttab3;
        }
        return null;
    }

}

I think the problem comes from the fact that, even if the ShowReadOutExpandedSection() is called for every cell, in it you statically referent to the same instance of recyclerView and TableLayout by calling out to your activity's findById method.

To achieve what you want you have to write your own Recycler Adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM