0

ViewPager loads the data of next fragment while swiping or tab change.

I'm using same fragment in view pager and replacing the data of fragment according to view pager position.

Below is the code to set the adapter to view pager

    Adapter adapter = new Adapter(getSupportFragmentManager());

    ImageListFragment fragment = new ImageListFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("type", 1);
    fragment.setArguments(bundle);
    adapter.addFragment(fragment, getString(R.string.item_1));

    fragment = new ImageListFragment();
    bundle = new Bundle();
    bundle.putInt("type", 2);
    fragment.setArguments(bundle);
    adapter.addFragment(fragment, getString(R.string.item_2));

    fragment = new ImageListFragment();
    bundle = new Bundle();
    bundle.putInt("type", 3);
    fragment.setArguments(bundle);
    adapter.addFragment(fragment, getString(R.string.item_3));

    fragment = new ImageListFragment();
    bundle = new Bundle();
    bundle.putInt("type", 4);
    fragment.setArguments(bundle);
    adapter.addFragment(fragment, getString(R.string.item_4));

    fragment = new ImageListFragment();
    bundle = new Bundle();
    bundle.putInt("type", 5);
    fragment.setArguments(bundle);
    adapter.addFragment(fragment, getString(R.string.item_5));
    adapter.notifyDataSetChanged();

and the Adapter code is below

    static class Adapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);

        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }

the Fragment code is below

    public static final String STRING_IMAGE_URI = "ImageUri";
    public static final String STRING_IMAGE_POSITION = "ImagePosition";
    public static MainActivity mActivity;
    public static int type;
    ArrayList <ItemsDetails> itemsDetailsArrayList =new ArrayList<>();
    RecyclerView rv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rv = (RecyclerView) inflater.inflate(R.layout.layout_recylerview_list, container, false);
       setupRecyclerView();
        return rv;
    }

    private void setupRecyclerView() {

        if (ImageListFragment.this.getArguments().getInt("type") == 1){
            type =1;
            Log.e("ImageListFragment", "1 - fragment - offer");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setItemList();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);
        }else if (ImageListFragment.this.getArguments().getInt("type") == 2){
            type =2;
            Log.e("ImageListFragment", "2 - fragment - Electronics");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setElectronicsUrlsDetails();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);
        }else if (ImageListFragment.this.getArguments().getInt("type") == 3){
            type =3;
            Log.e("ImageListFragment", "3 - fragment - Lifestyle");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setLifeStyleUrls();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);

        }else if (ImageListFragment.this.getArguments().getInt("type") == 4){
            type =4;
            Log.e("ImageListFragment", "4 - fragment - Home Appliances");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setHomeApplianceUrls();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);
        }else if (ImageListFragment.this.getArguments().getInt("type") == 5){
            type =5 ;
            Log.e("ImageListFragment", "5 - fragment - Books");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setItemList();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);
        }else {
            type =6;
            Log.e("ImageListFragment", "6 - fragment - more");
            itemsDetailsArrayList.clear();
            ImageUrlUtils.setItemList();
            itemsDetailsArrayList= ImageUrlUtils.getItemsList();
            setAdapter(rv, itemsDetailsArrayList);
        }
    }

       private void setAdapter(RecyclerView rv, ArrayList <ItemsDetails> itemsDetailsArrayList)
    {
        Log.e("ImageListFragment", +type +" itemsDetailsArrayList - "+itemsDetailsArrayList.size());
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        SimpleStringRecyclerViewAdapter simpleStringRecyclerViewAdapter = new SimpleStringRecyclerViewAdapter(mActivity, itemsDetailsArrayList);
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(simpleStringRecyclerViewAdapter);
    }

Now I'm facing the issue that first fragment loads the data second fragment. for example if I'm on nth fragment the data shows of n+1th fragment.

