locked
Fragment Adapter Not Showing RRS feed

  • Question

  • User367727 posted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Android.App;
    using Android.Content;
    using Android.Graphics;
    using Android.OS;
    using Android.Runtime;
    using Android.App;
    using Android.Support.V4.App;
    using Android.Support.V4.View;
    using Android.Util;
    using Android.Views;
    using Android.Widget;
    using Com.Gigamole.Navigationtabstrip;
    using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
    using Fragment = Android.Support.V4.App.Fragment;
    using FragmentManager = Android.Support.V4.App.FragmentManager;
    
    namespace Kitaplik
    {
        public class AnaSayfaFragment : Android.App.Fragment
        {
            public override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Create your fragment here
            }
            public static ViewPager viewpagerim;
            Android.Support.V4.App.FragmentActivity ali;
            View AnaLayout;
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                ali = new FragmentActivity();
                AnaLayout = inflater.Inflate(Resource.Layout.activity_main, container, false);
                FlashCardDeckAdapter adapterim = new FlashCardDeckAdapter(ali.SupportFragmentManager);
                viewpagerim = AnaLayout.FindViewById<ViewPager>(Resource.Id.vpPager);
                viewpagerim.Adapter = adapterim;
                #region TabStrip
                NavigationTabStrip navigationTabStrip = AnaLayout.FindViewById<NavigationTabStrip>(Resource.Id.tagger);
                navigationTabStrip.SetTitles("Yeni Çikanlar", "Popüler", "Kesfet");
                navigationTabStrip.SetTabIndex(0, true);
                navigationTabStrip.SetStripWeight(20);
                //navigationTabStrip.StripColor = Color.Red;
                //  navigationTabStrip.TitleSize = 15;
                navigationTabStrip.InactiveColor = Color.Gray;
                navigationTabStrip.AnimationDuration = 300;
                navigationTabStrip.StripFactor = 2;
                navigationTabStrip.CornersRadius = 10;
                //navigationTabStrip.StripColor = Color.Blue;
                navigationTabStrip.ActiveColor = Color.Black;
                navigationTabStrip.SetStripType(NavigationTabStrip.StripType.Point);
                navigationTabStrip.SetStripGravity(NavigationTabStrip.StripGravity.Bottom);
                navigationTabStrip.SetTypeface("fonts/typeface.ttf");
                //navigationTabStrip.OnTabStripSelectedIndexListener = new MyListener(this);
                navigationTabStrip.SetViewPager(viewpagerim);
                #endregion
                return AnaLayout;
            }
        }
    }
    

    Wednesday, September 5, 2018 9:55 AM

Answers

  • User367727 posted

    I solved

    I use FrameLayout instead of LinearLayout.Its worked.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 7, 2018 1:08 PM

