How to check Viewpager item ids with Espresso? - android

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"))));

Related

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

How to divide activity to two page/layouts as in viewpager?

I have an application with a single page. But I want to create another page for an information from the file that I fill up using background service. I want it look like as swipable tabs. What is the best option to do it ?
I have only 1 Activity and 1 service. Service is just for getting information from web and saving it to file. First page is for controls, second, which I want to add, is for ListView that will read from that file service is filling up (swipable tabs will be the best). I tried to use Fragments, but there is too much to rewrite in order to transfer logic to Fragment.
Your Best option is to use ViewPager and Fragments as the UI codes will be clean and will easily be obtained from internet.
The other option if you are not willing to rewrite the code is to use a Tabbed View and clicking on the tab will hide and show elements in the mainlayout( Will not have animations though)

How to create a scrollable list of items to use with a TabLayout?

I'd like to first note that this is my first ever proper android app I'm making (I've only followed tutorials so far), so please be detailed in your answers and forgive me if I'm spouting nonsense :).
I'm trying to create an app where the main activity is going to be a TabLayout, and in one of the tabs I want to have a fragment that is a list which you can scroll up and down, and also I would like each element in the list to have multiple interactive elements (like two different buttons for example).
My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want. However when following tutorials I found that ListFragments seem to be rather tricky, and I'm not sure if I can make it work with a TabLayout.
I've looked for other methods, but I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments.
So what should I do? Is there a way you could make a ListFragment work with a TabLayout and I'm just being silly, or is there a better way to do this?
My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want
The contents of a ListFragment will not be fragments. It is possible that the contents of a ListFragment will represent model objects for which you plan on showing other fragments elsewhere in your UI.
I'm not sure if I can make it work with a TabLayout
A ListFragment can be used inside or outside of a TabLayout. I would not think that a ListFragment inside of a TabLayout would be particularly more troublesome than any other sort of fragment.
Your ListFragment problems will come from:
I would like each element in the list to have multiple interactive elements (like two different buttons for example).
ListView, which underlies ListFragment, is tricky to use with interactive elements in list rows. As your design sounds rather unusual, you might wish to consider using other UI patterns rather than multiple buttons in list rows. If you insist upon this design, you may find RecyclerView to be a bit easier in the long run, even if it is more challenging at the outset.
I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments
That's the only thing you are ever going to find, in all likelihood. Again, ListFragment is a fragment that contains a ListView. ListView rows are not fragments, whether the ListView is contained in a Fragment or not. The only thing that I have ever heard of that is designed for vertical scrolling of fragments would be the various vertical ViewPager implementations floating around.

Viewpager to read different files?

I want to use a view page to read an indeterminate number of files, i have an activity that creates a person's profile and saves the data, and now i need one that uses ViewPager to access the files with only a swipe, i have the layout created and i can load a single file to it and i have a list of all the files in that directory the only thing i need is to implement that in a viewpager in order to switch between my Profiles, i don't know how to use ViewPager can someone please help me out in this? providing an example code, or a useful link that goes according to my problem?
Example of using ViewPager with fragments
Example of using ViewPager without fragments
You should implement FragmentStatePagerAdapter
which is best for paging across a collection of objects for which the number of pages is undetermined. It destroys fragments as the user navigates to other pages, minimizing memory usage.
Check this tutorial:
http://developer.android.com/training/implementing-navigation/lateral.html

Is ViewPager really what I need for this scenario?

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.

Categories

Resources