From b42e91134bb0f9c13c320463d456b76369abc293 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 14 Nov 2017 12:09:19 -0800 Subject: [PATCH] Remove inset divider from last recyclerview element --- .../securesms/ConversationListFragment.java | 66 ++++++++++++++++--- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationListFragment.java b/src/org/thoughtcrime/securesms/ConversationListFragment.java index e4675600e9..c8aa943acc 100644 --- a/src/org/thoughtcrime/securesms/ConversationListFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationListFragment.java @@ -27,6 +27,7 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Build; @@ -39,7 +40,6 @@ import android.support.v4.content.Loader; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.view.ActionMode; -import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; @@ -126,13 +126,7 @@ public class ConversationListFragment extends Fragment list.setHasFixedSize(true); list.setLayoutManager(new LinearLayoutManager(getActivity())); list.setItemAnimator(new DeleteItemAnimator()); - - TypedArray typedArray = getContext().obtainStyledAttributes(new int[]{R.attr.conversation_list_item_divider}); - Drawable itemDrawable = typedArray.getDrawable(0); - DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL); - if (itemDrawable != null) itemDecoration.setDrawable(itemDrawable); - list.addItemDecoration(itemDecoration); - typedArray.recycle(); + list.addItemDecoration(new InsetDividerItemDecoration(getActivity())); new ItemTouchHelper(new ArchiveListenerCallback()).attachToRecyclerView(list); @@ -561,6 +555,62 @@ public class ConversationListFragment extends Fragment } } + private static class InsetDividerItemDecoration extends RecyclerView.ItemDecoration { + + private Drawable divider; + private final Rect bounds = new Rect(); + + InsetDividerItemDecoration(Context context) { + TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.conversation_list_item_divider}); + this.divider = typedArray.getDrawable(0); + typedArray.recycle(); + } + + @Override + public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { + if (parent.getLayoutManager() == null) { + return; + } + + canvas.save(); + + final int left; + final int right; + + if (parent.getClipToPadding()) { + left = parent.getPaddingLeft(); + right = parent.getWidth() - parent.getPaddingRight(); + canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom()); + } else { + left = 0; + right = parent.getWidth(); + } + + final int childCount = parent.getChildCount(); + + for (int i = 0; i < childCount-1; i++) { + final View child = parent.getChildAt(i); + parent.getDecoratedBoundsWithMargins(child, bounds); + final int bottom = bounds.bottom + Math.round(child.getTranslationY()); + final int top = bottom - divider.getIntrinsicHeight(); + divider.setBounds(left, top, right, bottom); + divider.draw(canvas); + } + + canvas.restore(); + } + + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { + if (divider == null) { + outRect.set(0, 0, 0, 0); + return; + } + + outRect.set(0, 0, 0, divider.getIntrinsicHeight()); + } + } + }