Scroll fragments to top from the tab bar
This commit is contained in:
@@ -56,7 +56,7 @@ import me.grishka.appkit.utils.BindableViewHolder;
|
||||
import me.grishka.appkit.utils.V;
|
||||
import me.grishka.appkit.views.UsableRecyclerView;
|
||||
|
||||
public abstract class BaseStatusListFragment<T extends DisplayItemsParent> extends BaseRecyclerFragment<T> implements PhotoViewerHost{
|
||||
public abstract class BaseStatusListFragment<T extends DisplayItemsParent> extends BaseRecyclerFragment<T> implements PhotoViewerHost, ScrollableToTop{
|
||||
protected ArrayList<StatusDisplayItem> displayItems=new ArrayList<>();
|
||||
protected DisplayItemsAdapter adapter;
|
||||
protected String accountID;
|
||||
@@ -377,22 +377,7 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
|
||||
Toolbar toolbar=getToolbar();
|
||||
if(toolbar==null)
|
||||
return;
|
||||
toolbar.setOnClickListener(v->{
|
||||
if(list.getChildCount()>0 && list.getChildAdapterPosition(list.getChildAt(0))>10){
|
||||
list.scrollToPosition(0);
|
||||
list.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
|
||||
@Override
|
||||
public boolean onPreDraw(){
|
||||
list.getViewTreeObserver().removeOnPreDrawListener(this);
|
||||
list.scrollBy(0, V.dp(300));
|
||||
list.smoothScrollToPosition(0);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}else{
|
||||
list.smoothScrollToPosition(0);
|
||||
}
|
||||
});
|
||||
toolbar.setOnClickListener(v->scrollToTop());
|
||||
}
|
||||
|
||||
protected int getMainAdapterOffset(){
|
||||
@@ -552,6 +537,24 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
|
||||
return holders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scrollToTop(){
|
||||
if(list.getChildCount()>0 && list.getChildAdapterPosition(list.getChildAt(0))>10){
|
||||
list.scrollToPosition(0);
|
||||
list.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
|
||||
@Override
|
||||
public boolean onPreDraw(){
|
||||
list.getViewTreeObserver().removeOnPreDrawListener(this);
|
||||
list.scrollBy(0, V.dp(300));
|
||||
list.smoothScrollToPosition(0);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}else{
|
||||
list.smoothScrollToPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
protected class DisplayItemsAdapter extends UsableRecyclerView.Adapter<BindableViewHolder<StatusDisplayItem>> implements ImageLoaderRecyclerAdapter{
|
||||
|
||||
public DisplayItemsAdapter(){
|
||||
|
||||
@@ -158,6 +158,11 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
|
||||
|
||||
private void onTabSelected(@IdRes int tab){
|
||||
Fragment newFragment=fragmentForTab(tab);
|
||||
if(tab==currentTab){
|
||||
if(newFragment instanceof ScrollableToTop)
|
||||
((ScrollableToTop) newFragment).scrollToTop();
|
||||
return;
|
||||
}
|
||||
getChildFragmentManager().beginTransaction().hide(fragmentForTab(currentTab)).show(newFragment).commit();
|
||||
if(newFragment instanceof LoaderFragment){
|
||||
LoaderFragment lf=(LoaderFragment) newFragment;
|
||||
|
||||
@@ -83,7 +83,7 @@ import me.grishka.appkit.imageloader.requests.UrlImageLoaderRequest;
|
||||
import me.grishka.appkit.utils.CubicBezierInterpolator;
|
||||
import me.grishka.appkit.utils.V;
|
||||
|
||||
public class ProfileFragment extends LoaderFragment implements OnBackPressedListener{
|
||||
public class ProfileFragment extends LoaderFragment implements OnBackPressedListener, ScrollableToTop{
|
||||
private static final int AVATAR_RESULT=722;
|
||||
private static final int COVER_RESULT=343;
|
||||
|
||||
@@ -430,10 +430,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
|
||||
toolbarTitleView.setTranslationY(titleTransY);
|
||||
toolbarSubtitleView.setTranslationY(titleTransY);
|
||||
}
|
||||
getToolbar().setOnClickListener(v->{
|
||||
getScrollableRecyclerView().scrollToPosition(0);
|
||||
scrollView.smoothScrollTo(0, 0);
|
||||
});
|
||||
getToolbar().setOnClickListener(v->scrollToTop());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -798,6 +795,12 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scrollToTop(){
|
||||
getScrollableRecyclerView().scrollToPosition(0);
|
||||
scrollView.smoothScrollTo(0, 0);
|
||||
}
|
||||
|
||||
private class ProfilePagerAdapter extends RecyclerView.Adapter<SimpleViewHolder>{
|
||||
@NonNull
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.joinmastodon.android.fragments;
|
||||
|
||||
/*package*/ interface ScrollableToTop{
|
||||
void scrollToTop();
|
||||
}
|
||||
Reference in New Issue
Block a user