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/abhsax130778@gmail.com/lc0ZOERO/p:a5d7b55eb
- Disabled default swipe using onTouchEvent and onInterceptTouchEvent using custom ViewPager.
- Declared FragmentStatePagerAdapter provided with a list of fragments, each of which is displayed in each tab.
- Declare Fragments in the main_layout of the main_activity which has this ViewPager alongside. Idea is to show the activity_fragments when a button on a ViewPager_fragment is clicked and when the user presses the return button, then the default ViewPager_fragment is shown again using the back stack transactions on add and popping them on BackPressed of the activity. I am therefore also maintaining my custom back-stack to show/hide the activity_fragments when the back stack pops a transaction.
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
- On the Fragment area which has multiple views in its layout.
- In only the left-right direction.
- Only from activity_fragment to ViewPager's tab fragment to behave like history navigation as already done in onBakPressed/onKeyDown implementation using back stack popups and my custom back stack.
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.