Android: Use material dividers

This commit is contained in:
Charles Lombardo 2022-09-13 20:59:58 -04:00
parent 3420823002
commit 33048d0d4c
11 changed files with 36 additions and 190 deletions

View File

@ -14,9 +14,10 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.divider.MaterialDividerItemDecoration;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
import org.dolphinemu.dolphinemu.ui.DividerItemDecoration;
public class CheatListFragment extends Fragment
{
@ -38,6 +39,10 @@ public class CheatListFragment extends Fragment
recyclerView.setAdapter(new CheatsAdapter(activity, viewModel));
recyclerView.setLayoutManager(new LinearLayoutManager(activity));
recyclerView.addItemDecoration(new DividerItemDecoration(activity, null));
MaterialDividerItemDecoration divider =
new MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL);
divider.setLastItemDecorated(false);
recyclerView.addItemDecoration(divider);
}
}

View File

@ -14,10 +14,11 @@ import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.divider.MaterialDividerItemDecoration;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem;
import org.dolphinemu.dolphinemu.ui.DividerItemDecoration;
import java.util.ArrayList;
import java.util.HashMap;
@ -136,7 +137,11 @@ public final class SettingsFragment extends Fragment implements SettingsFragment
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(manager);
recyclerView.addItemDecoration(new DividerItemDecoration(requireActivity(), null));
MaterialDividerItemDecoration divider =
new MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL);
divider.setLastItemDecorated(false);
recyclerView.addItemDecoration(divider);
SettingsActivityView activity = (SettingsActivityView) getActivity();
mPresenter.onViewCreated(menuTag, activity.getSettings());

View File

@ -1,162 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* Implementation from:
* https://gist.github.com/lapastillaroja/858caf1a82791b6c1a36
*/
public final class DividerItemDecoration extends RecyclerView.ItemDecoration
{
private Drawable mDivider;
private boolean mShowFirstDivider = false;
private boolean mShowLastDivider = false;
public DividerItemDecoration(Context context, AttributeSet attrs)
{
final TypedArray a = context
.obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
mDivider = a.getDrawable(0);
a.recycle();
}
public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
boolean showLastDivider)
{
this(context, attrs);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
public DividerItemDecoration(Drawable divider)
{
mDivider = divider;
}
public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
boolean showLastDivider)
{
this(divider);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent,
@NonNull RecyclerView.State state)
{
super.getItemOffsets(outRect, view, parent, state);
if (mDivider == null)
{
return;
}
if (parent.getChildAdapterPosition(view) < 1)
{
return;
}
if (getOrientation(parent) == LinearLayoutManager.VERTICAL)
{
outRect.top = mDivider.getIntrinsicHeight();
}
else
{
outRect.left = mDivider.getIntrinsicWidth();
}
}
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state)
{
if (mDivider == null)
{
super.onDrawOver(c, parent, state);
return;
}
// Initialization needed to avoid compiler warning
int left = 0, right = 0, top = 0, bottom = 0, size;
int orientation = getOrientation(parent);
int childCount = parent.getChildCount();
if (orientation == LinearLayoutManager.VERTICAL)
{
size = mDivider.getIntrinsicHeight();
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
}
else
{ //horizontal
size = mDivider.getIntrinsicWidth();
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
}
for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++)
{
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (orientation == LinearLayoutManager.VERTICAL)
{
top = child.getTop() - params.topMargin;
bottom = top + size;
}
else
{ //horizontal
left = child.getLeft() - params.leftMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
// show last divider
if (mShowLastDivider && childCount > 0)
{
View child = parent.getChildAt(childCount - 1);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (orientation == LinearLayoutManager.VERTICAL)
{
top = child.getBottom() + params.bottomMargin;
bottom = top + size;
}
else
{ // horizontal
left = child.getRight() + params.rightMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private int getOrientation(RecyclerView parent)
{
if (parent.getLayoutManager() instanceof LinearLayoutManager)
{
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
}
else
{
throw new IllegalStateException(
"DividerItemDecoration can only be used with a LinearLayoutManager.");
}
}
}

View File

@ -50,7 +50,7 @@
app:layout_constraintWidth_max="400dp"
app:layout_constraintEnd_toStartOf="@id/divider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/divider"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/user_data_new_location" />
<TextView
@ -84,15 +84,14 @@
app:barrierDirection="end"
app:constraint_referenced_ids="text_type,text_path,text_android_11" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="#1F000000"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/text_type"
app:layout_constraintBottom_toBottomOf="@id/text_android_11"
app:layout_constraintStart_toEndOf="@id/barrier_text"
app:layout_constraintEnd_toStartOf="@id/barrier_buttons" />

View File

@ -53,11 +53,10 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/divider" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="#1F000000"
android:layout_marginStart="24dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

View File

@ -51,11 +51,10 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/divider" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#1F000000"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -52,11 +52,10 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/divider" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#1F000000"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/spacing_large"
android:layout_marginVertical="@dimen/spacing_small"
app:layout_constraintStart_toStartOf="parent"

View File

@ -44,11 +44,10 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_description" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider_1"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#1F000000"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -145,11 +144,10 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBaseline_toBaselineOf="@id/label_revision" />
<View
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider_2"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#1F000000"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -110,13 +110,12 @@
</ScrollView>
<View
android:id="@+id/divider"
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider_2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/colorOnSurfaceVariant"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="16dp"/>
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/menu_exit"

View File

@ -63,4 +63,8 @@
<item name="tickVisible">false</item>
<item name="labelBehavior">gone</item>
</style>
<style name="DolphinDivider" parent="Widget.Material3.MaterialDivider">
<item name="dividerColor">?attr/colorSurfaceVariant</item>
</style>
</resources>

View File

@ -54,6 +54,7 @@
<item name="materialAlertDialogTheme">@style/DolphinMaterialDialog</item>
<item name="popupTheme">@style/DolphinPopup</item>
<item name="sliderStyle">@style/DolphinSlider</item>
<item name="materialDividerStyle">@style/DolphinDivider</item>
</style>
<!-- Trick for API >= 27 specific changes -->