I have an Activity with two Fragments, where only one Fragment is shown at the same time. But the user can swipe between them.
The left Fragment is a MapFragment, and the user is able to scroll and zoom this as normal. But when the user starts a swipe touching the rightmost part of the screen, it will scroll to the other Fragment instead of scrolling the map.
In other words:
if (motionEvent.getX()<myFragment.getWidth()-SOME_VALUE) {
scroll the map
} else {
switch to the other Fragment
}
My question: What is a good value for the constant SOME_VALUE? Are there any system defined constants I can use?
Wild guess: SOME_VALUE should be a percentage of your screenwidth, for example 10%.
Most important is to test this on an actual device. I'm not sure if the user finds your described behaviour logical (I think I wouldn't).
I myself would probably choose a small menu bar at the top of the screen to be able to switch between the fragments. Then your swipe has become very simple without having to choose what to do depending on the position that started the swipe.
Related
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?
This may sound stupid, but can I do that? I have searched a little, and this says "Re-attach a fragment ... causes its view hierarchy to be re-created, attached to the UI, and displayed.", probably meaning that the UI of the fragment is recreated losing states and other things.
The reason why I am trying to do is that I would like to achieve something like the Bottom Sheet of Google Maps. The only title part is showed at the bottom, but we can swipe it up and it becomes a Bottom Sheet. I think when only the title part is showing, it is not actually a Bottom Sheet, as it would be difficult to make exactly that part of a Bottom Sheet to be shown. I think Google Maps has put the title part separately, and is replacing it with a Bottom Sheet whose top part looks just like the title part, when swiping up begins.
So, to achieve similar effect, I thought I would create a cluster of UI as a fragment, and when the user begins swiping up, I place a Bottom Sheet with the peek size is set exactly to the height of the fragment, and move the fragment into the top of the Bottom Sheet, so that it can be swiped up.
But I want to keep everything of the fragment without recreating anything, as if I take the fragment and just move into the fragment instantly. Is that possible, or should I save the states and restore them when the fragment's UI is recreated?
Yes. Consider fragment as View and play with ObjectAnimator or any animation class as per behaviour. (Slide-up animation will do the trick)
Note: Fragment will initialize first even you display 20% of portion over main layout. State will remain same This does answers your question i guess.
Here is similar example for your work around. Reference
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).
Here's what I want to do in an app, hope it's clear:
NOTE: I don't want to have all 3 views on the screen at once!
I have a search window, I have results, and I want to show details about a result item when it's clicked.
On a small screen (phone) it's pretty clear: I show one view at a time.
On a tablet, especially in landscape mode I'd like to show 2 views at a time, means:
[search | results] and when a result item is clicked, show [results | item detail].
My ideas:
Have an Activity "SearchAndResults", when result item is clicked, open another Activity calles ResultsAndDetails. Both Activities would use Fragments, eg SearchFragment, ResultFragment, DetailFragment.
This might be quite easy to implement but doesn't seem very elegant to me, especially because I can't have any animation (let the result view slide to the left, let the detail view slide in from the right)
Have all 3 view in one activity and hide/ show them with layout.setvisibility(). No animations either, and I hate to do too much layout stuff in my code.
A viewpager that shows 2 views, but can't be swiped by the user, only from code?
What's a good way to do this?
I would use Fragments, as in your idea #1. This question seems to be describing the exact same type of layout that you are trying to implement. It has several answers on how to animate fragment transitions, including complete code examples.
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.