I'm trying to do a sort of infinite scroll. Each Fragment contains a scrolling WebView (or ScrollView), and when the user hits the bottom, I'd like to test for continued swiping to reveal the next Fragment below.
The user should not be able to go back, and I'm not interested in caching, so that rules out RecyclerView or ViewPager (modified to scroll vertically). I'm only concerned about managing the current Fragment, loading the next, and removing the last one once it's offscreen.
I want the next Fragment to follow the users touch, and I want the transition to abort if the user lets go below a certain threshold.
FragmentManager allows custom animations but they are non-interactive. I could perhaps write my own ViewGroup where I can manually animate a Fragment into position, and remove the previous Fragment manually, but I'm not sure if this is the best path forward.
Anyone have thoughts/tips on a good approach here?
Related
I have the requirement for what is effectively two activities side by side, allowing the user to scroll from one to the other, and back. I've decided that ViewPager is probably the best way to handle this, but am open to suggestions.
When the activity loads, the left hand view is displayed. I want to indicate to the user that the other view exists, by just scrolling it a small amount to the left and back again (similar to the way it's now common to show that a sliding drawer menu exists).
I've tried a number of things but have yet failed to find a method where both the views scroll smoothly together. Another way of interpretting the requirement is saying I want to programmatically fake a user flicking the page briefly.
Try calling the method fakeDragBy(float)! According to the documentation you need to first call beginFakeDrag() to initiate it and then call endFakeDrag(). This will mantain the ViewPagers default snapping behavior, so once you've revealed the other screen is there you don't have to animate the transition back, simply call endFakeDrag and the ViewPager will correctly snap back to the last screen (As long as you haven't dragged it past the half-way mark).
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
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.
During a swipe from View A to View B, when the user releases in the ViewPager
- I would like the ViewPager to freeze at the current point and to animate back, to View A.
How should I implement that?
I have never done that before, but my guess would be to create your own CustomViewPager and make it do the new features for you.
This is the link to the method of the ViewPager that handles which page is to be displayed next.
I would try to add some kind of delay here for your freeze-effect and tweak the float-values which trigger whether you successfully swipe or pop back.
I've used ViewPager to let the user flip left and right to navigate among pages of a given activity.
Is it possible to programatically control the flow of pages of the ViewPager?
For exemple:
If the user didn't fill a given EditText on the current page, I don't want him to be able to navigate to the next one.
Another case: If the user is currently on page 1 and if hi filled a given EditText with a specific value and the user flip right to left, I want him to go straight to the 5th page instead of the 2nd one.
By "flipping" you mean dragging with a finger or just using the ViewPager for animation?
If it's the former, you'll have to dynamically edit the children of the ViewPager to get the effect you want. You can override onInterceptTouchEvent() to block touch events until a page is completed.
However, I recommend disabling finger-dragging in the ViewPager (perhaps using onInterceptTouchEvent()) and then using setCurrentItem(4, true) to show the animation and navigate to a specific page. The logic you need can be implemented using buttons which will call setCurrentItem(4, true). For example, you can have a "Next->" button that is grayed out unless the form is completely completed. That will be more intuitive then the first option anyway.
Edit: 4 is just whatever child index you want to switch to.