1

I am a little bit confused on how should I approach this particular case of doing some swipes between fragments.

So yea, I asked ViewPager or RecyclerView, because these 2 are my only options, if anyone can come up with a better idea, it is really welcome.

The flow is the following, I have a Main Timeline(ListView), each item of it opens a fragment with details about it. What I would actually want to do is to swipe between these full screen fragments without going back to MTL and open another item of the list.

You would ask me what I tried, well: RecyclerView - HORIZONTALLY oriented as a root of the fragment, and each item of this RV had the details of each event. The problem with this is that it gets really buggy because I have a huge logic inside each item(like, another RV - horizontally , a PagerView also horizontally to swipe between images (or a youtube frame that is being played if is the case. Plus a lot of other stuff in this, so the logic of parent RV inside the onBindViewHolder() is really tricky.

Would be better to use a PagerView with fragments(since I have the DetailsFragment kind of ready) ? The problem here is that I need a certain number of swipes, right ?Whole Fragment

Ionut J. Bejan
  • 734
  • 11
  • 28

1 Answers1

2

Go with viewpager.

Because creating fragments inside recyclerview causes recyclerview performs to slow down.Also to create fragments in onBindViewHolder() dynamically every time you need different unique id of frame layout to load which will be tough to generate.

For more information on why recycler view is bad idea to load fragments check this.

Fragment replacing in RecyclerView item

Also try to use the ViewPager with an implementation of FragmentStatePagerAdapter. The adapter will optimize the memory usage by destroying fragments that are not visible at a given moment.

Check the documentation for details and code sample.

https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Yes I know, already kind of did this and was so buggy. But as I said, the problem with viewpager would be the number of swipes. Firstly I have 20 items in MTL, but there are way much more(when I'm at the bottom of it I request more from server) – Ionut J. Bejan Sep 28 '17 at 09:11
  • yes I know..because previously I had tried to use fragments insided recyclerview in one of the project.It is very buggy and tough to use. As I said try to use FragmentStatePagerAdapter instead of FragmentPagerAdapter as it will hep to improve performance. – Sharath kumar Sep 28 '17 at 09:14
  • Thanks for your answer. One more question, what about my inside swiping ? Because it's the ViewPager swipe(which is horizontally) and inside this I will have another viewPager + a RV (both also horizontally). – Ionut J. Bejan Sep 28 '17 at 09:23
  • what is inside swiping ?Also you can directly show fragment inside viewpager no?again why a viewPager + a RV? – Sharath kumar Sep 28 '17 at 09:29
  • I uploaded a screenshot with the full fragment. Under similar events I have a RV horizontally and on top where is the image, I have a PagerView to swipe between those pictures(2 pics in the above case) – Ionut J. Bejan Sep 28 '17 at 09:39
  • main swipe should be recycler view and the second one i.e similar posts should be horizontal recycler view – Sharath kumar Sep 28 '17 at 09:43
  • because that contains just an image and a name which can be used inside a layout..onclick you can call the main viewpager with refreshed data – Sharath kumar Sep 28 '17 at 09:44
  • By main swipe you mean the swipe of whole fragment ? which we decided that it has to be PagerView? – Ionut J. Bejan Sep 28 '17 at 09:46
  • ya make it simple..I think you can create a single fragment and many instance of that as viewpager is swiped...keep one main viewpager and a fragment..onswipe refresh its data..also similar items should be a horizontal recylerview whose layout should be inside the fragment. – Sharath kumar Sep 28 '17 at 09:52
  • It is already like this, we kind of misunderstood each other a bit. Indeed the fragment is always the same(layout doesn't change) what change is it's data. But I was asking if there would be any problem of the swipe. On my previous implementation with RecyclerView as the root, my inside elements(images & similar events) were not swiped. – Ionut J. Bejan Sep 28 '17 at 09:54
  • I kind of have 2 swipes on each pagerview fragment to be more clear – Ionut J. Bejan Sep 28 '17 at 09:54
  • ohk...if UI is same then you can use singl fragment and keep refreshing as user swipes – Sharath kumar Sep 28 '17 at 09:55
  • Great, thanks a lot. I will mark this as right answer :) Really apreciated – Ionut J. Bejan Sep 28 '17 at 10:04
  • Thanks a lot..happy that this cleared your confusion and helped you :) – Sharath kumar Sep 28 '17 at 10:05
  • I came back with a slight misunderstood of what's going on with this implementation. I understood that `PagerView` loads next page (by default), but somehow, my data for the events for the visible fragment is taken from next fragment...How could I handle this? – Ionut J. Bejan Sep 29 '17 at 10:52
  • I dint understand your question..can u explain? – Sharath kumar Oct 01 '17 at 14:20
  • 'onInstantiateItem' I take my data based on position. But, last data saved on this variable is the one from next page not the current/visible one.. – Ionut J. Bejan Oct 01 '17 at 14:22
  • not visible in a sense?you have to save the data as member variable inside your pager adapter class – Sharath kumar Oct 01 '17 at 14:30
  • Maybe in the constructor of it. I see, that might help me. The biggest problem is not this, is that I have youtube video fragments playing in each page...and it fails each time for next fragment (because of the overlay problem). Anyway, thanks again for your answer – Ionut J. Bejan Oct 02 '17 at 07:42