All replies

  • User367727 posted
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    using Android.Content;
    using Android.OS;
    using Android.Support.V7.Widget;
    using Android.Views;
    using Android.Widget;
    using Com.Bumptech.Glide;
    using Com.Bumptech.Glide.Request;
    using Newtonsoft.Json;
    using Stone.Vega.Library;
    using static Android.Text.TextUtils;
    
    namespace Kitaplik
    {
        public class AnaFragment : Android.Support.V4.App.Fragment
        {
            static Context contexim;
            void Downloaded(object sender, DownloadDataCompletedEventArgs e)
            {
                string json = Encoding.UTF8.GetString(e.Result);
                kitapList = JsonConvert.DeserializeObject<List<Kitaplar>>(json);
                ListGorunum.SetAdapter(new RecyclerAdapter(kitapList, this));
            }
            RecyclerView ListGorunum;
            ProgressBar yukleniyor;
            View AnaLayout;
            private RecyclerView.LayoutManager mLayoutManager;
                private List<Kitaplar> kitapList;
            public string Get(string uri)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
            private WebClient mClient;
            private Uri mUrl;
            public override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                contexim = Context;
                #region SampleData
                //var urim = Android.Net.Uri.Parse("http://localhost/mobil/api.php");
                //var jsonum = Get("http://www.pizzaboy.de/app/pizzaboy.json");
                //JsonValue jValue = JsonValue.Parse(jsonum);
                //foreach (var item in (JsonArray)jValue)
                //{
                //    kitapList.Add(new Kitaplar() { Ad = item["Name"].ToString().Trim().Trim('"'), Kategori = item["Address1"].ToString().Trim().Trim('"'), ResimUrl = item["Zip"].ToString().Trim().Trim('"'), Tarih = item["City"].ToString().Trim().Trim('"'), });
                //}
                //kitapList.Add(new Kitaplar() { Yazar = "Seyhülislam Mehmet Efendi", Ad = "Fetvalar", Kategori = "Fikih", Tarih = "1209", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Erzurumlu Ibrahim Hakki Hz.", Ad = "Marifetname", Kategori = "Tarih", Tarih = "1305", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Ahmet Cevdet Pasa", Ad = "Mecelle", Kategori = "Ansiklopedi", Tarih = "1145", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Gönenli Mustafa Efendi", Ad = "Fikih Dersleri", Kategori = "Sözlük", Tarih = "1256", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Seyhülislam Mehmet Efendi", Ad = "Fetvalar", Kategori = "Fikih", Tarih = "1209", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Erzurumlu Ibrahim Hakki Hz.", Ad = "Marifetname", Kategori = "Tarih", Tarih = "1305", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Ahmet Cevdet Pasa", Ad = "Mecelle", Kategori = "Ansiklopedi", Tarih = "1145", ResimUrl = "sdg" });
                //kitapList.Add(new Kitaplar() { Yazar = "Gönenli Mustafa Efendi", Ad = "Fikih Dersleri", Kategori = "Sözlük", Tarih = "1256", ResimUrl = "sdg" });
                #endregion
            }
            public static AnaFragment NewInstance(int key)
            {
                var bundle = new Bundle();
                bundle.PutInt("sayfa_istek", key);
                return new AnaFragment { Arguments = bundle };
            }
            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);
                mClient = new WebClient();
                kitapList = new List<Kitaplar>();
                var gelen = Arguments.GetInt("sayfa_istek");
                switch (gelen)
                {
                    case 1:
                        mUrl = new Uri("http://ardom.merakla.com/api.php?durum=yeni_cikan");
                        break;
                    case 2:
                        mUrl = new Uri("http://ardom.merakla.com/api.php?durum=populer");
                        break;
                    case 3:
                        mUrl = new Uri("http://ardom.merakla.com/api.php");
                        break;
                }// 60 102
                mClient.DownloadDataAsync(mUrl);
                mClient.DownloadDataCompleted += Downloaded;
                //if (Arguments != null)
                //    Toast.MakeText(Activity, Arguments.GetInt("sayfa_istek").ToString(), ToastLength.Short).Show();
                AnaLayout = inflater.Inflate(Resource.Layout.AnaLayout, container, false);
                mLayoutManager = new LinearLayoutManager(this.Activity);
                ListGorunum = AnaLayout.FindViewById<RecyclerView>(Resource.Id.listgorunum);
                yukleniyor = AnaLayout.FindViewById<ProgressBar>(Resource.Id.yukleniyor);
                ListGorunum.SetLayoutManager(new VegaLayoutManager());
                yukleniyor.Visibility = ViewStates.Gone;
                return AnaLayout;
            }
            internal class RecyclerAdapter : RecyclerView.Adapter
            {
                private List<Kitaplar> mEmails;
                AnaFragment activity;
                public RecyclerAdapter(List<Kitaplar> emails, AnaFragment activity)
                {
                    mEmails = emails;
                    this.activity = activity;
                }
    
                public override int ItemCount
                {
                    get { return mEmails.Count; }
                }
                static String casus;
                public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
                {
                    MyView myHolder = holder as MyView;
                    myHolder.yazarAdView.Text = mEmails[position].yazarAd;
                    myHolder.kitapAdView.Text = mEmails[position].Ad;
                    myHolder.kitapTarihView.Text = mEmails[position].Tarih;
                    myHolder.kitapKategoriView.Text = mEmails[position].kategoriAd;
                    holder.IsRecyclable = false;
                    casus = mEmails[position].ResimUrl;
                    Glide.With(activity)
                    .Load(casus)
                    .Apply(RequestOptions.CircleCropTransform()).Into(myHolder.kitap_resim);
                }
                public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
                {
                    MyView view;
                    View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.kitaplar_row, parent, false);
                    TextView yazarAd = row.FindViewById<TextView>(Resource.Id.yazarAd);
                    TextView kitapAd = row.FindViewById<TextView>(Resource.Id.kitapAd);
                    TextView kitapTarih = row.FindViewById<TextView>(Resource.Id.kitapTarih);
                    TextView kitapKategori = row.FindViewById<TextView>(Resource.Id.kitapKategori);
                    ImageView kitap_resim = row.FindViewById<ImageView>(Resource.Id.kitap_resim);
                    kitapAd.Ellipsize = TruncateAt.Marquee;
                    kitapAd.SetSingleLine(true);
                    kitapAd.Selected = true;
                    yazarAd.Ellipsize = TruncateAt.End;
                    yazarAd.SetSingleLine(true);
                    yazarAd.Selected = true;
                    view = new MyView(row) { yazarAdView = yazarAd, kitapAdView = kitapAd, kitapTarihView = kitapTarih, kitapKategoriView = kitapKategori, kitap_resim = kitap_resim };
                    return view;
                }
    
                public class MyView : RecyclerView.ViewHolder
                {
                    public View mMainView { get; set; }
                    public TextView yazarAdView { get; set; }
                    public TextView kitapAdView { get; set; }
                    public TextView kitapTarihView { get; set; }
                    public TextView kitapKategoriView { get; set; }
                    public ImageView kitap_resim { get; set; }
                    public MyView(View view) : base(view)
                    {
                        //ExpandableRelativeLayout expandableLayout = view.FindViewById<ExpandableRelativeLayout>(Resource.Id.expandableLinearLayout1);
                        //mMainView = view;
                        //mMainView.Click += delegate {
                        //    expandableLayout.Expand();
                        //};
                    }
                }
            }
        }
    }
    
    Wednesday, September 5, 2018 5:08 PM
  • User364855 posted

    @ispirLee25 I see you have posted a similar question in this forum: https://forums.xamarin.com/discussion/138300/viewpager-not-showing-in-contentframelayout

    It seems that your NavigationTabStrip prevents your ViewPager from working. I have tested your code if you remove the NavigationTabStrip and pass the FragmentActivity from the MainActivity class instead of creating a new one in the AnaSayfaFragment class. The code works.

        FragmentActivity activity;
        public AnaSayfaFragment(FragmentActivity activity)
        {
            this.activity = activity;
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            AnaLayout = inflater.Inflate(Resource.Layout.activity_main, container, false);
            FlashCardDeckAdapter adapterim = new FlashCardDeckAdapter(activity.SupportFragmentManager);
            viewpagerim = AnaLayout.FindViewById<ViewPager>(Resource.Id.vpPager);
            viewpagerim.Adapter = adapterim;
            ...
    

    Thursday, September 6, 2018 10:00 AM
  • User367727 posted

    Very thanks my brother :)

    How to use navigation tab strip ? Have you idea?

    Thursday, September 6, 2018 10:34 AM
  • User367727 posted

    I solved

    I use FrameLayout instead of LinearLayout.Its worked.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 7, 2018 1:08 PM