[android] Updated V7 support library to use the DividerItemDecorator

[android] Got rid of custom DivideItemDecorador, which has a bug, and replaced it with decorator from support library and refactored all code regarding the item decoration
This commit is contained in:
Александр Зацепин 2017-09-19 15:21:04 +03:00 committed by Roman Kuznetsov
parent 2dd4045f8c
commit 644a044fec
7 changed files with 130 additions and 169 deletions

View file

@ -31,15 +31,15 @@ apply plugin: 'io.fabric'
dependencies {
// android support libs
compile ('com.android.support:support-v4:23.4.0') {
compile ('com.android.support:support-v4:25.0.0') {
force = true;
}
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:preference-v7:23.4.0'
compile 'com.android.support:preference-v14:23.4.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.android.support:preference-v7:25.0.0'
compile 'com.android.support:preference-v14:25.0.0'
// google play services
compile 'com.google.android.gms:play-services-location:10.0.1'
compile 'com.google.android.gms:play-services-analytics:10.0.1'

View file

@ -2,7 +2,7 @@ propMinSdkVersion=15
# TODO use 23 target and build tools version, when ProGuard problem will be fixed
# https://code.google.com/p/android/issues/detail?id=184567
propTargetSdkVersion=23
propCompileSdkVersion=23
propCompileSdkVersion=25
propBuildToolsVersion=25.0.2
propVersionCode=752
propVersionName=7.5.2

View file

@ -12,7 +12,6 @@ import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
@ -85,7 +84,7 @@ import com.mapswithme.maps.widget.LineCountTextView;
import com.mapswithme.maps.widget.ObservableScrollView;
import com.mapswithme.maps.widget.RatingView;
import com.mapswithme.maps.widget.ScrollViewShadowController;
import com.mapswithme.maps.widget.recycler.DividerItemDecoration;
import com.mapswithme.maps.widget.recycler.ItemDecoratorFactory;
import com.mapswithme.maps.widget.recycler.RecyclerClickListener;
import com.mapswithme.maps.widget.recycler.SingleChangeItemAnimator;
import com.mapswithme.util.ConnectionState;
@ -712,8 +711,8 @@ public class PlacePageView extends RelativeLayout
R.id.rv__place_hotel_gallery);
mRvHotelGallery.setLayoutManager(new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false));
mRvHotelGallery.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(getContext(),
R.drawable.divider_transparent_quarter)));
mRvHotelGallery.addItemDecoration(
ItemDecoratorFactory.createHotelGalleryDecorator(getContext(), LinearLayoutManager.HORIZONTAL));
mGalleryAdapter.setListener(this);
mRvHotelGallery.setAdapter(mGalleryAdapter);
}
@ -891,8 +890,8 @@ public class PlacePageView extends RelativeLayout
mRvSponsoredProducts.setLayoutManager(new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL,
false));
Drawable divider = ContextCompat.getDrawable(getContext(), R.drawable.divider_transparent_half);
mRvSponsoredProducts.addItemDecoration(new DividerItemDecoration(divider, true));
mRvSponsoredProducts.addItemDecoration(
ItemDecoratorFactory.createSponsoredGalleryDecorator(getContext(), LinearLayoutManager.HORIZONTAL));
mIvSponsoredLogo.setOnClickListener(this);
}

View file

