I need to show text below image but with horizontal swipe functionality. That is every time a person swipes horizontally image and text is updated. But the problem is I don't know the number of images with text beforehand as they are returned by server. How can I proceed with this?
Asked
Active
Viewed 40 times
-1
-
1If you explain what it has got to do with fragments maybe I would be able to help, but you've provided almost no info. Viewapger uses an adapter. If underlying data of the adapter receives more items you have to notifyDatasetChanged() and that's it... – ssuukk Oct 02 '15 at 09:02
-
I thought to add fragments dynamically. – Stormbuster Oct 02 '15 at 09:03
-
Is there any other way to add fragments besides dynamically? – ssuukk Oct 02 '15 at 11:44
3 Answers
1
Use a ViewPageAdapter
to achieve this functionality. The first step is to obtain the images and the corresponding text from the server. I would prefer to customize the server to return it json array.
With the obtained value from server, write an adapter something similar to below. Here myValues
will be the array where all the values in the server will be stored.
Create a Fragment
(below MyFragment
) with a static method that will return a new instance of the fragment with desired values set.
Now as you swipe, the method getItem(pos)
will be called and a new instance of fragment will be shown to the user.
private class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList<Values> myValues = new ArrayList<Values>();
public MyPagerAdapter(FragmentManager fm,ArrayList<Values> val) {
super(fm);
this.myValues = val;
}
@Override
public Fragment getItem(int pos) {
return MyFragment.newInstance("text","image");
}
@Override
public int getCount() {
return myValues.length();
}
}
}
Hope this helps. :)

Panther
- 8,938
- 3
- 23
- 34
1
use a RecyclerView with Horizontal Scroll.
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setHasFixedSize(true);
recyclerview.setAdapter(adapter);

Syed Arsalan Kazmi
- 949
- 1
- 11
- 17