Skip to content

Commit

Permalink
Use DividerItemDecoration instead of custom divider in people list
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Jun 29, 2022
1 parent 84598d0 commit d1eeb1e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.wordpress.android.ui.people;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -19,15 +15,14 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.google.android.material.appbar.AppBarLayout;

import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.analytics.AnalyticsTracker;
Expand All @@ -47,7 +42,6 @@
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.GravatarUtils;
import org.wordpress.android.util.NetworkUtils;
import org.wordpress.android.util.RtlUtils;
import org.wordpress.android.util.image.ImageManager;
import org.wordpress.android.util.image.ImageType;

Expand Down Expand Up @@ -108,6 +102,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);

Context context = getContext();
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.people_list_fragment, container, false);

Toolbar toolbar = rootView.findViewById(R.id.toolbar_main);
Expand All @@ -124,10 +119,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

mActionableEmptyView = rootView.findViewById(R.id.actionable_empty_view);
mFilteredRecyclerView = rootView.findViewById(R.id.filtered_recycler_view);
mFilteredRecyclerView
.addItemDecoration(new PeopleItemDecoration(getActivity()));
mFilteredRecyclerView.setLogT(AppLog.T.PEOPLE);
mFilteredRecyclerView.setSwipeToRefreshEnabled(false);
if (context != null) {
mFilteredRecyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
}

// the following will change the look and feel of the toolbar to match the current design
mFilteredRecyclerView.setToolbarLeftAndRightPadding(
Expand Down Expand Up @@ -478,44 +474,4 @@ public void onClick(View v) {
}
}
}

// Taken from http://stackoverflow.com/a/27037230
private class PeopleItemDecoration extends RecyclerView.ItemDecoration {
private InsetDrawable mDivider;

// use a custom drawable
PeopleItemDecoration(Context context) {
int[] attrs = {android.R.attr.listDivider};
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawable = ta.getDrawable(0);
ta.recycle();

int inset = context.getResources().getDimensionPixelOffset(R.dimen.people_list_divider_left_margin);

if (RtlUtils.isRtl(context)) {
mDivider = new InsetDrawable(drawable, 0, 0, inset, 0);
} else {
mDivider = new InsetDrawable(drawable, inset, 0, 0, 0);
}
}

@Override
public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
int left = ViewCompat.getPaddingStart(parent);
int right = parent.getWidth() - ViewCompat.getPaddingEnd(parent);

int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();

mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
}
1 change: 0 additions & 1 deletion WordPress/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@
<dimen name="people_avatar_sz">40dp</dimen>
<dimen name="people_list_row_height">72dp</dimen>
<dimen name="people_list_row_role_margin_top">20dp</dimen>
<dimen name="people_list_divider_left_margin">72dp</dimen>
<dimen name="people_chips_min_target">40dp</dimen>
<dimen name="people_default_margin_top">5dp</dimen>

Expand Down

0 comments on commit d1eeb1e

Please sign in to comment.