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

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.

Related

How to slide images with Viewpager in 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.

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

screenshot of all Views in ViewPager

I have a ViewPager that consist of List of ScrollViews. I want to make a screenshot from all of the Scrollviews after the first load of the ViewPager.
After ViewPager is created, i am going through the Views and creating bitmaps, but i get a screenshot only for the first pages that are loaded offscreen.
If i use the command:
pager.setOffscreenPageLimit(totalPages);
than I am able to get all of the screenshots. However this approach is memory consuming and my app crashes with outOfMemory when the ViewPager consists of a lot of pages.
Is there a way to create screenshot of all the views even the ones that are not preloaded - offScreen?
If you don't set offScreenPageLimit(totalPages), ViewPager will initially only load 1 more page other than you see. That means other pages will not be even created until you swipe there. That also means you cannot take screenshot of a screen/layout that is not created/inflated yet. And yes, it is not a good practice memorywise to set offscreen page limit to a number that ViewPager loads all pages at start.
Answer: No, if you mind your memory consumption. Otherwise, yes.

Multiple instances of an Activity - is this bad practice?

I am creating an app which will have multiple 'grids' containing a title and image thumbnails in each grid square.
Each Grid will have different content stored in it.
I have so far created one activity that initialises an instance of GridView, and uses a custom GridAdapter. (See photo for what it currently looks like) I was planning to swipe left to create a new empty grid in which the user can upload content. There may be anywhere up to 50 grids.
I'm just learning how to implement the gesture, and how to create a new instance of the activity, but from what I've read, I am thinking I have designed it badly.
I was planning for each grid to be an Activity (each takes up the full screen).
I envisaged an Activity as being like a Class in java that you can create instances from a blueprint. I thought if I created one 'Grid' I could create a new instance of it each time. Fragments didn't seem appropriate at the time, as the android tutorials often described them as being purposed to add components to activities.
I'm starting to think though that I am using the wrong methodology here and I need to change it? Can someone guide me in the right direction? I have written all the code already - if I need to change it, do Fragments and Activities share any methods, meaning I can retain some work?
Like you mentioned, using activities for holding content in your use case where switching may be triggered using gestures will definitely be resource heavy and cumbersome. Since, you mentioned swipe gestures, I believe fragments would be much lightweight in this situation. In fact, I would suggest you even look at ViewPager which even recycles fragments for you and optimizes user experience by loading the next fragment for a smoother experience. It will also handle swipe gestures for you!
[UPDATE]
Based on your updated explanation of the user flow, I'm certain that the ViewPager would fare as a better option mainly because it allows for a much better control and user navigation. It will also take care of handling swipe gestures and memory issues that come with these types of flows. Moreover, it will even allow for a page titles and bottom tab indicators in case you need them.
It will require each of its pages to be a fragment (your ViewPager will itself reside in an Activity). Once the user clicks on a grid cell, you can show a dialog window from where user input can be captured. This setup should be optimal for you resource wise in my opinion.

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