@ -1,155 +0,0 @@
package com.mapswithme.maps.widget.recycler;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Adds interior dividers to a RecyclerView with a LinearLayoutManager or its
* subclass.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration
{
@NonNull
private final Drawable mDivider;
private int mOrientation;
private final boolean mUsePaddingOnBorders;
/**
* Sole constructor. Takes in a {@link Drawable} to be used as the interior
* divider.
*
* @param divider A divider {@code Drawable} to be drawn on the RecyclerView
*/
public DividerItemDecoration(@NonNull Drawable divider)
{
mDivider = divider;
mUsePaddingOnBorders = false;
}
/**
* Sole constructor. Takes in a {@link Drawable} to be used as the interior
* divider.
*
* @param divider A divider {@code Drawable} to be drawn on the RecyclerView
* @param usePaddingOnBorders A flag to use padding on borders
*/
public DividerItemDecoration(@NonNull Drawable divider, boolean usePaddingOnBorders)
{
mDivider = divider;
mUsePaddingOnBorders = usePaddingOnBorders;
}
/**
* Draws horizontal or vertical dividers onto the parent RecyclerView.
*
* @param canvas The {@link Canvas} onto which dividers will be drawn
* @param parent The RecyclerView onto which dividers are being added
* @param state The current RecyclerView.State of the RecyclerView
*/
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state)
{
if (mOrientation == LinearLayoutManager.HORIZONTAL)
drawHorizontalDividers(canvas, parent);
else if (mOrientation == LinearLayoutManager.VERTICAL)
drawVerticalDividers(canvas, parent);
}
/**
* Determines the size and location of offsets between items in the parent
* RecyclerView.
*
* @param outRect The {@link Rect} of offsets to be added around the child
* view
* @param view The child view to be decorated with an offset
* @param parent The RecyclerView onto which dividers are being added
* @param state The current RecyclerView.State of the RecyclerView
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
super.getItemOffsets(outRect, view, parent, state);
if (!mUsePaddingOnBorders && parent.getChildAdapterPosition(view) == 0)
return;
boolean isLastItem = parent.getChildAdapterPosition(view) == parent.getChildCount() - 1;
mOrientation = ((LinearLayoutManager) parent.getLayoutManager()).getOrientation();
if (mOrientation == LinearLayoutManager.HORIZONTAL)
{
outRect.left = mDivider.getIntrinsicWidth();
if (isLastItem && !mUsePaddingOnBorders)
outRect.right = mDivider.getIntrinsicWidth();
}
else if (mOrientation == LinearLayoutManager.VERTICAL)
{
outRect.top = mDivider.getIntrinsicHeight();
if (isLastItem && !mUsePaddingOnBorders)
outRect.bottom = mDivider.getIntrinsicHeight();
}
}
/**
* Adds dividers to a RecyclerView with a LinearLayoutManager or its
* subclass oriented horizontally.
*
* @param canvas The {@link Canvas} onto which horizontal dividers will be
* drawn
* @param parent The RecyclerView onto which horizontal dividers are being
* added
*/
private void drawHorizontalDividers(Canvas canvas, RecyclerView parent)
{
int parentTop = parent.getPaddingTop();
int parentBottom = parent.getHeight() - parent.getPaddingBottom();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++)
{
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int parentLeft = child.getRight() + params.rightMargin;
int parentRight = parentLeft + mDivider.getIntrinsicWidth();
mDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom);
mDivider.draw(canvas);
}
}
/**
* Adds dividers to a RecyclerView with a LinearLayoutManager or its
* subclass oriented vertically.
*
* @param canvas The {@link Canvas} onto which vertical dividers will be
* drawn
* @param parent The RecyclerView onto which vertical dividers are being
* added
*/
private void drawVerticalDividers(Canvas canvas, RecyclerView parent)
{
int parentLeft = parent.getPaddingLeft();
int parentRight = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++)
{
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int parentTop = child.getBottom() + params.bottomMargin;
int parentBottom = parentTop + mDivider.getIntrinsicHeight();
mDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom);
mDivider.draw(canvas);
}
}
}

View file

@ -0,0 +1,38 @@
package com.mapswithme.maps.widget.recycler;
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.view.View;
class HotelDividerItemDecoration extends DividerItemDecoration
{
/**
* Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a
* {@link LinearLayoutManager}.
*
* @param context Current context, it will be used to access resources.
* @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
HotelDividerItemDecoration(Context context, int orientation)
{
super(context, orientation);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
super.getItemOffsets(outRect, view, parent, state);
int itemCount = state.getItemCount();
int itemPosition = parent.getChildAdapterPosition(view);
// Last position.
if (itemPosition != RecyclerView.NO_POSITION && itemPosition > 0
&& itemCount > 0 && itemPosition == itemCount - 1)
{
outRect.set(0, 0, 0, 0);
}
}
}

View file

@ -0,0 +1,39 @@
package com.mapswithme.maps.widget.recycler;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import com.mapswithme.maps.R;
public class ItemDecoratorFactory
{
@NonNull
public static RecyclerView.ItemDecoration createHotelGalleryDecorator(@NonNull Context context,
int orientation)
{
DividerItemDecoration decoration = new HotelDividerItemDecoration(context, orientation);
decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_transparent_quarter));
return decoration;
}
@NonNull
public static RecyclerView.ItemDecoration createSponsoredGalleryDecorator(@NonNull Context context,
int orientation)
{
DividerItemDecoration decoration = new SponsoredDividerItemDecoration(context, orientation);
decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_transparent_half));
return decoration;
}
@NonNull
public static RecyclerView.ItemDecoration createRatingRecordDecorator(@NonNull Context context,
int orientation)
{
DividerItemDecoration decoration = new DividerItemDecoration(context, orientation);
decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_transparent_base));
return decoration;
}
}

View file

@ -0,0 +1,40 @@
package com.mapswithme.maps.widget.recycler;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.*;
import android.view.View;
class SponsoredDividerItemDecoration extends DividerItemDecoration
{
private int mDividerWidth;
/**
* Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a
* {@link LinearLayoutManager}.
*
* @param context Current context, it will be used to access resources.
* @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
SponsoredDividerItemDecoration(Context context, int orientation)
{
super(context, orientation);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
super.getItemOffsets(outRect, view, parent, state);
// First element.
if (parent.getChildAdapterPosition(view) == 0)
outRect.left = mDividerWidth;
}
@Override
public void setDrawable(@NonNull Drawable drawable)
{
super.setDrawable(drawable);
mDividerWidth = drawable.getIntrinsicWidth();
}
}