and the ImageUrlUtils class is below

    public class ImageUrlUtils {
    static ArrayList<String> wishlistImageUri = new ArrayList<>();
    static ArrayList<ItemsDetails> cartListImageUri = new ArrayList<>();

    static ArrayList<ItemsDetails> itemsDetails = new ArrayList<>();
     static ArrayList<ItemsDetails> wishlistedItemsDetails = new ArrayList<>();

public static ArrayList<ItemsDetails> getItemsList()
{
    return itemsDetails;
}
public static void setItemList()
{
    itemsDetails.clear();
    String[] strings = getOffersUrls();

    for (int i =0; i<strings.length; i++)
    {
        ItemsDetails details = new ItemsDetails();
        details.setItemId("food__00"+i);
        details.setItemName("Food Name");
        details.setItemDesc("Offer Desc Offer Desc Offer Desc Offer Desc Offer Desc Food Desc Food Desc");
        details.setItemFullPlatePrice((i+1)*100);
        details.setItemHalfPlateSize((i+1)*100/2);
        details.setDefaultPlateSize(0);
        ArrayList arrayList = new ArrayList();
        for (int j =0 ; j< 5; j++)
        {
          arrayList.add("Point "+j);
        }
        details.setItemsDetails(arrayList);
        details.setImgUrl(strings[i]);
        details.setWishlisted(false);
        details.setQuatity(0);
        itemsDetails.add(details);
    }
}

public static void setElectronicsUrlsDetails()
{
     itemsDetails.clear();
    String[] strings = getElectronicsUrls();

    for (int i =0; i<strings.length; i++)
    {
        ItemsDetails details = new ItemsDetails();
        details.setItemId("food__00"+i);
        details.setItemName("Food Name");
        details.setItemDesc("Breakfast Desc Breakfast Desc Breakfast Desc Food Desc Food Desc Food Desc Food Desc");
        details.setItemFullPlatePrice((i+1)*100);
        details.setItemHalfPlateSize((i+1)*100/2);
        details.setDefaultPlateSize(0);
        ArrayList arrayList = new ArrayList();
        for (int j =0 ; j< 5; j++)
        {
            arrayList.add("Point "+j);
        }
        details.setItemsDetails(arrayList);
        details.setImgUrl(strings[i]);
        details.setWishlisted(false);
        details.setQuatity(0);
        itemsDetails.add(details);
    }
}

public static void setLifeStyleUrls()
{
    itemsDetails.clear();
    String[] strings = getLifeStyleUrls();

    for (int i =0; i<strings.length; i++)
    {
        ItemsDetails details = new ItemsDetails();
        details.setItemId("food__00"+i);
        details.setItemName("Food Name");
        details.setItemDesc("Lunch Desc Lunch Desc Lunch Desc Lunch Desc Food Desc Food Desc Food Desc");
        details.setItemFullPlatePrice((i+1)*100);
        details.setItemHalfPlateSize((i+1)*100/2);
        details.setDefaultPlateSize(0);
        ArrayList arrayList = new ArrayList();
        for (int j =0 ; j< 5; j++)
        {
            arrayList.add("Point "+j);
        }
        details.setItemsDetails(arrayList);
        details.setImgUrl(strings[i]);
        details.setWishlisted(false);
        details.setQuatity(0);
        itemsDetails.add(details);
    }
}

public static void setHomeApplianceUrls()
{
    itemsDetails.clear();
    String[] strings = getHomeApplianceUrls();

    for (int i =0; i<strings.length; i++)
    {
        ItemsDetails details = new ItemsDetails();
        details.setItemId("food__00"+i);
        details.setItemName("Food Name");
        details.setItemDesc("Dinner Desc Dinner Desc Dinner Desc Food Desc Food Desc Food Desc Food Desc");
        details.setItemFullPlatePrice((i+1)*100);
        details.setItemHalfPlateSize((i+1)*100/2);
        details.setDefaultPlateSize(0);
        ArrayList arrayList = new ArrayList();
        for (int j =0 ; j< 5; j++)
        {
            arrayList.add("Point "+j);
        }
        details.setItemsDetails(arrayList);
        details.setImgUrl(strings[i]);
        details.setWishlisted(false);
        details.setQuatity(0);
        itemsDetails.add(details);
    }
}

public static String[] getImageUrls() {
    String[] urls = new String[] {
            "https://static.pexels.com/photos/5854/sea-woman-legs-water-medium.jpg",
            "https://static.pexels.com/photos/6245/kitchen-cooking-interior-decor-medium.jpg",
            "https://static.pexels.com/photos/6770/light-road-lights-night-medium.jpg",
            "https://static.pexels.com/photos/6041/nature-grain-moving-cereal-medium.jpg",
            "https://static.pexels.com/photos/7116/mountains-water-trees-lake-medium.jpg",
            "https://static.pexels.com/photos/6601/food-plate-yellow-white-medium.jpg",

            "https://static.pexels.com/photos/7262/clouds-ocean-seagull-medium.jpg",
            "https://static.pexels.com/photos/5968/wood-nature-dark-forest-medium.jpg",

            "https://static.pexels.com/photos/6571/pexels-photo-medium.jpeg",
            "https://static.pexels.com/photos/6740/food-sugar-lighting-milk-medium.jpg",
            "https://static.pexels.com/photos/5659/sky-sunset-clouds-field-medium.jpg",
            "https://static.pexels.com/photos/6945/sunset-summer-golden-hour-paul-filitchkin-medium.jpg",
            "https://static.pexels.com/photos/6151/animal-cute-fur-white-medium.jpg",
            "https://static.pexels.com/photos/5696/coffee-cup-water-glass-medium.jpg",
            "https://static.pexels.com/photos/6789/flowers-petals-gift-flower-medium.jpg",
            "https://static.pexels.com/photos/7202/summer-trees-sunlight-trail-medium.jpg",
            "https://static.pexels.com/photos/7147/night-clouds-summer-trees-medium.jpg",
            "https://static.pexels.com/photos/6342/woman-notebook-working-girl-medium.jpg",
            "https://static.pexels.com/photos/5998/sky-love-people-romantic-medium.jpg",
            "https://static.pexels.com/photos/6872/cold-snow-nature-weather-medium.jpg",
            "https://static.pexels.com/photos/7045/pexels-photo-medium.jpeg",
            "https://static.pexels.com/photos/6923/mountains-fog-green-beauty-medium.jpg",
            "https://static.pexels.com/photos/6946/summer-bicycle-letsride-paul-filitchkin-medium.jpg",
            "https://static.pexels.com/photos/5650/sky-clouds-field-blue-medium.jpg",
            "https://static.pexels.com/photos/6292/blue-pattern-texture-macro-medium.jpg",
            "https://static.pexels.com/photos/6080/grass-lawn-technology-tablet-medium.jpg",
            "https://static.pexels.com/photos/7124/clouds-trees-medium.jpg",
            "https://static.pexels.com/photos/5923/woman-girl-teenager-wine-medium.jpg",
            "https://static.pexels.com/photos/6133/food-polish-cooking-making-medium.jpg",
            "https://static.pexels.com/photos/6224/hands-people-woman-working-medium.jpg",
            "https://static.pexels.com/photos/6414/rucola-young-argula-sproutus-medium.jpg",
            "https://static.pexels.com/photos/6739/art-graffiti-abstract-vintage-medium.jpg",
            "https://static.pexels.com/photos/6703/city-train-metal-public-transportation-medium.jpg",
            "https://static.pexels.com/photos/6851/man-love-woman-kiss-medium.jpg",
            "https://static.pexels.com/photos/6225/black-scissors-medium.jpg",
            "https://static.pexels.com/photos/7185/night-clouds-trees-stars-medium.jpg",
            "https://static.pexels.com/photos/5847/fashion-woman-girl-jacket-medium.jpg",
            "https://static.pexels.com/photos/5542/vintage-railroad-tracks-bw-medium.jpg",
            "https://static.pexels.com/photos/5938/food-salad-healthy-lunch-medium.jpg",
            "https://static.pexels.com/photos/7234/water-clouds-ocean-splash-medium.jpg",
            "https://static.pexels.com/photos/6418/flowers-flower-roses-rose-medium.jpg",
            "https://static.pexels.com/photos/6436/spring-flower-hyacinth-medium.jpg",
            "https://static.pexels.com/photos/6351/smartphone-desk-laptop-technology-medium.jpg",
            "https://static.pexels.com/photos/5618/fish-fried-mint-pepper-medium.jpg",
            "https://static.pexels.com/photos/6874/landscape-nature-water-rocks-medium.jpg",
            "https://static.pexels.com/photos/6918/bridge-fog-san-francisco-lets-get-lost-medium.jpg",
            "https://static.pexels.com/photos/5658/light-sunset-red-flowers-medium.jpg",
            "https://static.pexels.com/photos/6111/smartphone-friends-internet-connection-medium.jpg",
            "https://static.pexels.com/photos/5670/wood-fashion-black-stylish-medium.jpg",
            "https://static.pexels.com/photos/5838/hands-woman-hand-typing-medium.jpg",
            "https://static.pexels.com/photos/7050/sky-clouds-skyline-blue-medium.jpg",
            "https://static.pexels.com/photos/6036/nature-forest-tree-bark-medium.jpg",
            "https://static.pexels.com/photos/5676/art-camera-photography-picture-medium.jpg",
            "https://static.pexels.com/photos/6688/beach-sand-blue-ocean-medium.jpg",
            "https://static.pexels.com/photos/6901/sunset-clouds-golden-hour-lets-get-lost-medium.jpg",
            "https://static.pexels.com/photos/7260/rocks-fire-camping-medium.jpg",
            "https://static.pexels.com/photos/5672/dog-cute-adorable-play-medium.jpg",
            "https://static.pexels.com/photos/7261/rocks-trees-hiking-trail-medium.jpg",
            "https://static.pexels.com/photos/6411/smartphone-girl-typing-phone-medium.jpg",
            "https://static.pexels.com/photos/6412/table-white-home-interior-medium.jpg",
            "https://static.pexels.com/photos/6184/technology-keyboard-desktop-book-medium.jpg",
            "https://static.pexels.com/photos/7295/controller-xbox-gaming-medium.jpg",
            "https://static.pexels.com/photos/6732/city-cars-traffic-lights-medium.jpg",
            "https://static.pexels.com/photos/7160/bird-trees-medium.jpg",
            "https://static.pexels.com/photos/6999/red-hand-summer-berries-medium.jpg",
            "https://static.pexels.com/photos/5787/flowers-meadow-spring-green-medium.jpg",
            "https://static.pexels.com/photos/7136/water-rocks-stream-leaves-medium.jpg",
            "https://static.pexels.com/photos/7291/building-historical-church-religion-medium.jpg",
            "https://static.pexels.com/photos/6696/road-nature-summer-forest-medium.jpg",
            "https://static.pexels.com/photos/7294/garden-medium.jpg",
            "https://static.pexels.com/photos/6948/flight-sky-art-clouds-medium.jpg",
            "https://static.pexels.com/photos/7299/africa-animals-zoo-zebras-medium.jpg",
            "https://static.pexels.com/photos/6345/dark-brown-milk-candy-medium.jpg",
            "https://static.pexels.com/photos/7288/animal-dog-pet-park-medium.jpg",
            "https://static.pexels.com/photos/5863/nature-plant-leaf-fruits-medium.jpg",
            "https://static.pexels.com/photos/6625/pexels-photo-medium.jpeg",
            "https://static.pexels.com/photos/6708/stairs-people-sitting-architecture-medium.jpg",
            "https://static.pexels.com/photos/6429/smartphone-technology-music-white-medium.jpg",
            "https://static.pexels.com/photos/6574/pexels-photo-medium.jpeg",
            "https://static.pexels.com/photos/7287/grass-lawn-meadow-medium.jpg",
            "https://static.pexels.com/photos/6100/man-hands-holidays-looking-medium.jpg",
            "https://static.pexels.com/photos/6100/man-hands-holidays-looking-medium.jpg",
            "https://static.pexels.com/photos/6877/dog-pet-fur-brown-medium.jpg",
            "https://static.pexels.com/photos/6790/light-road-nature-iphone-medium.jpg",
            "https://static.pexels.com/photos/7077/man-people-office-writing-medium.jpg",
            "https://static.pexels.com/photos/6889/light-mountains-sunrise-california-medium.jpg",
            "https://static.pexels.com/photos/7274/leaf-fall-foliage-medium.jpg",
            "https://static.pexels.com/photos/7285/flowers-garden-medium.jpg",
            "https://static.pexels.com/photos/6821/light-sky-beach-sand-medium.jpg",
            "https://static.pexels.com/photos/7297/animal-africa-giraffe-medium.jpg",
            "https://static.pexels.com/photos/6154/sea-sky-water-clouds-medium.jpg",
            "https://static.pexels.com/photos/7059/man-people-space-desk-medium.jpg",
            "https://static.pexels.com/photos/6666/coffee-cup-mug-apple-medium.jpg",
            "https://static.pexels.com/photos/5949/food-nature-autumn-nuts-medium.jpg",
            "https://static.pexels.com/photos/7064/man-notes-macbook-computer-medium.jpg",
            "https://static.pexels.com/photos/5743/beach-sand-legs-shoes-medium.jpg",
            "https://static.pexels.com/photos/6355/desk-laptop-working-technology-medium.jpg",
            "https://static.pexels.com/photos/5844/sea-water-boats-boat-medium.jpg",
            "https://static.pexels.com/photos/5541/city-night-building-house-medium.jpg",
            "https://static.pexels.com/photos/7017/food-peppers-kitchen-yum-medium.jpg",
            "https://static.pexels.com/photos/5725/grey-luxury-carpet-silver-medium.jpg",
            "https://static.pexels.com/photos/6932/italian-vintage-old-beautiful-medium.jpg",
            "https://static.pexels.com/photos/7093/coffee-desk-notes-workspace-medium.jpg",
    };
    return urls;
}

public static String[] getOffersUrls() {
    String[] urls = new String[]{
            "https://static.pexels.com/photos/1543/landscape-nature-man-person-medium.jpg",
            "https://static.pexels.com/photos/211048/pexels-photo-211048-medium.jpeg",
            "https://static.pexels.com/photos/1778/numbers-time-watch-white-medium.jpg",
            "https://static.pexels.com/photos/111147/pexels-photo-111147-medium.jpeg",
            "https://static.pexels.com/photos/2713/wall-home-deer-medium.jpg",
            "https://static.pexels.com/photos/168575/pexels-photo-168575-medium.jpeg",
            "https://static.pexels.com/photos/213384/pexels-photo-213384-medium.jpeg",
            "https://static.pexels.com/photos/67442/pexels-photo-67442-medium.jpeg",
            "https://static.pexels.com/photos/159494/book-glasses-read-study-159494-medium.jpeg",
            "https://static.pexels.com/photos/1543/landscape-nature-man-person-medium.jpg",
            "https://static.pexels.com/photos/211048/pexels-photo-211048-medium.jpeg",
            "https://static.pexels.com/photos/2713/wall-home-deer-medium.jpg",
            "https://static.pexels.com/photos/177143/pexels-photo-177143-medium.jpeg",
            "https://static.pexels.com/photos/106936/pexels-photo-106936-medium.jpeg"
    };
    return urls;
}

public static String[] getHomeApplianceUrls() {
    String[] urls = new String[]{
            "https://static.pexels.com/photos/1778/numbers-time-watch-white-medium.jpg",
            "https://static.pexels.com/photos/189293/pexels-photo-189293-medium.jpeg",
            "https://static.pexels.com/photos/4703/inside-apartment-design-home-medium.jpg",
            "https://static.pexels.com/photos/133919/pexels-photo-133919-medium.jpeg",
            "https://static.pexels.com/photos/111147/pexels-photo-111147-medium.jpeg",
            "https://static.pexels.com/photos/2713/wall-home-deer-medium.jpg",
            "https://static.pexels.com/photos/177143/pexels-photo-177143-medium.jpeg",
            "https://static.pexels.com/photos/106936/pexels-photo-106936-medium.jpeg",
            "https://static.pexels.com/photos/1778/numbers-time-watch-white-medium.jpg",
            "https://static.pexels.com/photos/189293/pexels-photo-189293-medium.jpeg",
            "https://static.pexels.com/photos/4703/inside-apartment-design-home-medium.jpg",
            "https://static.pexels.com/photos/133919/pexels-photo-133919-medium.jpeg",
            "https://static.pexels.com/photos/111147/pexels-photo-111147-medium.jpeg",
            "https://static.pexels.com/photos/2713/wall-home-deer-medium.jpg",
            "https://static.pexels.com/photos/177143/pexels-photo-177143-medium.jpeg",
            "https://static.pexels.com/photos/106936/pexels-photo-106936-medium.jpeg"
    };
    return urls;
}

public static String[] getElectronicsUrls() {
    String[] urls = new String[]{
            "https://static.pexels.com/photos/204611/pexels-photo-204611-medium.jpeg",
            "https://static.pexels.com/photos/214487/pexels-photo-214487-medium.jpeg",
            "https://static.pexels.com/photos/168575/pexels-photo-168575-medium.jpeg",
            "https://static.pexels.com/photos/213384/pexels-photo-213384-medium.jpeg",
            "https://static.pexels.com/photos/114907/pexels-photo-114907-medium.jpeg",
            "https://static.pexels.com/photos/185030/pexels-photo-185030-medium.jpeg",
            "https://static.pexels.com/photos/133579/pexels-photo-133579-medium.jpeg",
            "https://static.pexels.com/photos/51383/photo-camera-subject-photographer-51383-medium.jpeg",
            "https://static.pexels.com/photos/205926/pexels-photo-205926-medium.jpeg",
            "https://static.pexels.com/photos/2396/light-glass-lamp-idea-medium.jpg",
            "https://static.pexels.com/photos/1854/person-woman-hand-relaxing-medium.jpg",
            "https://static.pexels.com/photos/204611/pexels-photo-204611-medium.jpeg",
            "https://static.pexels.com/photos/214487/pexels-photo-214487-medium.jpeg",
            "https://static.pexels.com/photos/168575/pexels-photo-168575-medium.jpeg",
            "https://static.pexels.com/photos/213384/pexels-photo-213384-medium.jpeg",
            "https://static.pexels.com/photos/114907/pexels-photo-114907-medium.jpeg"
    };
    return urls;
}

public static String[] getLifeStyleUrls() {
    String[] urls = new String[]{
            "https://static.pexels.com/photos/169047/pexels-photo-169047-medium.jpeg",
            "https://static.pexels.com/photos/160826/girl-dress-bounce-nature-160826-medium.jpeg",
            "https://static.pexels.com/photos/1702/bow-tie-businessman-fashion-man-medium.jpg",
            "https://static.pexels.com/photos/35188/child-childrens-baby-children-s-medium.jpg",
            "https://static.pexels.com/photos/70845/girl-model-pretty-portrait-70845-medium.jpeg",
            "https://static.pexels.com/photos/26378/pexels-photo-26378-medium.jpg",
            "https://static.pexels.com/photos/193355/pexels-photo-193355-medium.jpeg",
            "https://static.pexels.com/photos/1543/landscape-nature-man-person-medium.jpg",
            "https://static.pexels.com/photos/211048/pexels-photo-211048-medium.jpeg",
            "https://static.pexels.com/photos/189857/pexels-photo-189857-medium.jpeg",
            "https://static.pexels.com/photos/157948/model-modelling-attractive-fashion-157948-medium.jpeg",
            "https://static.pexels.com/photos/33608/dog-ice-woman-purple-medium.jpg",
            "https://static.pexels.com/photos/157940/hair-bracelet-beautiful-beauty-157940-medium.jpeg",
            "https://static.pexels.com/photos/35188/child-childrens-baby-children-s-medium.jpg",
            "https://static.pexels.com/photos/70845/girl-model-pretty-portrait-70845-medium.jpeg",
            "https://static.pexels.com/photos/26378/pexels-photo-26378-medium.jpg",
            "https://static.pexels.com/photos/193355/pexels-photo-193355-medium.jpeg",
    };
    return urls;
}

public void addWishlistImageUri(String wishlistImageUri) {
    this.wishlistImageUri.add(0,wishlistImageUri);
}

public static void addWishlistImageUri(ItemsDetails wishlistImageUri) {
    wishlistImageUri.setQuatity(1);
    wishlistedItemsDetails.add(wishlistImageUri);
}

public static void removeWishlistImageUri(String foofId) {

    for (int i=0; i<wishlistedItemsDetails.size() ;i++)
    {
        ItemsDetails itemsDetails = wishlistedItemsDetails.get(i);
        if (itemsDetails.getItemId().equals(foofId))
        {
            wishlistedItemsDetails.remove(i);
            break;
        }
    }
}

public static ArrayList<ItemsDetails> getWishlistImageUri(){
    return wishlistedItemsDetails;
}

public static void updateItemsDeatils(String itemId){

    for (int i =0; i< itemsDetails.size(); i++)
    {
        ItemsDetails details = itemsDetails.get(i);
        if (details.getItemId().equals(itemId)) {
            details.setQuatity(0);
            details.setWishlisted(false);
            break;
        }

}

public void addCartListImageUri(ItemsDetails wishlistImageUri, int default_plate_postion) {

  if (!checkCart(wishlistImageUri)) {
      wishlistImageUri.setQuatity(1);
      wishlistImageUri.setDefaultPlateSize(default_plate_postion);
      this.cartListImageUri.add(wishlistImageUri);
  }
}

public static boolean checkCart(ItemsDetails wishlistImageUri)
{
    boolean value = false;
    for (int i = 0; i< cartListImageUri.size() ; i++)
    {
        ItemsDetails itemsDetails = cartListImageUri.get(i);
        if (itemsDetails.getItemId().equals(wishlistImageUri.getItemId()))
        {
            value = true;
        }
        else value = false;
    }
    return value;
}
public static void removeCartListImageUri(String foodId) {
    for (int i=0;i<cartListImageUri.size();i++)
    {
        ItemsDetails itemsDetails = cartListImageUri.get(i);
        if (itemsDetails.getItemId().equals(foodId))
        {
            cartListImageUri.remove(i);
            break;
        }
    }
}
public ArrayList<ItemsDetails> getCartListImageUri(){ return this.cartListImageUri; }

}

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
  • 1
    `public static int type;` - You don't want that to be `static`. You also don't really want the `public static MainActivity mActivity;` right above it at all, but that's likely unrelated to the current issue. Just use `getActivity()` in the `SimpleStringRecyclerViewAdapter` constructor call. – Mike M. Feb 06 '18 at 12:27
  • TL:DR setUserVisibleHint() will help you to know which fragment you are currently in. – CopsOnRoad Feb 06 '18 at 12:29
  • @MikeM. that did not wok for me. – ppreetikaa Feb 06 '18 at 12:34
  • Then I would guess that there are further issues in `ImageUrlUtils`, but I couldn't tell you what. – Mike M. Feb 06 '18 at 12:37
  • Sadly your issue is what makes the view pager swipe so smoothly as it is its default property to be ready with previous or next view see link https://stackoverflow.com/a/34609359/9287163 – krishank Tripathi Feb 06 '18 at 12:39
  • @MikeM. ImageUrlUtils class returns the ArrayList of Model class – ppreetikaa Feb 06 '18 at 12:39
  • @krishankTripathi where is the link – ppreetikaa Feb 06 '18 at 12:40
  • Right. But if it's that data that's incorrect for the current `Fragment`, then we'd have to see that class to be able to determine any problems there. – Mike M. Feb 06 '18 at 12:41
  • pls see again i updated it. – krishank Tripathi Feb 06 '18 at 12:43
  • @MikeM. i have upadated the ImageUrlUtils in the question – ppreetikaa Feb 06 '18 at 13:00
  • `itemsDetails` is `static` there, as well, so all of your `Fragment`s are essentially using the same `ArrayList`. When the `ViewPager` loads the next off-screen `Fragment`, that list is then populated with that `Fragment`'s data, so you see it in the current one, too. The simplest solution would be to just keep separate lists for each. – Mike M. Feb 06 '18 at 13:11
  • @MikeM. Thank you so much that worked for me. But i m curious to know that what is the difference between using the same ArrayList and different ArrayList for the Fragment. – ppreetikaa Feb 07 '18 at 04:38
  • The way you had your code setup originally, all of the `Fragment`s were using the one same list, the same single object. As others mentioned, `ViewPager` loads pages one ahead, so the next `Fragment` is ready to be swiped on-screen. After the first `Fragment` set the list data, the second one set its own data in that same list, clearing out the first's, so when the first `RecyclerView` went to grab items from its `Adapter`, the list was already filled with the second's data. Then, when you swiped, the same thing would happen with the second and third `Fragment`s, and so on. You follow me? – Mike M. Feb 07 '18 at 04:58
  • 1
    @MikeM. yes I got it. Once again thank you so much – ppreetikaa Feb 07 '18 at 05:01

1 Answers1

0

ViewPager is intented to work in that way to avoid lag in animations while switching from one fragment to other fragment, it will load atleast one extra fragment alongside your Visible Fragment by which it makes sure there is scope to swipe.

because, while swiping view pager has capability to show two fragments simultaneously hence loads the one extra always, without two pages view pager is useless.