From c6dd0097d1e6f59b0c82b36b103e7ccdafb1f85d Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 20 Apr 2019 10:41:51 +0300 Subject: [PATCH] drop dummy bottom-list entries and intercept draw up/down to hide/show the Add FAB --- .../ui/MobileLedgerListFragment.java | 49 ++++++++++++++++++- .../AccountSummaryAdapter.java | 2 +- .../AccountSummaryFragment.java | 12 ++--- .../ui/activity/MainActivity.java | 3 ++ .../TransactionListAdapter.java | 2 +- .../TransactionListFragment.java | 10 +--- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java index 093aff39..654bd4db 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java @@ -17,19 +17,23 @@ package net.ktnx.mobileledger.ui; +import android.view.MotionEvent; + import net.ktnx.mobileledger.ui.activity.MainActivity; import net.ktnx.mobileledger.ui.transaction_list.TransactionListAdapter; import net.ktnx.mobileledger.utils.Colors; +import net.ktnx.mobileledger.utils.DimensionUtils; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; public class MobileLedgerListFragment extends Fragment { - protected MainActivity mActivity; public SwipeRefreshLayout swiper; - protected RecyclerView root; public TransactionListAdapter modelAdapter; + protected MainActivity mActivity; + protected RecyclerView root; protected void themeChanged(Integer counter) { swiper.setColorSchemeColors(Colors.primary); } @@ -38,4 +42,45 @@ public class MobileLedgerListFragment extends Fragment { if (swiper == null) return; swiper.setRefreshing(isRunning); } + protected void manageFabOnScroll() { + int triggerPixels = DimensionUtils.dp2px(mActivity, 30f); + root.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { + private float upAnchor = -1; + private float downAnchor = -1; + private float lastY; + @Override + public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { + switch (e.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + lastY = upAnchor = downAnchor = e.getAxisValue(MotionEvent.AXIS_Y); + break; + case MotionEvent.ACTION_MOVE: + final float currentY = e.getAxisValue(MotionEvent.AXIS_Y); + if (currentY > lastY) { + // swipe down + upAnchor = lastY; + + mActivity.fabShouldShow(); + } + else { + // swipe up + downAnchor = lastY; + + if (currentY < upAnchor - triggerPixels) mActivity.fabHide(); + } + + lastY = currentY; + + break; + } + return false; + } + @Override + public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { + } + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + } + }); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java index ef912841..3ceccc15 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java @@ -108,7 +108,7 @@ public class AccountSummaryAdapter @Override public int getItemCount() { - return Data.accounts.size() + 1; + return Data.accounts.size(); } public void startSelection() { try (LockHolder lh = Data.accounts.lockForWriting()) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index d75577ff..86f66d8d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -124,15 +124,9 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { // })); mActivity.fabShouldShow(); - root.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (fab != null) { - if (dy < 0) mActivity.fabShouldShow(); - if (dy > 0) fab.hide(); - } - } - }); + + manageFabOnScroll(); + swiper = mActivity.findViewById(R.id.account_swiper); Colors.themeWatch.observe(this, this::themeChanged); swiper.setOnRefreshListener(() -> { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index f934d4d6..8c573210 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -631,6 +631,9 @@ public class MainActivity extends ProfileThemedActivity { MobileLedgerProfile profile = Data.profile.get(); if ((profile != null) && profile.isPostingPermitted()) fab.show(); } + public void fabHide() { + fab.hide(); + } public void navProfilesHeadClicked(View view) { if (profileListExpanded) { collapseProfileList(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java index 1e09ac4b..a15ab0f6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java @@ -126,7 +126,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter 0) fab.hide(); - } - }); + + manageFabOnScroll(); LinearLayoutManager llm = new LinearLayoutManager(mActivity); -- 2.39.5