Is ViewPager really what I need for this scenario? - android

APPLICATION IDEA: Idea is simple. When application is launched it will connect to web service which will return number of rows in particular column in database, then it will retrive data from first row and display in activity. When user interacts with its finger, left or right (e.g swiping through pages), it needs to load next (or previous) row from database.
Please check following picture you may understand better.
PROBLEM: Since I'm new in Android I would like to know which scenario best fits my needs. I actually need to load data from database when user swipes and show this on my current screen.
WHAT I'VE TRIED: I already made my "homework" and I searched google for my problem. I used implementation of ViewPager class with PagerAdapter class. But when I swipe on the next page nothing is showed ( Yes I used AsynTask and I sucessfully pulled data to my device), but instead is showed on the NEXT page.
So I need to know if ViewPager is really what I need? Do I need to use ViewPager together with FragmentPagerAdapter so that my text will load exactly when my page is showed?

ViewPager suits this scenario well.
Note that ViewPager by default loads the next and previous pages while the current page is being shown (off-screen page limit 1). Your adapter likely has some bugs that make it pull data for the wrong page. To get help with them, please post a more specific question.

Related

android -Need some suggestion for making a viewpager for very big datas

I want to make a leitner application . the data is coming from database .
For instance , I've about 500 rows in my database and I want to make a viewpager for showing them , each row comes on one page so I'll have 500 pages in the viewPager .
The question is , What is the best way to implement something like this ? is it going to have memory problems or something ?
What are the better ways to do so ? if no other way ,What type of viewPager is better to use for database? A fragment viewAdapter for example
For this you should use a normal ViewPager with FragmentStatePagerAdapter.
As described in documentation
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to much
less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.

can it loads content after the tab is clicked? not creating the spec in one go

Now I want to create a TabHost for 3 categories, Pizza, Spaghetti and Snack. Information will be taken form database. Is it possible to access only information of that specific category after the tab is clicked instead of creating it when the activity starts. If it is possible, which way is better.
I made something like you over a week ago and after long debugging I realized that the system loads the present tab and the one after it at the same time in case to be ready for swiping anytime you wanna do it (I mean their layouts). So if you place your database logic on the Fragments not on your Activity directly (different database logic on the different tabs), when you load your app, only the first tab database logic will be loaded, but the second tabs layout will be loaded too. Hopefully you could understand what I meant :)

How to check Viewpager item ids with Espresso?

I have a Viewpager that's composed of copies of the same fragment view. You can swipe between them. I'm writing an Espresso test and trying to assert on ids of each page, but they are obviously ambiguous because there are multiple pages loaded and they all share the same ids. I don't want to change the view pager to load only 1 page at a time. In my application, it's inefficient to do so.
I've also thought of using tags and tag each view with a unique string, but then I'm modifying the source code only so that my test works. I don't like that. What is the correct way of checking content inside the page of a viewpager with Espresso?
Assuming that only one of the pages is displayed at a time you can use swipeLeft/swipeRight to move to a page and then check that the right thing is displayed in that page.
There are some examples here.
e.g.
onView(withId(R.id.pager))
.perform(swipeLeft())
.check(matches(hasDescendant(withText("Position #2"))));

Implement some functionality like in Instagram android app

There are two things which I like on the Instagram for Android app and I'd like to implement them in my app.
1. Infinite go back in history of fragments
If you tap on a user, you can see his details, taping on followers will return a list of followers, pressing on another user will show his details... and so on. Basically you can do this thing for many times BUT when you go back everything is instant without loading. How can this be implemented? My initial thought was to have only one activity with a top actionbar and for the rest use fragments (one fragment for user details, one fragment for users list) and so on. The problem is I can't think of a good way to allow going back in history. The only way I can see is by caching all the data (user data / list adapters) is an ArrayList so when the user presses back, take the last item from the list and instantiate the fragment. Is there a better way of doing it ? I'm thinking I could start a new activity for each user interaction and them when the user presses back, simply finish the current one. My only worry in this case would be running out of memory. Is there a way to cache fragments with their state ?
2.GridView inside ScrollView
On user details there are two main layouts: a layout with user details and a gridview of images. When the user scrolls, at the scroll's end, the gridview gets new set of items (load as you scroll). While I know how to implement load as I scroll for the gridview, I don't know how to add the gridview inside a scrollview and keep listening for scroll events
Haven't got a quick answer for number 2 but for the first question why not just add the fragments to the backstack with FragmentTransaction.addToBackStack ?
That way you get the natural back-action with fragments without having to start new activities for every action.

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