如何在片段中充气Recyclerview

时间:2017-01-04 01:27:35

标签: c# android android-fragments xamarin xamarin.android

我正在转换到我的应用程序中的标签页,我正在尝试将我的recyclerview转换为使用viewpager在我的tablayout中显示。我的问题是它没有显示我用我的代码添加的数据。只有xml文件的初始布局,其中包含每个重复行的模板。

MainActivity.cs

 protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);


            mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);

            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.SetLayoutManager(mLayoutManager);
            game = new List<Models.MatchHistoryListView>();
            mAdapter = new MatchListCustomAdapter(game, mRecyclerView);
            mRecyclerView.SetAdapter(mAdapter);
            viewPager = FindViewById<ViewPager>(Resource.Id.main_viewpager);

            setupViewPager(viewPager);
            tabLayout = FindViewById<TabLayout>(Resource.Id.main_sliding_tabs);
            tabLayout.SetupWithViewPager(viewPager);

            var trans = SupportFragmentManager.BeginTransaction();
            trans.Add(Resource.Id.main_fragmentContainer, new matchHistoryFragment(), "matchHistoryFragment");
            trans.Commit();
}
  public void addData()
{
game.Add(new Models.MatchHistoryListView()
                {
                    Name = "" + mclass.summonerId[0, i],
                    ChampionId = mclass.championId[0, i],
                    Id = mclass.recentGamesId[i],
                    Score = mclass.summonerScoreText[i],
                    championPortraito = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/champion/" + mclass.championName[0, i] + ".png"),
                    Item0 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 0] + ".png"),
                    Item1 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 1] + ".png"),
                    Item2 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 2] + ".png"),
                    Item3 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 3] + ".png"),
                    Item4 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 4] + ".png"),
                    Item5 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 5] + ".png"),
                    Item6 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/item/" + mclass.summonerItems[i, 6] + ".png"),
                    Spell1 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/spell/" + mclass.summonerSpellName[i, 0] + ".png"),
                    Spell2 = new Uri("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/spell/" + mclass.summonerSpellName[i, 1] + ".png"),
                    goldEarned = MyClass.goldEarned[i],
                    minionsKilled = MyClass.minionsKilled[i],

                    MapName = mclass.mapId[i],
                    listViewDrawable = winorlose



                });
             mAdapter.NotifyDataSetChanged();

        mRecyclerView.SetAdapter(mAdapter);

  }
.....................bunch of code here...............


private matchHistoryFragment matchHistoryFrag;
        private Fragment1 frag1;

        private void InditialFragment()
        {
            matchHistoryFrag = new matchHistoryFragment();
            frag1 = new Fragment1();
            // moreFrg = new More();
            //todoFrag = new Todo();
        }
        public void setupViewPager(ViewPager viewPager)
        {
            InditialFragment();
            ViewPagerAdapter adapter = new ViewPagerAdapter(SupportFragmentManager);


            adapter.addFragment(matchHistoryFrag, "Match History");
            adapter.addFragment(frag1, "Frag1");
            viewPager.Adapter = adapter;
        }

MatchListCustomAdapter.cs(简化了一些代码以使其更小)

public class MatchListCustomAdapter : RecyclerView.Adapter
    {
        public Context spaghettiContext;
        MainActivity mainAct = new MainActivity();
        public Activity activity;
        private List<App2.Models.MatchHistoryListView> mgame;
        private RecyclerView mRecyclerView;
        private MyClass mclass = new MyClass();
        public MatchListCustomAdapter(List<Models.MatchHistoryListView> game,RecyclerView recyclerView)
        {

            this.mgame = game;
            mRecyclerView = recyclerView;
        }
        public class MyView : RecyclerView.ViewHolder
        {
            .....objects declared here....


            public MyView(View view) : base(view)
            {

                mMainView = view;


            }


        }
        public override int ItemCount
        {
            get
            {
                return mgame.Count;
            }
        }


        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.RecentGamesList, parent, false);
            ......finding the objects here......
            MyView view = new MyView(row) { .....sets the objects here....};
            return view;

        }

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {

            MyView myHolder = holder as MyView;

            myHolder.mMainView.Click += (s,e) =>
            {
                 .....function here........

            };
            .......Setting the values here.......
        }

    }

matchHistoryFragment.cs

namespace App2.Droid.Resources.layout.Fragments
{
    public class matchHistoryFragment : Android.Support.V4.App.Fragment
    {

        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.RecentGamesList, container, 
    }
}

}

0 个答案:

没有答案