简体   繁体   中英

Passing position as an argument to Fragment In FragmentPagerAdapter

The getItem() position is not always right! Sometimes it will show 0,1,2,3,4 then if I go back it shows a different position (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

I'm also not sure how to implement this answer into my fragments: getItem() calling multiple time in FragmentPagerAdapter

Like this: Weird dissappearing Fragments with FragmentPagerAdapter

Basically I've looked into:

pager.getCurrentItem();

How do I pass this into an already created fragment withing 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. Changing it to a non static int fixed the problems!

   //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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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