Skip to content

Commit

Permalink
Offset scroll-to-post to show part of above post
Browse files Browse the repository at this point in the history
  • Loading branch information
aforcier committed Jul 31, 2017
1 parent 08c6954 commit 711f21e
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -511,10 +512,21 @@ public void onPostsLoaded(int postCount) {
final int position = mPostsListAdapter.getPositionForPost(mTargetPost);
if (position > -1) {
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getActivity()) {
private static final int SCROLL_OFFSET_DP = 23;

@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}

@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd,
int snapPreference) {
// Assume SNAP_TO_START, and offset the scroll, so the bottom of the above post shows
int offsetPx = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, SCROLL_OFFSET_DP, getResources().getDisplayMetrics());
return boxStart - viewStart + offsetPx;
}
};

smoothScroller.setTargetPosition(position);
Expand Down

0 comments on commit 711f21e

Please sign in to comment.