Scroll feed to top when bottom feed tab clicked

This commit is contained in:
Ammar Githam 2020-11-07 20:26:20 +09:00
parent 25b2b504cb
commit e530a336a7
5 changed files with 37 additions and 4 deletions

View File

@ -29,6 +29,7 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData;
import androidx.navigation.NavBackStackEntry;
@ -56,6 +57,7 @@ import awais.instagrabber.asyncs.SuggestionsFetcher;
import awais.instagrabber.customviews.helpers.CustomHideBottomViewOnScrollBehavior;
import awais.instagrabber.databinding.ActivityMainBinding;
import awais.instagrabber.fragments.PostViewV2Fragment;
import awais.instagrabber.fragments.main.FeedFragment;
import awais.instagrabber.fragments.settings.MorePreferencesFragmentDirections;
import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.IntentModel;
@ -429,6 +431,16 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
firstFragmentGraphIndex);
navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController));
currentNavControllerLiveData = navControllerLiveData;
binding.bottomNavView.setOnNavigationItemReselectedListener(item -> {
// Log.d(TAG, "setupBottomNavigationBar: item: " + item);
final Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.main_nav_host);
if (navHostFragment != null) {
final Fragment fragment = navHostFragment.getChildFragmentManager().getPrimaryNavigationFragment();
if (fragment instanceof FeedFragment) {
((FeedFragment) fragment).scrollToTop();
}
}
});
}
private void setBottomNavSelectedItem(final int navId) {

View File

@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.transition.ChangeBounds;
@ -80,6 +81,13 @@ public class PostsRecyclerView extends RecyclerView {
}
};
private final RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
public PostsRecyclerView(@NonNull final Context context) {
super(context);
}
@ -313,4 +321,10 @@ public class PostsRecyclerView extends RecyclerView {
super.onDetachedFromWindow();
lifeCycleOwner = null;
}
@Override
public void smoothScrollToPosition(final int position) {
smoothScroller.setTargetPosition(position);
layoutManager.startSmoothScroll(smoothScroller);
}
}

View File

@ -380,4 +380,9 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre
preferences -> new Handler().postDelayed(() -> binding.feedRecyclerView.setLayoutPreferences(preferences), 200));
fragment.show(getChildFragmentManager(), "posts_layout_preferences");
}
public void scrollToTop() {
binding.feedRecyclerView.smoothScrollToPosition(0);
binding.storiesContainer.setExpanded(true);
}
}

View File

@ -759,7 +759,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Unfollow success: " + result);
// Log.d(TAG, "Unfollow success: " + result);
onRefresh();
}
@ -776,7 +776,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
new ServiceCallback<FriendshipRepoChangeRootResponse>() {
@Override
public void onSuccess(final FriendshipRepoChangeRootResponse result) {
Log.d(TAG, "Follow success: " + result);
// Log.d(TAG, "Follow success: " + result);
onRefresh();
}

View File

@ -19,9 +19,11 @@ import java.util.List;
import awais.instagrabber.R;
/**
* This is a Java rewrite of <a href="https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt">NavigationExtensions</a>
* from architecture-components-samples. Some modifications have been done, check git history.
*/
public class NavigationExtensions {
// private static final String TAG = "NavigationExtensions";
@NonNull
public static LiveData<NavController> setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView,
@NonNull List<Integer> navGraphIds,