Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ded80076 authored by Stevie Kideckel's avatar Stevie Kideckel
Browse files

Add spacing between items as decorations instead of margins

There are bugs in the accounting for the margins if we manipulate the
view directly, causing the wrong top to be reported and the view to be
shifted when we call scrollToPosition. Item decorations ensure that the
layout system for the recycler view always has the right details about
the spacing.

Fix: 191642682
Test: verified locally
Change-Id: Ie80563757079e885c8178883ab16e314d01c5b32
parent 61bc5c6c
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="16dp"
    android:layout_marginBottom="@dimen/widget_list_entry_bottom_margin"
    android:paddingVertical="@dimen/widget_list_header_view_vertical_padding"
    android:orientation="horizontal"
    launcher:appIconSize="48dp">
+1 −2
Original line number Diff line number Diff line
@@ -18,5 +18,4 @@
    android:id="@+id/widgets_table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="16dp"
    android:layout_marginBottom="@dimen/widget_list_entry_bottom_margin"/>
    android:layout_marginHorizontal="16dp"/>
+1 −1
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@
    <dimen name="widget_list_content_corner_radius">4dp</dimen>

    <dimen name="widget_list_header_view_vertical_padding">20dp</dimen>
    <dimen name="widget_list_entry_bottom_margin">2dp</dimen>
    <dimen name="widget_list_entry_spacing">2dp</dimen>

    <dimen name="widget_preview_shadow_blur">0.5dp</dimen>
    <dimen name="widget_preview_key_shadow_distance">1dp</dimen>
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
-->
<resources>
    <item type="id" name="apps_list_view_work" />
    <item type="id" name="tag_widget_entry" />
    <item type="id" name="view_type_widgets_list" />
    <item type="id" name="view_type_widgets_header" />
    <item type="id" name="view_type_widgets_search_header" />
+23 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.launcher3.widget.picker;
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGETSTRAY_APP_EXPANDED;

import android.content.Context;
import android.graphics.Rect;
import android.os.Process;
import android.util.Log;
import android.util.Size;
@@ -34,6 +35,7 @@ import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutParams;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.android.launcher3.BaseActivity;
@@ -107,7 +109,8 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
    @Nullable private Predicate<WidgetsListBaseEntry> mFilter = null;
    @Nullable private RecyclerView mRecyclerView;
    @Nullable private PackageUserKey mPendingClickHeader;
    private int mShortcutPreviewPadding;
    private final int mShortcutPreviewPadding;
    private final int mSpacingBetweenEntries;

    private final WidgetPreviewLoadedCallback mPreviewLoadedCallback =
            ignored -> updateVisibleEntries();
@@ -141,11 +144,28 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
        mShortcutPreviewPadding =
                2 * context.getResources()
                        .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
        mSpacingBetweenEntries =
                context.getResources().getDimensionPixelSize(R.dimen.widget_list_entry_spacing);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        mRecyclerView = recyclerView;

        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(
                    @NonNull Rect outRect,
                    @NonNull View view,
                    @NonNull RecyclerView parent,
                    @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int position = ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
                boolean isHeader =
                        view.getTag(R.id.tag_widget_entry) instanceof WidgetsListBaseEntry.Header;
                outRect.top += position > 0 && isHeader ? mSpacingBetweenEntries : 0;
            }
        });
    }

    @Override
@@ -329,7 +349,9 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
    @Override
    public void onBindViewHolder(ViewHolder holder, int pos) {
        ViewHolderBinder viewHolderBinder = mViewHolderBinders.get(getItemViewType(pos));
        WidgetsListBaseEntry entry = mVisibleEntries.get(pos);
        viewHolderBinder.bindViewHolder(holder, mVisibleEntries.get(pos), pos);
        holder.itemView.setTag(R.id.tag_widget_entry, entry);
    }

    @Override
Loading