Set item offset in Postfragment when using StaggeredGridLayoutManager. Minor UI tweaks.

This commit is contained in:
Alex Ning 2019-08-29 13:22:27 +08:00
parent 8f1d183858
commit c5a824ac89
9 changed files with 44 additions and 2 deletions

View File

@ -7,11 +7,11 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -22,6 +22,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.DimenRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -159,6 +160,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} else { } else {
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration =
new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset);
mPostRecyclerView.addItemDecoration(itemDecoration);
} }
mGlide = Glide.with(activity); mGlide = Glide.with(activity);
@ -583,4 +587,35 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
currentPosition = -1; 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);
}
}
}
} }

View File

@ -28,6 +28,7 @@
android:id="@+id/fetch_comments_info_linear_layout_comments_listing_fragment" android:id="@+id/fetch_comments_info_linear_layout_comments_listing_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -15,6 +15,7 @@
android:id="@+id/no_subscriptions_linear_layout_followed_users_listing_fragment" android:id="@+id/no_subscriptions_linear_layout_followed_users_listing_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -26,6 +26,7 @@
android:id="@+id/fetch_post_info_linear_layout_post_fragment" android:id="@+id/fetch_post_info_linear_layout_post_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -28,6 +28,7 @@
android:id="@+id/fetch_subreddit_listing_info_linear_layout_subreddit_listing_fragment" android:id="@+id/fetch_subreddit_listing_info_linear_layout_subreddit_listing_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -15,6 +15,7 @@
android:id="@+id/no_subscriptions_linear_layout_subreddits_listing_fragment" android:id="@+id/no_subscriptions_linear_layout_subreddits_listing_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -28,6 +28,7 @@
android:id="@+id/fetch_user_listing_info_linear_layout_user_listing_fragment" android:id="@+id/fetch_user_listing_info_linear_layout_user_listing_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="48dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">

View File

@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="36dp" android:layout_margin="48dp"
android:orientation="vertical"> android:orientation="vertical">
<ImageView <ImageView

View File

@ -9,4 +9,5 @@
<dimen name="text_margin">16dp</dimen> <dimen name="text_margin">16dp</dimen>
<dimen name="roundedBottomSheetCornerRadiusLeftPadding">0dp</dimen> <dimen name="roundedBottomSheetCornerRadiusLeftPadding">0dp</dimen>
<dimen name="roundedBottomSheetCornerRadiusRightPadding">0dp</dimen> <dimen name="roundedBottomSheetCornerRadiusRightPadding">0dp</dimen>
<dimen name="staggeredLayoutManagerItemOffset">16dp</dimen>
</resources> </resources>