0

I am setting up the fragment A,B thru FragmentStatePagerAdapter and having activity with FragmentActivity inside

Acitivity#onCreate

   journeyDetailInfos =  Manager.INSTANCE.getSeatOffline();
        binding = DataBindingUtil.setContentView(this, R.layout.layout);
        viewModel = ViewModelProviders.of(this).get(XYZModelView.class);

        binding.pager.setAdapter(new SeatJourneyAdapter(this, getSupportFragmentManager(), journeyDetailInfos));
        binding.pager.setOffscreenPageLimit(10);
        binding.pager.addOnPageChangeListener(this);

        binding.tablayout.setupWithViewPager(binding.pager);

        binding.tablayout.setOnClickListener(view -> onTabChange());
        lastSelectedPage = binding.tablayout.getSelectedTabPosition();

        int length = binding.tablayout.getTabCount();
        for (int i = 0; i < length; i++) {
            binding.tablayout.getTabAt(i).setCustomView(getTabView(i));
        }

SeatJourneyAdapter.java Adpater has following code

    HashMap<Integer,XYZFragment> fragments;
    ArrayList<JourneyDetailsInfo> journeyDetailInfos;
    Context context;
    private int container_id;
    HashMap mFragmentTags ;


    SeatJourneyAdapter(Context context, FragmentManager fragmentManager, ArrayList<JourneyDetailsInfo> journeyDetailInfos)
    {
        super(fragmentManager);
        frgaments = new HashMap<Integer,XYZFragment>();
        this.journeyDetailInfos = journeyDetailInfos;
        mFragmentTags = new HashMap<Integer, String>();

    }

    @Override
    public Fragment getItem(int position) {
        XYZFragement fragment = null;
            fragment = new XYZFragement();
            fragment.setPosition(position);
            fragment.setSeatDetails(journeyDetailInfos.get(position).getPaxDetailsInfos());
            fragment.setCountJourney(journeyDetailInfos.size());
            frgaments.put(position,fragment);
        return fragment;
    }

    @Override
    public int getCount() {
        return journeyDetailInfos.size();
    }

And onCreateView of Fragment is

    binding = DataBindingUtil.inflate(inflater, R.layout.layout_name, container, false);
            binding.paxWithSeatList.setLayoutManager(new LinearLayoutManager(this.getContext()));
            binding.setSeatPax(viewModel);
            viewModel.setPassengers(seatDetails);
            return binding.getRoot(); 

Problem is when the data list "journeyDetailInfos" has count of 2 the Fragment shows a data of second item of data list "journeyDetailInfos". It works smooth if the count is more than 2 While I tried with all possible solution discussed in issue nothing worked for me.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
anupam_kamble
  • 144
  • 2
  • 6

1 Answers1

0

Try FragmentPagerAdapter instead of FragmentStatePagerAdapter and Override getItemId to update pager smoothly.

SeatJourneyAdapter.java

private class SeatJourneyAdapter extends FragmentPagerAdapter {

    final SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public SeatJourneyAdapter() {
        super(getChildFragmentManager());
    }

    @Override
    public Fragment getItem(final int pos) {
    XYZFragement fragment = null;
        fragment = new XYZFragement();
        fragment.setPosition(position);
        fragment.setSeatDetails(journeyDetailInfos.get(position).getPaxDetailsInfos());
        fragment.setCountJourney(journeyDetailInfos.size());
        frgaments.put(position,fragment);
        return fragment;
    }

    /**
     * Implementing this method is IMPORTANT for detorying the fragment.
     * Use unique id for each item on pagerData.
     */
    @Override
    public long getItemId(int position) {
        return journeyDetailInfos.get(position).getId(); // Unique id by position and pager data. 
    }


    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

    @Override
    public int getCount() {
        return journeyDetailInfos.size();
    }
}
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73