【问题标题】:Passing position as an argument to Fragment In FragmentPagerAdapter将位置作为参数传递给 FragmentPagerAdapter 中的 Fragment
【发布时间】:2015-04-18 20:39:39
【问题描述】:

getItem() 的位置并不总是正确的!有时它会显示 0,1,2,3,4 然后如果我返回它会显示不同的位置 (4,2,3,1,0)。如果我需要将位置传递到数组列表/树形图中,使用“位置”的正确方法是什么???

02-18 20:07:22.453  11479-11479/com.app E/aaa﹕ position created:0
02-18 20:07:22.453  11479-11479/com.app E/aaa﹕ position created:1
02-18 20:07:29.053  11479-11479/com.app E/aaa﹕ position created:2
02-18 20:07:35.503  11479-11479/com.app E/aaa﹕ position created:0

行:

 return SingleCard.newInstance(get_all_cards_as_objects().get(position));

需要正确的位置,而不是被渲染到内存中的位置!我需要片段内的数据随实际页面位置而变化。它将根据实际页面位置知道要抓取的数据(从地图中)。

final ViewPager pager = (ViewPager) findViewById(R.id.pager);
        this.parent_nested_fragment_activity = this;

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()){
            private final int PARENT_COUNT = get_all_cards_as_objects().size();

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                Object obj = super.instantiateItem(container, position);
                pager.setObjectForPosition(obj, position);
                return obj;
            }

            @Override
            public Fragment getItem(int position) {
            Toast.makeText(CardDealer.this,"Selected page position: " + position, Toast.LENGTH_SHORT).show();
            Log.e("aaa", "position created:" + position);
                    return SingleCard.newInstance(get_all_cards_as_objects().get(position));
            }

我遇到了像他这样的问题,但那个答案没有帮助: FragmentPagerAdapter getItem wrong position

我也不确定如何在我的片段中实现这个答案:getItem() calling multiple time in FragmentPagerAdapter

像这样: Weird dissappearing Fragments with FragmentPagerAdapter

基本上我已经调查过了:

pager.getCurrentItem();

如何使用 getItem() 将其传递到已创建的片段中;

 return SingleCard.newInstance(get_all_cards_as_objects().get(pager.getCurrentItem()));

【问题讨论】:

    标签: java android fragment fragmentpageradapter


    【解决方案1】:

    好的,我想通了,我正在创建的实际片段有问题。我在 SingleCard 片段中使用了静态 int。将其更改为非静态 int 解决了问题!

       //problem:
         private static int page;
       //solution:
         private int page;
    
    public class SingleCard extends Fragment {
        public static SingleCard newInstance ( int sent_in_position ) {
            SingleCard fragmentFirst = new SingleCard ();
            Bundle args = new Bundle();
            args.putInt("position", sent_in_position);
            fragmentFirst . setArguments(args);
            return fragmentFirst;
        }
    
    @Override
    public void onCreate (Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("position", 0);
    }
    

    【讨论】:

      【解决方案2】:

      另外检查 getArguments() 是否为 null,因为如果为 null 将返回错误

      page = getArguments() != null ? getArguments().getInt("position") : 0;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-23
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多