How to slide images with Viewpager in Android? - android

I want to slide images using Viewpager, and a number of pictures should be used. (like 30)
When you swipe the screen, The images must be changed. (So you should use Viewpager.)
of course i can do this by using 30 fragments for the images,
but that's not what i wanted.
i want to use only one fragment.
I've been searching it for a long time and the only thing that i know is, getArguments() and setArguments() would be helpful,
but still have no idea how to do it..
Please let me know.
*not good at English tho..

Use ViewPager2. Undercover it's contain RecyclerView that allows you to reuse fragments that goes off the screen.

Related

What is the best way to display multiple ImageViews in a fragment?

The images in my fragment are slow to show? or load?
I have a constraint layout that displays up to about 40 various ImageViews. All videos are shown simultaneously. When I navigate to the fragment it can take up to a second or two for the navigation to end.
The images are in my project as drawables and programmatically found by their drawable id and displayed programmatically, not in a list.
How can I do this better for a better experience?
While using RecyclerView with GridLayoutManager would make your life really easy, If you are really not willing then here's a few suggestions:
I am assuming by referring to time taken for navigation you are saying that the actual transition from a fragment/activity to this particular fragment happens really slowly. So for most of the time you are staring at a blank screen.
i) first, place some blank placeholder images to your ImageViews. So that the user at least knows that the app hasn't stopped responding.
ii) secondly, if you are using any of the onCreate(), onCreateView(), onViewCreated() methods to initialize your ImageViews programmatically, I suggest you move this code to onStart() method of your fragment.
iii) lastly, use Glide library to asynchronously load your ImageViews with the images.

ViewPager or RecyclerView with fragments?

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 ?
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

Avoiding the default loading of fragments in a view pager

i am new to android. i noticed that on implementing the adapter of the view pager the getItem() will result in returning two adjacent fragments. How can i skip this default loading and load only the fragment in the current page. Please help me.
You should not do that. Please note that when swiping to next page two fragments are visible at the same time so they have both be created.
You can use ViewPager.setOffscreenPageLimit(1) for this. I've used that to keep all my three fragments alive. I Don't know what happens if you set it to 1.
Confusing, this is from the documentation. It says this value defaults to 1 but when I was working on that I got always two fragments like you do:
You should keep this limit low, especially if your pages have complex
layouts. This setting defaults to 1.

Android : ViewFlipper Or ViewPager - Which is the better option?

I am getting stuck in one stage. I have a total of 20 to 25 images that should get animated like ViewPager does. Now on all the images I have onClick() events and I don't know if I should work with ViewPager or ViewFlipper. I can implement both things, no issues here.
What I Want : I just want a suggestion that according to my problem which will be the best option, ViewFlipper or ViewPager?
What I Have Searched : I have gone through different links on StackOverflow, namely How to improve the performance of ViewFlipper/ViewAnimator and ViewFlipper vs Fragments, but I could not find the thing I want.
I have worked with ViewPager somewhat, but at that time there were just 3 or 4 Fragments I had to manage. If I were to use it to solve this problem I have to manage 25 Fragments this time. So I am wondering if there is a better solution available.
I have also done a little R & D on ViewFliper and know that it has only one Activity I have to manage but it does not have the animation accuracy that ViewPager has.
Please suggest me whether I should go for ViewPager or ViewFlipper?
Thanks in Advance.
Update (from 5+ years old original post):
Use ViewPager2 from JetPack. That's it.
I left original answer below but just for reference. Please do not use very old and not maintained widgets (like Gallery and ViewFlipper any more):
Intro: On first thought, I would recommend Gallery widget instead of ViewFlipper but it is depreciated since JellyBean (API 16), probably because of bad recycling of non-visible elements implementation.
Answer: What u really should use now is ViewPager or HorizontalScrollView. In your case (despite not too much given details) I think is better ViewPager because according to documentation:
.. HorizontalScrollView is a FrameLayout, meaning you should place one
child in it, containing the entire contents to scroll ..
On the other side for ViewPager you should implement PagerAdapter to generate pages which will be shown in this view.
My final answer is A, ViewPager ;)
Hope u will find this helpful ;) Cheers

Android Viewpager with asynctasks (spinner while page is loading)

I want a Viewpager that shows loading while content is coming in from the background. Basically I expect the first View to be loaded, but View+1 and View-1 will still be loading. If the user swipes to either side I want them to be presented with a spinning dialog while it loads
Would I just add AsyncTasks into the ViewPager with some conditions determining when they will run? I dont want too many AsyncTasks to be loading as the viewpager will have many views off to the sides.
I think the Trulia app does this, it is similar to what I am looking for. Apartment image viewing shows a loading screen while the images are loading in that viewpage.
Also for the record, can I just treat viewpagers like onCreate functions of an activity? That would really clear things up
Insight appreciated
Have a look at the supplied FragmentPagerAdapter if you want to perform more Activity-like lifecycle management of each page.

Categories

Resources