theanimashaun
theanimashaun

Reputation: 306

How to set View Pager for each item of a List

I have a List of items in an android app , and i want each item to be on a page . the problem is each item sets a layout with an imageview which downloads its image from a unique url. Is this possible to implement and how please ?

Help is needed (In any Form)

Upvotes: 1

Views: 864

Answers (1)

2Dee
2Dee

Reputation: 8629

Each page should be a Fragment, depending on your item object, you may want to implement Parcelable and pass your object as an argument to your Fragment, like so :

in your FragmentPagerAdapter :

@Override
public Fragment getItem(int position) { 
    PageFragment fragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putParcelable("ARGUMENT_KEY_NAME", itemObjectsList.get(position));
    fragment.setArguments(bundle);
    return fragment;
}

and in your Fragment's code, read your object like so :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getArguments();
    Item item = bundle.getParcelable("ARGUMENT_KEY_NAME");
    // do things with your Item object
}

Alternatively, if your item object is too large or if for any other reason you can't easily pass the whole object in a Bundle, you could just pass in the relevant info for the Fragment to be created, i.e. text and image urls etc ...

Upvotes: 1

Related Questions