-1

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?

Stormbuster
  • 76
  • 2
  • 12
  • 1
    If 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 Answers3

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
0

You need to update textView (either in separate fragment or in the same activity) inside onSwipeLeft or onSwipeRight.

To have these callbacks you need to create your custom listener that extends the TouchListener.

Great example of doing this is here.

Community
  • 1
  • 1
Ivan V
  • 3,024
  • 4
  • 26
  • 36