What I would like to achieve: Have a ViewPager with TabLayout where each Tab contains a new Fragment that has a RecyclerView layout out horizontally and managed by a FragmentPagerAdapter.
What I am struggling with: The default behaviour is that when you get end of the list on one of the tabs, it switches Tabs and snaps (snapping means if you pull over 50% of the viewport, it jumps to either direction). I want it to free-flow and not to snap. Sort of like how it happens in iOS by default.
I got inspired for this setup by the following two CodePath guide:
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter
How should I approach this problem (preferably without any other libraries)?
What I would do is use HorizontalScrollView instead of a ViewPager. The only caveat is that the view pager sends the lifecycle events to fragments as you scroll between them, whereas for HorizontalScrollView you will have to add them initially, and then they will be continuously active unless you manually change that. Also you may have an option to not use the fragments and use simple views depending on what you use the fragments for.
I would definitely not override the touch events for the ViewPager because it does some cleanup when it detects the UP motion, so just swallowing it is not a correct thing to do.
Related
I'm writing an app with multiple tabs. Every tab is a fragment and every tab uses the same layout file. You can change to the next tab by swiping (handled by a SwipeAdapter). Inflating the same layout for every tab again is quite inefficient and makes the app lag. Is there a way to inflate the layout once in the beginning and to reuse the inflated layout for all tabs?
Or alternatively, is there a way to do so with e.g. three inflated layouts? (-> one for each the currently displayed tab and both neighbours, so when you swipe from tab 4 to 5, the inflated layout of tab 3 - which is not needed anymore - is used for tab 6)
I know you can reuse inflated views with a listview, but it wouldn't work here because I want the user to swipe instead of continuous scrolling.
Welcome to StackOverflow. Use RecyclerView with a SnapHelper (works both horizontally and vertically). If you Google it you will find many tutorials.
Alternatively you can use ViewPager2, which is in alpha state (if you can wait for it to be stable). It is based in RecyclerView, and is an update to the old ViewPager to bring it to 2019. See this tutorial for example Hands on With ViewPager2. See also ViewPager2 releases page at Google to keep track of development progress.
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
So I have a single activity that controls a viewPager and this viewPager controls five fragments. In one of the fragments, there is an ImageView.
I want this imageView transit to the next page when I do a swipe left. More specifically, the position of this imageView will transit as I am swiping and once the swiping is done and the next fragment layout is completely shown on the screen, this ImageView should be a fixed location in this layout.
Does anybody know how I can make this work?
Thanks!
I think I know what you mean. It might be easier to make it a separate View on top of the pager, perhaps disabled so drags will go through it. and then you need to get a live reading of the position of the page. onPageScrolled on ViewPager.OnPageChangeListener is pretty cool and gives you what you need to place elements or do other transitionary effects based on the actual position of the Views in your ViewPager.
I want to swipe between fragments, and I am currently using the viewpager.
There is one problem though: When I swipe to the right, entries made in EditTexts of the current
fragment need to be validated, and only if the input is ok, it should be swiped to the next page.
I managed to do that by disabling the viewpager when necessary, however then I also cannot swipe to the left (i.e. the previous fragment), since the method to validate the input (onPageScrolled(...)) is not called anymore due to the fact that the viewpager is disabled. It is only enabled again when I type something in the EditText.
I haven't found a way to deal with that problem yet.
So I'd like to know if there is an alternative to using fragments with swipe gestures? Maybe in combination with the viewpager's setCurrentItem() method, but without using the viewpagers scrolling-animation?
I have seen lots of examples of how to use a ViewPager to scan through multiple instances of the same Fragment. I don't want to do that. I want to have a single instance of a Fragment that runs off the edge of a screen and use a ViewPager (or at least the nice clean ViewPager swiping effect) to scan through the different sections of the single fragment. Is this possible?
Is this possible?
You can:
Use nested fragments for the pages in ViewPager, or
Use views for the pages in ViewPager, such as in this sample app, or
Use HorizontalScrollView instead of ViewPager to wrap around a single wide view