Skip to content

Commit f7d1fab

Browse files
authored
Fixes commons-app#2252 "Tapping on map doesn't close tab in 'nearby'". (commons-app#4265)
* closing the list on tap ouside * minor improvement * minor changes * javadoc * official javadoc * javadoc error
1 parent 88be88f commit f7d1fab

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Bundle;
1111
import android.os.Parcelable;
1212
import android.view.LayoutInflater;
13+
import android.view.MotionEvent;
1314
import android.view.View;
1415
import android.view.ViewGroup;
1516
import android.view.animation.Animation;
@@ -24,6 +25,7 @@
2425
import androidx.recyclerview.widget.RecyclerView;
2526
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver;
2627
import androidx.recyclerview.widget.RecyclerView.ItemAnimator;
28+
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
2729
import androidx.recyclerview.widget.SimpleItemAnimator;
2830
import butterknife.BindView;
2931
import butterknife.ButterKnife;
@@ -156,6 +158,50 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
156158
}
157159
}
158160
});
161+
162+
//Fab close on touch outside (Scrolling or taping on item triggers this action).
163+
rvContributionsList.addOnItemTouchListener(new OnItemTouchListener() {
164+
165+
/**
166+
* Silently observe and/or take over touch events sent to the RecyclerView before
167+
* they are handled by either the RecyclerView itself or its child views.
168+
*/
169+
@Override
170+
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
171+
if (e.getAction() == MotionEvent.ACTION_DOWN) {
172+
if (isFabOpen) {
173+
animateFAB(isFabOpen);
174+
}
175+
}
176+
return false;
177+
}
178+
179+
/**
180+
* Process a touch event as part of a gesture that was claimed by returning true
181+
* from a previous call to {@link #onInterceptTouchEvent}.
182+
*
183+
* @param rv
184+
* @param e MotionEvent describing the touch event. All coordinates are in the
185+
* RecyclerView's coordinate system.
186+
*/
187+
@Override
188+
public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
189+
//required abstract method DO NOT DELETE
190+
}
191+
192+
/**
193+
* Called when a child of RecyclerView does not want RecyclerView and its ancestors
194+
* to intercept touch events with {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}.
195+
*
196+
* @param disallowIntercept True if the child does not want the parent to intercept
197+
* touch events.
198+
*/
199+
@Override
200+
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
201+
//required abstract method DO NOT DELETE
202+
}
203+
204+
});
159205
}
160206

161207
private int getSpanCount(final int orientation) {

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static fr.free.nrw.commons.wikidata.WikidataConstants.PLACE_OBJECT;
99

1010
import android.Manifest;
11+
import android.annotation.SuppressLint;
1112
import android.content.BroadcastReceiver;
1213
import android.content.Context;
1314
import android.content.Intent;
@@ -28,6 +29,7 @@
2829
import android.view.MenuInflater;
2930
import android.view.MenuItem;
3031
import android.view.MenuItem.OnMenuItemClickListener;
32+
import android.view.MotionEvent;
3133
import android.view.View;
3234
import android.view.ViewGroup;
3335
import android.view.animation.Animation;
@@ -445,14 +447,41 @@ private void initViews() {
445447
}
446448

447449
/**
448-
* Creates bottom sheet behaviours from bottom sheets, sets initial states and visibility
450+
* a) Creates bottom sheet behaviours from bottom sheets, sets initial states and visibility
451+
* b) Gets the touch event on the map to perform following actions:
452+
* if fab is open then close fab.
453+
* if bottom sheet details are expanded then collapse bottom sheet details.
454+
* if bottom sheet details are collapsed then hide the bottom sheet details.
455+
* if listBottomSheet is open then hide the list bottom sheet.
449456
*/
457+
@SuppressLint("ClickableViewAccessibility")
450458
private void initBottomSheets() {
451459
bottomSheetListBehavior = BottomSheetBehavior.from(rlBottomSheet);
452460
bottomSheetDetailsBehavior = BottomSheetBehavior.from(bottomSheetDetails);
453461
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
454462
bottomSheetDetails.setVisibility(View.VISIBLE);
455463
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
464+
465+
mapView.setOnTouchListener((v, event) -> {
466+
467+
// Motion event is triggered two times on a touch event, one as ACTION_UP
468+
// and other as ACTION_DOWN, we only want one trigger per touch event.
469+
470+
if(event.getAction() == MotionEvent.ACTION_DOWN) {
471+
if (isFABsExpanded) {
472+
collapseFABs(true);
473+
} else if (bottomSheetDetailsBehavior.getState()
474+
== BottomSheetBehavior.STATE_EXPANDED) {
475+
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
476+
} else if (bottomSheetDetailsBehavior.getState()
477+
== BottomSheetBehavior.STATE_COLLAPSED) {
478+
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
479+
} else if (isListBottomSheetExpanded()) {
480+
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
481+
}
482+
}
483+
return false;
484+
});
456485
}
457486

458487
public void initNearbyFilter() {

0 commit comments

Comments
 (0)