From ed62ad321be5436246aa05eb7d5911f83eea5ac5 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 17 Nov 2020 00:00:09 +0100 Subject: [PATCH] Add support for swiping between groups in main view --- .../protect/card_locker/MainActivity.java | 87 ++++++++++++++++++- app/src/main/res/layout/content_main.xml | 8 +- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 60547508a..c9a20cc0e 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -17,9 +17,11 @@ import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.view.ContextMenu; +import android.view.GestureDetector; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; @@ -35,12 +37,13 @@ import java.util.List; import java.util.Map; import protect.card_locker.preferences.SettingsActivity; -public class MainActivity extends AppCompatActivity +public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { private static final String TAG = "Catima"; private static final int MAIN_REQUEST_CODE = 1; private Menu menu; + private GestureDetector gestureDetector; protected String filter = ""; protected int selectedTab = 0; @@ -72,6 +75,23 @@ public class MainActivity extends AppCompatActivity } }); + + gestureDetector = new GestureDetector(this, this); + + View.OnTouchListener gestureTouchListener = new View.OnTouchListener() { + @Override + public boolean onTouch(final View v, final MotionEvent event){ + return gestureDetector.onTouchEvent(event); + } + }; + + final View helpText = findViewById(R.id.helpText); + final View noMatchingCardsText = findViewById(R.id.noMatchingCardsText); + final View list = findViewById(R.id.list); + + helpText.setOnTouchListener(gestureTouchListener); + noMatchingCardsText.setOnTouchListener(gestureTouchListener); + list.setOnTouchListener(gestureTouchListener); } @Override @@ -486,4 +506,69 @@ public class MainActivity extends AppCompatActivity int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK; return (currentNightMode == Configuration.UI_MODE_NIGHT_YES); } + + @Override + public boolean onDown(MotionEvent e) { + return false; + } + + @Override + public void onShowPress(MotionEvent e) { + + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + return false; + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + return false; + } + + @Override + public void onLongPress(MotionEvent e) { + + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + Log.d(TAG, "On fling"); + + TabLayout groupsTabLayout = findViewById(R.id.groups); + if (groupsTabLayout.getTabCount() < 2) { + return false; + } + + Integer currentTab = groupsTabLayout.getSelectedTabPosition(); + + // Swipe right + if (velocityX < -150) { + Integer nextTab = currentTab + 1; + + if (nextTab == groupsTabLayout.getTabCount()) { + groupsTabLayout.selectTab(groupsTabLayout.getTabAt(0)); + } else { + groupsTabLayout.selectTab(groupsTabLayout.getTabAt(nextTab)); + } + + return true; + } + + // Swipe left + if (velocityX > 150) { + Integer nextTab = currentTab - 1; + + if (nextTab < 0) { + groupsTabLayout.selectTab(groupsTabLayout.getTabAt(groupsTabLayout.getTabCount() - 1)); + } else { + groupsTabLayout.selectTab(groupsTabLayout.getTabAt(nextTab)); + } + + return true; + } + + return false; + } } \ No newline at end of file diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 71eea4380..46422b10f 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -13,7 +13,8 @@ style="@style/AppTheme.TextView.NoData" android:id="@+id/helpText" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="match_parent" + android:gravity="center" android:text="@string/noGiftCards" android:visibility="gone"/> @@ -21,13 +22,14 @@ style="@style/AppTheme.TextView.NoData" android:id="@+id/noMatchingCardsText" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="match_parent" + android:gravity="center" android:text="@string/noMatchingGiftCards" android:visibility="gone"/>