本文介绍了刷新使用视图寻呼机在一个片段中选择选项卡上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有我的看法片段内页,但我想重新加载标签selected.I试图在 GetItemPosition PositionNone 上的数据/ code>方法在我的 FragmentPagerAdapter ,但它不工作。



我尝试添加 notifyDataSetChanged(); 在选项卡中选择,但它会抛出空指针异常。
我甚至尝试设置 viewPager.setOffscreenPageLimit
我设法找到的链接:

,但它是基于Java。
我已成功地大部分代码转换为C#,但卡上的注释代码。我不知道如何调用该侦听器,并在同一时间实例化类?有很多类似的问题,但不幸的是没有之一Xamarin的Android。



我需要做的这xamarin机器人。任何帮助将不胜感激。



这是我的FragmentAdapter

 公共类TabsFragmentPagerAdapter:FragmentPagerAdapter 
{
私人只读Android.Support.V4.App.Fragment []片段;
静态只读INT NUM_ITEMS = 2;
私人只读Android.Support.V4.App.FragmentManager FragmentManager;
私人Android.Support.V4.App.Fragment mFragmentAtPos0;
私人只读ICharSequence []标题;

公共TabsFragmentPagerAdapter(Android.Support.V4.App.FragmentManager FM,Android.Support.V4.App.Fragment []片段,ICharSequence []标题):基地(FM)
{
this.fragments =片段;
this.titles =职称;

}
公众覆盖诠释计数
{
得到
{
返回fragments.Length;
}
}

公共覆盖INT GetItemPosition(java.lang.Object中objectValue)
{
如果(objectValue.GetType()== typeof运算( startJobFrag)及和放大器; mFragmentAtPos0.GetType()== typeof运算(jobCaptureFrag))
{
返回PositionNone;
}
返回PositionUnchanged;
}


公众覆盖ICharSequence GetPageTitleFormatted(INT位置)
{
返回标题[位置]
}


公众覆盖Android.Support.V4.App.Fragment的GetItem(INT位置)
{
如果(位置== 0)
{
如果(mFragmentAtPos0 == NULL)
{
//没有工作
}
返回mFragmentAtPos0;
}
,否则
{
返回新jobCaptureFrag();
}
}

这是我的Actvity

 无效FnInitTabLayout()
{
变种碎片=新Android.Support.V4.App.Fragment []
{
新的frag 1(),
新FRAG 2(),
新的frag 3(),
};

//标签标题数组
VAR标题= CharSequence.ArrayFromStringArray(新[] {
1,
第2页,
第3页,
});

VAR viewPager = FindViewById< ViewPager>(Resource.Id.viewpager);
// viewpager控股片段阵列和分页标题字体
// viewPager。
viewPager.Adapter =新TabsFragmentPagerAdapter(SupportFragmentManager,片段,标题);

//给这个TabLayout的ViewPager
tabLayout.SetupWithViewPager(viewPager);

viewPager.PageSelected + =(对象发件人,ViewPager.PageSelectedEventArgs E)=>
{
// FnInitTabLayout();
如果(e.Position == 0)
{
//处理选项卡中单击/选中

}
};
}

公共接口FirstPageFragmentListener
{
无效onSwitchToNextFragment();
}



片段

 公共类片段1:Android.Support.V4.App.Fragment 
{
公众覆盖无效的onCreate(捆绑savedInstanceState)
{
群。在OnCreate(savedInstanceState);

//创建您的片段在这里
}

公众覆盖查看OnCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
{
//使用这个来回报您的自定义视图此片段
//返回inflater.Inflate(Resource.Layout.YourFragment,集装箱,FALSE);

查看视图= inflater.Inflate(Resource.Layout.myview,集装箱,FALSE);

}
}


解决方案

我设法解决它在我自己的。
在我的活动我叫 NotifyDataSetChanged()上Pageselected事件



活动

  ViewPager.PageSelected + =(对象发件人,ViewPager.PageSelectedEventArgs E)=> 
{
如果(e.Position == 1)
{
_pager.Adapter.NotifyDataSetChanged();
}
};



然后,我改变了我的适配器从 FragmentPagerAdapter FragmentStatePagerAdapter 并重写 GetItemPosition



适配器

 公共类TabsFragmentPagerAdapter:FragmentStatePagerAdapter 
{
公共覆盖INT GetItemPosition(爪哇。 Lang.Object objectValue)
{
返回PositionNone;
}
}



刷新一个特定的标签 - 适配器内部

 公众覆盖Android.Support.V4.App.Fragment的GetItem(INT位置)
$ { b $ b如果(位置== 1)//第二个选项卡中选择
{
NotifyDataSetChanged();
}
返回片段[位置]
}



我敢肯定有一个更好的方式来做到这一点,但这个工程。


I have my fragments inside view page but I would like to reload the data on the tab selected.I tried returning PositionNone in the GetItemPosition method in my FragmentPagerAdapter but it does not work.

I tried adding notifyDataSetChanged(); on tab selected but it throws nullpointer exception.I even tried setting the viewPager.setOffscreenPageLimitI managed to find the link :Replace Fragment inside a ViewPagerbut it is on java.I have managed to convert most of the code to c# but gets stuck on the commented code. I am not sure how to call the listener and instantiate the class at the same time? There is a lot of similar question but unfortunately not one in Xamarin Android.

I need to do this in xamarin android. Any help will be greatly appreciated.

This is my FragmentAdapter

public class TabsFragmentPagerAdapter : FragmentPagerAdapter
{
    private readonly Android.Support.V4.App.Fragment[] fragments;
    static readonly int NUM_ITEMS = 2;
    private readonly Android.Support.V4.App.FragmentManager FragmentManager;
    private Android.Support.V4.App.Fragment mFragmentAtPos0;
    private readonly ICharSequence[] titles;

    public TabsFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm, Android.Support.V4.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
    {
        this.fragments = fragments;
        this.titles = titles;

    }
    public override int Count
    {
        get
        {
            return fragments.Length;
        }
    }

    public override int GetItemPosition(Java.Lang.Object objectValue)
    {
        if (objectValue.GetType() == typeof(startJobFrag) && mFragmentAtPos0.GetType() == typeof(jobCaptureFrag))
        {
            return PositionNone;
        }
        return PositionUnchanged;
    }


    public override ICharSequence GetPageTitleFormatted(int position)
    {
        return titles[position];
    }


    public override Android.Support.V4.App.Fragment GetItem(int position)
    {
        if (position == 0)
        {
            if (mFragmentAtPos0 == null)
            {
                //not working
            }
            return mFragmentAtPos0;
        }
        else
        {
            return new jobCaptureFrag();
        }
    }

This is my Actvity

 void FnInitTabLayout()
    {
        var fragments = new Android.Support.V4.App.Fragment[]
        {
new frag 1(),
new frag 2(),
new frag 3(),
        };

        //Tab title array
        var titles = CharSequence.ArrayFromStringArray(new[] {
    "Page 1",
    "Page 2",
     "Page 3",
   });

        var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
        //viewpager holding fragment array and tab title text
        //viewPager.
        viewPager.Adapter = new TabsFragmentPagerAdapter(SupportFragmentManager, fragments, titles);

        // Give the TabLayout the ViewPager
        tabLayout.SetupWithViewPager(viewPager);

        viewPager.PageSelected += (object sender, ViewPager.PageSelectedEventArgs e) =>
        {
            //FnInitTabLayout();
            if(e.Position == 0)
            {
              //handle tab click/selected

            }
        };
}

public interface FirstPageFragmentListener
{
    void onSwitchToNextFragment();
}

Fragment

public class Fragment1: 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.YourFragment, container, false);

        View view = inflater.Inflate(Resource.Layout.myview, container, false);

   }
}
解决方案

I managed to solve it on my own.In My activity I called NotifyDataSetChanged() on Pageselected event

Activity

  ViewPager.PageSelected += (object sender,                 ViewPager.PageSelectedEventArgs e) =>
        {
            if (e.Position == 1)
            {
                _pager.Adapter.NotifyDataSetChanged();
            }
        };

Then I changed my Adapter from FragmentPagerAdapter to FragmentStatePagerAdapter and override the GetItemPosition.

Adapter

    public class TabsFragmentPagerAdapter : FragmentStatePagerAdapter
    {
        public override int GetItemPosition(Java.Lang.Object objectValue)
        {
            return PositionNone;
        }
    }

Refresh a specific tab - inside the Adapter

 public override Android.Support.V4.App.Fragment GetItem(int position)
    {
        if(position == 1) //second tab selected
        {
            NotifyDataSetChanged();
        }
        return fragments[position];
    }

I'm sure there's a better way to do it but this works.

这篇关于刷新使用视图寻呼机在一个片段中选择选项卡上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:15