refactor(compose-fab): show fab after small scroll distance

This commit is contained in:
FineFindus 2023-01-25 22:09:10 +01:00
parent cc4483dea1
commit 327426e443
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
4 changed files with 58 additions and 18 deletions

View File

@ -71,7 +71,7 @@ public class AccountTimelineFragment extends StatusListFragment{
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState){ public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
fab = ((ProfileFragment) getParentFragment()).getFab(); // fab = ((ProfileFragment) getParentFragment()).getFab();
} }
@Override @Override

View File

@ -75,7 +75,7 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
protected String accountID; protected String accountID;
protected PhotoViewer currentPhotoViewer; protected PhotoViewer currentPhotoViewer;
protected ImageButton fab; protected ImageButton fab;
protected boolean isScrollingUp = false; protected int scrollDiff = 0;
protected HashMap<String, Account> knownAccounts=new HashMap<>(); protected HashMap<String, Account> knownAccounts=new HashMap<>();
protected HashMap<String, Relationship> relationships=new HashMap<>(); protected HashMap<String, Relationship> relationships=new HashMap<>();
protected Rect tmpRect=new Rect(); protected Rect tmpRect=new Rect();
@ -286,21 +286,19 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
currentPhotoViewer.offsetView(-dx, -dy); currentPhotoViewer.offsetView(-dx, -dy);
if (fab!=null) { if (fab!=null) {
if (dy > 0 ) { if (dy > 0 && fab.getVisibility() == View.VISIBLE) {
if (isScrollingUp) { TranslateAnimation animate = new TranslateAnimation(
fab.setVisibility(View.INVISIBLE); 0,
TranslateAnimation animate = new TranslateAnimation( 0,
0, 0,
0, fab.getHeight() * 2);
0, animate.setDuration(300);
fab.getHeight() * 2); animate.setFillAfter(true);
animate.setDuration(300); fab.startAnimation(animate);
animate.setFillAfter(true); fab.setVisibility(View.INVISIBLE);
fab.startAnimation(animate); scrollDiff = 0;
isScrollingUp = false; } else if (dy < 0 && fab.getVisibility() != View.VISIBLE) {
} if (scrollDiff > 400) {
} else {
if (!isScrollingUp) {
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation( TranslateAnimation animate = new TranslateAnimation(
0, 0,
@ -310,7 +308,9 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
animate.setDuration(300); animate.setDuration(300);
animate.setFillAfter(true); animate.setFillAfter(true);
fab.startAnimation(animate); fab.startAnimation(animate);
isScrollingUp = true; scrollDiff = 0;
} else {
scrollDiff += Math.abs(dy);
} }
} }
} }

View File

@ -400,6 +400,29 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
}); });
scrollView.setOnScrollChangeListener(this::onScrollChanged); scrollView.setOnScrollChangeListener(this::onScrollChanged);
scrollView.setNestedScrollListener((target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed) -> {
if (dyConsumed > 0) {
fab.setVisibility(View.INVISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0,
0,
0,
fab.getHeight() * 2);
animate.setDuration(300);
animate.setFillAfter(true);
fab.startAnimation(animate);
} else {
fab.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0,
0,
fab.getHeight() * 2,
0);
animate.setDuration(300);
animate.setFillAfter(true);
fab.startAnimation(animate);
}
});
titleTransY=getToolbar().getLayoutParams().height; titleTransY=getToolbar().getLayoutParams().height;
if(toolbarTitleView!=null){ if(toolbarTitleView!=null){
toolbarTitleView.setTranslationY(titleTransY); toolbarTitleView.setTranslationY(titleTransY);

View File

@ -10,7 +10,12 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
public class NestedRecyclerScrollView extends CustomScrollView{ public class NestedRecyclerScrollView extends CustomScrollView{
public interface NestedScrollListener{
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed);
}
private Supplier<RecyclerView> scrollableChildSupplier; private Supplier<RecyclerView> scrollableChildSupplier;
private NestedScrollListener nestedScrollListener;
public NestedRecyclerScrollView(Context context){ public NestedRecyclerScrollView(Context context){
super(context); super(context);
@ -24,6 +29,18 @@ public class NestedRecyclerScrollView extends CustomScrollView{
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
} }
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (nestedScrollListener != null) {
nestedScrollListener.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
}
}
public void setNestedScrollListener(NestedScrollListener listener) {
this.nestedScrollListener = listener;
}
@Override @Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
final RecyclerView rv = (RecyclerView) target; final RecyclerView rv = (RecyclerView) target;