Hide toolbar icons in PostRecyclerViewAdapter when item width drops below threshold.

This commit is contained in:
Docile-Alligator
2026-07-26 12:10:59 -04:00
parent 4d74af4390
commit 01b37ad0b4
3 changed files with 83 additions and 1 deletions

View File

@ -11,7 +11,6 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.MotionEvent;
@ -281,6 +280,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
private boolean canPlayVideo = true;
private RecyclerView.RecycledViewPool mGalleryRecycledViewPool;
private MultiPlayPlayerSelector multiPlayPlayerSelector;
private int itemWidth;
// postHistorySharedPreferences will be null when being used in HistoryPostFragment.
public PostRecyclerViewAdapter(BaseActivity activity, PostFragmentBase fragment, RedditDataRoomDatabase redditDataRoomDatabase,
@ -1190,6 +1190,48 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
mCallback.currentlyBindItem(holder.getBindingAdapterPosition());
}
if (itemWidth < 250) {
if (((PostViewHolder) holder).commentsCountButton != null) {
((PostViewHolder) holder).commentsCountButton.setVisibility(View.GONE);
}
if (((PostViewHolder) holder).saveButton != null) {
((PostViewHolder) holder).saveButton.setVisibility(View.GONE);
}
if (((PostViewHolder) holder).shareButton != null) {
((PostViewHolder) holder).shareButton.setVisibility(View.GONE);
}
} else if (itemWidth < 316) {
if (((PostViewHolder) holder).commentsCountButton != null) {
((PostViewHolder) holder).commentsCountButton.setVisibility(View.GONE);
}
if (((PostViewHolder) holder).saveButton != null) {
((PostViewHolder) holder).saveButton.setVisibility(View.VISIBLE);
}
if (((PostViewHolder) holder).shareButton != null) {
((PostViewHolder) holder).shareButton.setVisibility(View.GONE);
}
} else if (itemWidth < 420) {
if (((PostViewHolder) holder).commentsCountButton != null) {
((PostViewHolder) holder).commentsCountButton.setVisibility(View.GONE);
}
if (((PostViewHolder) holder).saveButton != null) {
((PostViewHolder) holder).saveButton.setVisibility(View.VISIBLE);
}
if (((PostViewHolder) holder).shareButton != null) {
((PostViewHolder) holder).shareButton.setVisibility(View.VISIBLE);
}
} else {
if (((PostViewHolder) holder).commentsCountButton != null) {
((PostViewHolder) holder).commentsCountButton.setVisibility(View.VISIBLE);
}
if (((PostViewHolder) holder).saveButton != null) {
((PostViewHolder) holder).saveButton.setVisibility(View.VISIBLE);
}
if (((PostViewHolder) holder).shareButton != null) {
((PostViewHolder) holder).shareButton.setVisibility(View.VISIBLE);
}
}
} else if (holder instanceof PostGalleryViewHolder) {
Post post = getItem(position);
if (post != null) {
@ -1971,6 +2013,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
this.canPlayVideo = canPlayVideo;
}
public void provideItemWidth(int width) {
itemWidth = width;
}
public abstract class PostViewHolder extends RecyclerView.ViewHolder {
AspectRatioGifImageView iconGifImageView;
ImageView stickiedPostImageView;

View File

@ -154,6 +154,8 @@ public abstract class PostFragmentBase extends Fragment {
protected AdjustableTouchSlopItemTouchHelper touchHelper;
private boolean shouldSwipeBack;
protected final Map<String, String> subredditOrUserIcons = new HashMap<>();
private View.OnLayoutChangeListener onLayoutChangeListener;
private int recyclerViewWidth;
public PostFragmentBase() {
// Required empty public constructor
@ -322,6 +324,26 @@ public abstract class PostFragmentBase extends Fragment {
return false;
});
onLayoutChangeListener = (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
int width = right - left;
if (recyclerViewWidth == width) {
return;
}
recyclerViewWidth = width;
PostRecyclerViewAdapter adapter = getPostAdapter();
if (adapter != null) {
if (mStaggeredGridLayoutManager != null) {
width /= mStaggeredGridLayoutManager.getSpanCount();
}
int finalWidth = width;
v.post(() -> {
adapter.provideItemWidth(Utils.convertPxToDp(finalWidth, mActivity));
refreshAdapter();
});
}
};
getPostRecyclerView().addOnLayoutChangeListener(onLayoutChangeListener);
SharedPreferencesLiveDataKt.stringLiveData(mSharedPreferences, SharedPreferencesUtils.LONG_PRESS_POST_NON_MEDIA_AREA, SharedPreferencesUtils.LONG_PRESS_POST_VALUE_SHOW_POST_OPTIONS).observe(getViewLifecycleOwner(), s -> {
if (getPostAdapter() != null) {
getPostAdapter().setLongPressPostNonMediaAreaAction(s);
@ -355,6 +377,15 @@ public abstract class PostFragmentBase extends Fragment {
ViewCompat.requestApplyInsets(view);
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (onLayoutChangeListener != null) {
getPostRecyclerView().removeOnLayoutChangeListener(onLayoutChangeListener);
onLayoutChangeListener = null;
}
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);

View File

@ -462,6 +462,11 @@ public final class Utils {
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
public static int convertPxToDp(int px, Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
@Nullable
public static Drawable getTintedDrawable(Context context, int drawableId, int color) {
final Drawable drawable = AppCompatResources.getDrawable(context, drawableId);