Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

RecyclerView in Fragment - PagerSlidingTabStrip menu

$
0
0

I guys,

I use PagerSlidingTabStrip for create a menu with two tab. And for my seconde tab, I want to create in a RecyclerView.

Nothing appeared in my layout. i don't know if is a probleme with my xml file, or my layoutManager declaration.
If you have any idea, or any advices ! I should re-create a new architecture for my code ?

This is my code :

MainActivity.cs

public class MainActivity : BaseActivity
{
    protected override int LayoutResource
    {
        get
        {
            return Resource.Layout.Main;
        }
    }

    private Android.Widget.Toolbar _topMenu;

    private Adapter _adapter;
    private ViewPager _pager;
    private PagerSlidingTabStrip _tabs;

    private RecyclerView _recyclerView;
    private LayoutManager _layoutManager;

    public string _tag = "MainActivity";

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

        _recyclerView = FindViewById<RecyclerView (Resource.Id.recyclerView);
        _recyclerView.HasFixedSize = true;

        _layoutManager = new LayoutManager(this, _recyclerView);
        _layoutManager.createLayoutManager();

        _layoutManager.addItem("one", "two", "three");
        _layoutManager.addItem("one", "two", "three");
        _layoutManager.addItem("one", "two", "three");

        _adapter = new Adapter(SupportFragmentManager);
        _pager = FindViewById<ViewPager>(Resource.Id.pager);
        _tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);

        _pager.Adapter = _adapter;
        _tabs.SetViewPager(_pager);
        _topMenu = FindViewById<Android.Widget.Toolbar>(Resource.Id.top_menu);
        SetActionBar(_topMenu);
    }

BaseActivity.cs

public abstract class BaseActivity : AppCompatActivity
    {
        public Toolbar Toolbar
        {
            get;
            set;
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(LayoutResource);
        }

        protected abstract int LayoutResource
        {
            get;
        }
    }

Main.xml

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true" >

      <include
          android:id="@+id/top_menu"
          layout="@layout/top_menu" />

      <com.refractored.PagerSlidingTabStrip
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"

          app:pstsShouldExpand="true"
          android:background="@color/Blue"

          app:pstsDividerWidth="1dp"
          app:pstsDividerPadding="12dp"
          app:pstsDividerColor="#50FFFFFF"

          android:textColor="@color/Green"
          app:pstsTextColorSelected="@color/White"

          app:pstsIndicatorColor="@color/Red"
          app:pstsUnderlineColor="@color/White" />

          <android.support.v4.view.ViewPager
              android:id="@+id/pager"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />

          <include
            layout="@layout/recyclerView" />

    </LinearLayout>

LayoutManager.cs

class LayoutManager : Activity
{
    public RecyclerView _mRecyclerView;
    public RecyclerView.LayoutManager _mLayoutManager;
    public RecyclerView.Adapter _mAdapter;
    public ItemList<Item> _mItems;
    public Activity _main;

    public LayoutManager(Activity main, RecyclerView recyclerView)
    {
        _main = main;
        _mRecyclerView = recyclerView;
        _mItems = new ItemList<Item>();
    }

    public void createLayoutManager()
    {
        _mLayoutManager = new LinearLayoutManager(_main);
        _mRecyclerView.SetLayoutManager(_mLayoutManager);
        _mAdapter = new RecyclerAdapter(_mItems, _mRecyclerView);
        _mItems.Adapter = _mAdapter;
        _mRecyclerView.SetAdapter(_mAdapter);
    }
    public void addItem(string name, string brand, string description)
    {
        _mItems.Add(new Item()
        {
            Img = Resource.Drawable.Icon,
            Name = name,
            Brand = brand,
            Desciption = description
        });
    }
}

CardFragment.cs

   public class CardFragment : Fragment
    {
        private int position;
        public static CardFragment NewInstance(int position)
        {
            var f = new CardFragment();
            var b = new Bundle();
            b.PutInt("position", position);
            f.Arguments = b;
            return f;
        }
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            position = Arguments.GetInt("position");
        }
        public override Android.Views.View  OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Bundle savedInstanceState)
        {
            var root = inflater.Inflate(Resource.Layout.menu_tagScan, container, false);

            if (position == 1)
            {
                root = inflater.Inflate(Resource.Layout.recyclerView, container, false);
            }
            else
            {
                root = inflater.Inflate(Resource.Layout.menu_tagScan, container, false);
            }

            return root;
        }
    }

I'm blocked with this probleme, this is a generale requeste (similare post in stackOverflow).

Thanks a lot,
Romain


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>