简体   繁体   English

将位置作为参数传递给 FragmentPagerAdapter 中的 Fragment

[英]Passing position as an argument to Fragment In FragmentPagerAdapter

The getItem() position is not always right! getItem() 位置并不总是正确的! Sometimes it will show 0,1,2,3,4 then if I go back it shows a different position (4,2,3,1,0).有时它会显示 0,1,2,3,4 然后如果我回去它会显示不同的位置(4,2,3,1,0)。 What is the proper way to use "position", if I need the position to pass into an arraylist/treemap???如果我需要将位置传递到数组列表/树状图中,使用“位置”的正确方法是什么???

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

The line:线路:

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

Needs the correct position, not the one being rendered into memory!需要正确的位置,而不是被渲染到内存中的位置! I need the data inside the fragment to change with the actual page position.我需要片段内的数据随实际页面位置而变化。 It will know the data to grab (from the map) based off the actual page 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));
            }

I have a problem like his, but that answer did not help: FragmentPagerAdapter getItem wrong position我有一个像他一样的问题,但那个答案没有帮助: FragmentPagerAdapter getItem wrong position

I'm also not sure how to implement this answer into my fragments: getItem() calling multiple time in FragmentPagerAdapter我也不确定如何将这个答案实现到我的片段中: getItem() 在 FragmentPagerAdapter 中多次调用

Like this: Weird dissappearing Fragments with FragmentPagerAdapter像这样: 用 FragmentPagerAdapter 消失的奇怪片段

Basically I've looked into:基本上我已经调查过:

pager.getCurrentItem();

How do I pass this into an already created fragment withing getItem();如何使用 getItem() 将其传递到已创建的片段中;

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

Ok I figured it out, the actual fragment I was creating had an issue.好的,我想通了,我创建的实际片段有问题。 I was using a static int inside of my SingleCard fragment.我在我的 SingleCard 片段中使用了一个静态int Changing it to a non static int fixed the problems!将其更改为非静态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);
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM