From c5a824ac89af99017b95b0806b700f0428e198e3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 29 Aug 2019 13:22:27 +0800 Subject: Set item offset in Postfragment when using StaggeredGridLayoutManager. Minor UI tweaks. --- .../infinityforreddit/PostFragment.java | 37 +++++++++++++++++++++- .../main/res/layout/fragment_comments_listing.xml | 1 + .../res/layout/fragment_followed_users_listing.xml | 1 + app/src/main/res/layout/fragment_post.xml | 1 + .../main/res/layout/fragment_subreddit_listing.xml | 1 + .../fragment_subscribed_subreddits_listing.xml | 1 + app/src/main/res/layout/fragment_user_listing.xml | 1 + .../item_load_comments_failed_placeholder.xml | 2 +- app/src/main/res/values/dimens.xml | 1 + 9 files changed, 44 insertions(+), 2 deletions(-) (limited to 'app/src') 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 diff --git a/app/src/main/res/layout/fragment_comments_listing.xml b/app/src/main/res/layout/fragment_comments_listing.xml index 7dfb31e4..886c093a 100644 --- a/app/src/main/res/layout/fragment_comments_listing.xml +++ b/app/src/main/res/layout/fragment_comments_listing.xml @@ -28,6 +28,7 @@ android:id="@+id/fetch_comments_info_linear_layout_comments_listing_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_followed_users_listing.xml b/app/src/main/res/layout/fragment_followed_users_listing.xml index e5b64728..62ab964e 100644 --- a/app/src/main/res/layout/fragment_followed_users_listing.xml +++ b/app/src/main/res/layout/fragment_followed_users_listing.xml @@ -15,6 +15,7 @@ android:id="@+id/no_subscriptions_linear_layout_followed_users_listing_fragment" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 9083b01d..240bcb60 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -26,6 +26,7 @@ android:id="@+id/fetch_post_info_linear_layout_post_fragment" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_subreddit_listing.xml b/app/src/main/res/layout/fragment_subreddit_listing.xml index d460b569..acc31dab 100644 --- a/app/src/main/res/layout/fragment_subreddit_listing.xml +++ b/app/src/main/res/layout/fragment_subreddit_listing.xml @@ -28,6 +28,7 @@ android:id="@+id/fetch_subreddit_listing_info_linear_layout_subreddit_listing_fragment" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_subscribed_subreddits_listing.xml b/app/src/main/res/layout/fragment_subscribed_subreddits_listing.xml index 25294baf..89227f12 100644 --- a/app/src/main/res/layout/fragment_subscribed_subreddits_listing.xml +++ b/app/src/main/res/layout/fragment_subscribed_subreddits_listing.xml @@ -15,6 +15,7 @@ android:id="@+id/no_subscriptions_linear_layout_subreddits_listing_fragment" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_user_listing.xml b/app/src/main/res/layout/fragment_user_listing.xml index 91eb926e..bdd39dab 100644 --- a/app/src/main/res/layout/fragment_user_listing.xml +++ b/app/src/main/res/layout/fragment_user_listing.xml @@ -28,6 +28,7 @@ android:id="@+id/fetch_user_listing_info_linear_layout_user_listing_fragment" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginBottom="48dp" android:gravity="center" android:orientation="vertical" android:visibility="gone"> diff --git a/app/src/main/res/layout/item_load_comments_failed_placeholder.xml b/app/src/main/res/layout/item_load_comments_failed_placeholder.xml index 094d58bc..ef3342d4 100644 --- a/app/src/main/res/layout/item_load_comments_failed_placeholder.xml +++ b/app/src/main/res/layout/item_load_comments_failed_placeholder.xml @@ -2,7 +2,7 @@ 16dp 0dp 0dp + 16dp \ No newline at end of file -- cgit v1.2.3