diff options
author | HuilinChen943 <110803090+HuilinChen943@users.noreply.github.com> | 2022-10-24 02:40:07 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 02:40:07 +0000 |
commit | fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299 (patch) | |
tree | 79e5f94bfc7c9e8c40b8b0c81dcca64a27d7c4e4 /app/src/main | |
parent | bd690de044b4bd333049af3dacfcf933eb6f4f0a (diff) | |
download | infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar.gz infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar.bz2 infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar.lz infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar.xz infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.tar.zst infinity-for-reddit-fe9e0bd5d7ce8f6d544cf32755ba7aa14cae3299.zip |
fix the bug: Scroll position is lost when changing screen orientation (#1173)
Diffstat (limited to '')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index cae1d8fa..0383baf0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -17,6 +17,7 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.Menu; @@ -38,6 +39,7 @@ import androidx.core.view.MenuItemCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.ConcatAdapter; import androidx.recyclerview.widget.ItemTouchHelper; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -128,6 +130,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic public static final String EXTRA_MESSAGE_FULLNAME = "EMF"; public static final String EXTRA_POST_LIST_POSITION = "EPLP"; private static final int EDIT_POST_REQUEST_CODE = 2; + private static final String SCROLL_POSITION_STATE = "SPS"; @BindView(R.id.swipe_refresh_layout_view_post_detail_fragment) SwipeRefreshLayout mSwipeRefreshLayout; @@ -246,6 +249,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private int swipeRightAction; private float swipeActionThreshold; private ItemTouchHelper touchHelper; + private int scrollPosition; public ViewPostDetailFragment() { // Required empty public constructor @@ -307,6 +311,40 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic if (savedInstanceState == null) { mRespectSubredditRecommendedSortType = mSharedPreferences.getBoolean(SharedPreferencesUtils.RESPECT_SUBREDDIT_RECOMMENDED_COMMENT_SORT_TYPE, false); viewPostDetailFragmentId = System.currentTimeMillis(); + } else { + scrollPosition = savedInstanceState.getInt(SCROLL_POSITION_STATE); + // if the scrollPosition < 0 do nothing + if (scrollPosition >= 0) { + if (getResources().getBoolean(R.bool.isTablet)) { + boolean separatePortrait = mPostDetailsSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_POST_AND_COMMENTS_IN_PORTRAIT_MODE, true); + boolean separateLandscape = mPostDetailsSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_POST_AND_COMMENTS_IN_LANDSCAPE_MODE, true); + if (separatePortrait != separateLandscape) { + if (mCommentsRecyclerView != null) { + //restore the position for commentsadapter + scrollPosition--; + mCommentsRecyclerView.scrollToPosition(scrollPosition); + } else { + // restore the position for mrecyclerview + scrollPosition++; + mRecyclerView.scrollToPosition(scrollPosition); + } + } + } else { + if (mSeparatePostAndComments) { + if (mCommentsRecyclerView != null) { + scrollPosition--; + mCommentsRecyclerView.scrollToPosition(scrollPosition); + } + } else { + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + if (mPostDetailsSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_POST_AND_COMMENTS_IN_LANDSCAPE_MODE, true)) { + scrollPosition++; + mRecyclerView.scrollToPosition(scrollPosition); + } + } + } + } + } } mGlide = Glide.with(this); @@ -1159,6 +1197,17 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); comments = mCommentsAdapter == null ? null : mCommentsAdapter.getVisibleComments(); + if (mCommentsRecyclerView != null) { + //scrollpositionn = mcommentsadapter.getPosition() + LinearLayoutManager myLayoutManager = (LinearLayoutManager) mCommentsRecyclerView.getLayoutManager(); + scrollPosition = myLayoutManager != null ? myLayoutManager.findFirstVisibleItemPosition() : 0; + + } else { + //scrollposition = mrecyclerviewadapter.getposition() + LinearLayoutManager myLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); + scrollPosition = myLayoutManager != null ? myLayoutManager.findFirstVisibleItemPosition() : 0; + } + outState.putInt(SCROLL_POSITION_STATE, scrollPosition); Bridge.saveInstanceState(this, outState); } |