I found the question below: Android: FragmentActivity inside FragmentActivity (ScrollView in NavigationBar) . However, this question of mine is about how to use Transactions to show fragments as well as enabling swipe gesture detection in a safe way. Note: There is a link in the answer that I have posted (This also has an effective way of showing fragments in a container using transactions, including two scenarios). Please see that. I have tried a few things in regards, but by using a fragment supporting ViewPager, not nested:
Details: https://moqups.com/[email protected]/lc0ZOERO/p:a5d7b55eb
Now what I want to achieve is to do the third point above using swipe-gestures for the right [left to right] swipes only.
I have used GestureListener and SimpleOnGestureListener and activity's OnTouchEvent for this.
The problem that I face is:
This gesture works on the portion of the activity screen which is below the fragment and the portion of the activity. I want the gesture work
I tried the following class and change in my activity fragment in layouts like this.
My Gesture and Touch listener extended with frame layout:
public class OnSwipeTouchListener extends FrameLayout {
private final GestureDetector gestureDetector;
private static final String TAG = "OnSwipeTouchListener";
private Context context;
public OnSwipeTouchListener(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
gestureDetector = new GestureDetector(context, new GestureListener());
setClickable(true);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(motionEvent);
return super.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
//see that e1 is null
}
return result;
}
}
public void onSwipeRight() {
///manage my back stack history here.
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
and then change frame layouts to this class reference:
<path.to.my.package.OnSwipeTouchListener
android:id="@+id/activity_frag_frame_layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible" >
<fragment
android:id="@+id/activity_frag1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="path.to.my.package.fragments.Activity_Frag1"
android:tag="act_frag1" />
</path.to.my.package.OnSwipeTouchListener>
It helped me little, so should it help you in finding solution. There is a searchView in one of the fragments that stopped working, and no click works. If you get it what I want to achieve, please help in this regards.
Update 1: Returning true in both onTouchEvent and onInterceptTouchEvent has the desired effect, but it blocks the clicks in fragments where SearchView is there, no clicks work in the clickable views present. Only the swipe is working. Update: In my latest: I have also discarded the use of transaction-back-stacks completely because I rely on my custom back-stack where I maintain the navigation history for all the tabs. This I do using setCurrentTab and onTabSelected methods given by ViewPager and FragmentStatePagerAdapter.
Update 2: This one is difficult to implement: Check which events need to be intercepted and which are to be passed to the child views: http://developer.android.com/training/gestures/viewgroup.html
Update 3:
In my layout for activity, there is a view pager, and in each frame layouts below it, there is one fragment.
When you start the app, the ViewPager shows the fragment in the first tab.
When a button on this fragment is clicked, one of the fragments in the frame layouts below view pager is shown and added to my custom back stack.
When the return key is pressed, this fragment is hidden again to show the tab fragment in the view pager. Every of the above is done. Only I want the swipe gesture for the left to right to work, and I found that I can do it by intercepting touch events. But it does not do that at all When I return true, only the swipe gesture works, otherwise, this action is canceled and the clicks on this fragment work.
I have mentioned this answer in http://mobi-app-dev.blogspot.in/2014/01/android-transactions.html.
Check which events needs to be intercepted and which are to be passed to the child views: http://developer.android.com/training/gestures/viewgroup.html
Instead of using my swipe gesture listener in frame-layout on which fragments are attached, now I am using it on the relative-layouts which are the root-views of the fragments.
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean intercepted = super.onInterceptTouchEvent(event);
// In general, we don't want to intercept touch events. They should be
// handled by the child view.
gestureDetector.onTouchEvent(event);
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean touched = super.onTouchEvent(event);
gestureDetector.onTouchEvent(event);
return touched;
}
Here are some tips that I follow now while using fragments in my activities:
[Note: In case you are using FragmentStatePagerAdapter, you can play with replacing a fragment with another fragment at a particular index in the list of fragment that you are using as a data for this adapter, in a way that you are in the same tab but its content is changed without changing the tab. Then use notifyDataSetChanged(). Here you do not use transactions, but otherwise you do].
Tip : Carefull while adding swipe feature because there are other clickable views. Swipe with intercept touch on RootView returning false but analysing touches.
One more thing: you should observe that having one fragment in one container at a time resolves another issue: If there are two fragments , one on the top of the other, the clicks from one fragment goes to the other fragment that is beneath. So hide or replace to have just one fragment at a time. Back-Stack helps in maintaining the navigation levels, and on configuration change or update, try to update the same instance of the target active fragment, instead of doing transactions because that changes the order of fragments navigated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With