Browse Source

Scroll feed to top when bottom feed tab clicked

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
e530a336a7
  1. 12
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 14
      app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java
  3. 5
      app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java
  4. 4
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
  5. 6
      app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java

12
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -29,6 +29,7 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.navigation.NavBackStackEntry; import androidx.navigation.NavBackStackEntry;
@ -56,6 +57,7 @@ import awais.instagrabber.asyncs.SuggestionsFetcher;
import awais.instagrabber.customviews.helpers.CustomHideBottomViewOnScrollBehavior; import awais.instagrabber.customviews.helpers.CustomHideBottomViewOnScrollBehavior;
import awais.instagrabber.databinding.ActivityMainBinding; import awais.instagrabber.databinding.ActivityMainBinding;
import awais.instagrabber.fragments.PostViewV2Fragment; import awais.instagrabber.fragments.PostViewV2Fragment;
import awais.instagrabber.fragments.main.FeedFragment;
import awais.instagrabber.fragments.settings.MorePreferencesFragmentDirections; import awais.instagrabber.fragments.settings.MorePreferencesFragmentDirections;
import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.IntentModel;
@ -429,6 +431,16 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
firstFragmentGraphIndex); firstFragmentGraphIndex);
navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController)); navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController));
currentNavControllerLiveData = navControllerLiveData; 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) { private void setBottomNavSelectedItem(final int navId) {

14
app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java

@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner; import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.transition.ChangeBounds; 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) { public PostsRecyclerView(@NonNull final Context context) {
super(context); super(context);
} }
@ -313,4 +321,10 @@ public class PostsRecyclerView extends RecyclerView {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
lifeCycleOwner = null; lifeCycleOwner = null;
} }
@Override
public void smoothScrollToPosition(final int position) {
smoothScroller.setTargetPosition(position);
layoutManager.startSmoothScroll(smoothScroller);
}
} }

5
app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java

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

4
app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java

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

6
app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java

@ -19,9 +19,11 @@ import java.util.List;
import awais.instagrabber.R; 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 { public class NavigationExtensions {
// private static final String TAG = "NavigationExtensions";
@NonNull @NonNull
public static LiveData<NavController> setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView, public static LiveData<NavController> setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView,
@NonNull List<Integer> navGraphIds, @NonNull List<Integer> navGraphIds,

Loading…
Cancel
Save