aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-08-29 05:22:27 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-08-29 05:22:27 +0000
commitc5a824ac89af99017b95b0806b700f0428e198e3 (patch)
tree7acc2ff82031e839d894c5cabbabcbc960217027 /app/src/main/java/ml
parent8f1d183858727dccc25407a8f1f51ce5b24cf0d8 (diff)
downloadinfinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar.gz
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar.bz2
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar.lz
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar.xz
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.tar.zst
infinity-for-reddit-c5a824ac89af99017b95b0806b700f0428e198e3.zip
Set item offset in Postfragment when using StaggeredGridLayoutManager. Minor UI tweaks.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java37
1 files changed, 36 insertions, 1 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index 86900902..23b24de2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -7,11 +7,11 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
+import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
-import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -22,6 +22,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
+import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
@@ -159,6 +160,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} else {
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
+ StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration =
+ new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset);
+ mPostRecyclerView.addItemDecoration(itemDecoration);
}
mGlide = Glide.with(activity);
@@ -583,4 +587,35 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
currentPosition = -1;
}
}
+
+ private static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration {
+
+ private int mItemOffset;
+
+ StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset) {
+ mItemOffset = itemOffset;
+ }
+
+ StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {
+ this(context.getResources().getDimensionPixelSize(itemOffsetId));
+ }
+
+ @Override
+ public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
+ @NonNull RecyclerView.State state) {
+ super.getItemOffsets(outRect, view, parent, state);
+
+ StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
+
+ int spanIndex = layoutParams.getSpanIndex();
+
+ int halfOffset = mItemOffset / 2;
+
+ if(spanIndex == 0) {
+ outRect.set(0, 0, halfOffset, 0);
+ } else {
+ outRect.set(halfOffset, 0, 0, 0);
+ }
+ }
+ }
} \ No newline at end of file