Skip to content

Commit b749d35

Browse files
committed
Deprecate RecyclerViewSwipeDecorator constructor and Builder constructor with context as first parameter, switch to recyclerView.context
1 parent 40e26e4 commit b749d35

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ A simple utility class to add a background, an icon and a label to a RecyclerVie
77
## How do I get set up? ##
88
Get it via Gradle
99
```groovy
10-
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.2.1'
10+
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.2.2'
1111
```
1212
or Maven
1313
```xml
1414
<dependency>
1515
<groupId>it.xabaras.android</groupId>
1616
<artifactId>recyclerview-swipedecorator</artifactId>
17-
<version>1.2.1</version>
17+
<version>1.2.2</version>
1818
<type>pom</type>
1919
</dependency>
2020
```
@@ -53,7 +53,7 @@ Create a RecyclerViewSwipeDecorator using the RecyclerViewSwipeDecoratorBuilder
5353
```java
5454
public void onChildDraw (Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,float dX, float dY,int actionState, boolean isCurrentlyActive){
5555

56-
new RecyclerViewSwipeDecorator.Builder(MainActivity.this, c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
56+
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
5757
.addBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.my_background))
5858
.addActionIcon(R.drawable.my_icon)
5959
.create()

recyclerview-swipedecorator/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'com.android.library'
22

3-
int VERSION_CODE = 5
4-
String VERSION_NAME = "1.2.1"
3+
int VERSION_CODE = 6
4+
String VERSION_NAME = "1.2.2"
55

66
android {
77
compileSdkVersion 28

recyclerview-swipedecorator/src/main/java/it/xabaras/android/recyclerview/swipedecorator/RecyclerViewSwipeDecorator.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
public class RecyclerViewSwipeDecorator {
22-
private Context context;
2322
private Canvas canvas;
2423
private RecyclerView recyclerView;
2524
private RecyclerView.ViewHolder viewHolder;
@@ -69,18 +68,34 @@ private RecyclerViewSwipeDecorator() {
6968
* @param dY The amount of vertical displacement caused by user's action
7069
* @param actionState The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE.
7170
* @param isCurrentlyActive True if this view is currently being controlled by the user or false it is simply animating back to its original state
71+
*
72+
* @deprecated in RecyclerViewSwipeDecorator 1.2.2
7273
*/
74+
@Deprecated
7375
public RecyclerViewSwipeDecorator(Context context, Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
76+
this(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
77+
}
78+
79+
/**
80+
* Create a @RecyclerViewSwipeDecorator
81+
* @param canvas The canvas which RecyclerView is drawing its children
82+
* @param recyclerView The RecyclerView to which ItemTouchHelper is attached to
83+
* @param viewHolder The ViewHolder which is being interacted by the User or it was interacted and simply animating to its original position
84+
* @param dX The amount of horizontal displacement caused by user's action
85+
* @param dY The amount of vertical displacement caused by user's action
86+
* @param actionState The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE.
87+
* @param isCurrentlyActive True if this view is currently being controlled by the user or false it is simply animating back to its original state
88+
*/
89+
public RecyclerViewSwipeDecorator(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
7490
this();
75-
this.context = context;
7691
this.canvas = canvas;
7792
this.recyclerView = recyclerView;
7893
this.viewHolder = viewHolder;
7994
this.dX = dX;
8095
this.dY = dY;
8196
this.actionState = actionState;
8297
this.isCurrentlyActive = isCurrentlyActive;
83-
this.iconHorizontalMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, context.getResources().getDisplayMetrics());
98+
this.iconHorizontalMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, recyclerView.getContext().getResources().getDisplayMetrics());
8499
}
85100

86101
/**
@@ -209,7 +224,7 @@ public void setIconHorizontalMargin(int iconHorizontalMargin) {
209224
* @param iconHorizontalMargin the margin in the given unit
210225
*/
211226
public void setIconHorizontalMargin(int unit, int iconHorizontalMargin) {
212-
this.iconHorizontalMargin = (int)TypedValue.applyDimension(unit, iconHorizontalMargin, context.getResources().getDisplayMetrics());
227+
this.iconHorizontalMargin = (int)TypedValue.applyDimension(unit, iconHorizontalMargin, recyclerView.getContext().getResources().getDisplayMetrics());
213228
}
214229

215230
/**
@@ -263,7 +278,7 @@ public void decorate() {
263278

264279
int iconSize = 0;
265280
if ( swipeRightActionIconId != 0 && dX > iconHorizontalMargin ) {
266-
Drawable icon = ContextCompat.getDrawable(context, swipeRightActionIconId);
281+
Drawable icon = ContextCompat.getDrawable(recyclerView.getContext(), swipeRightActionIconId);
267282
if ( icon != null ) {
268283
iconSize = icon.getIntrinsicHeight();
269284
int halfIcon = iconSize / 2;
@@ -278,7 +293,7 @@ public void decorate() {
278293
if ( mSwipeRightText != null && mSwipeRightText.length() > 0 && dX > iconHorizontalMargin + iconSize) {
279294
TextPaint textPaint = new TextPaint();
280295
textPaint.setAntiAlias(true);
281-
textPaint.setTextSize(TypedValue.applyDimension(mSwipeRightTextUnit, mSwipeRightTextSize, context.getResources().getDisplayMetrics()));
296+
textPaint.setTextSize(TypedValue.applyDimension(mSwipeRightTextUnit, mSwipeRightTextSize, recyclerView.getContext().getResources().getDisplayMetrics()));
282297
textPaint.setColor(mSwipeRightTextColor);
283298
textPaint.setTypeface(mSwipeRightTypeface);
284299

@@ -297,7 +312,7 @@ public void decorate() {
297312
int iconSize = 0;
298313
int imgLeft = viewHolder.itemView.getRight();
299314
if ( swipeLeftActionIconId != 0 && dX < - iconHorizontalMargin ) {
300-
Drawable icon = ContextCompat.getDrawable(context, swipeLeftActionIconId);
315+
Drawable icon = ContextCompat.getDrawable(recyclerView.getContext(), swipeLeftActionIconId);
301316
if ( icon != null ) {
302317
iconSize = icon.getIntrinsicHeight();
303318
int halfIcon = iconSize / 2;
@@ -313,7 +328,7 @@ public void decorate() {
313328
if ( mSwipeLeftText != null && mSwipeLeftText.length() > 0 && dX < - iconHorizontalMargin - iconSize ) {
314329
TextPaint textPaint = new TextPaint();
315330
textPaint.setAntiAlias(true);
316-
textPaint.setTextSize(TypedValue.applyDimension(mSwipeLeftTextUnit, mSwipeLeftTextSize, context.getResources().getDisplayMetrics()));
331+
textPaint.setTextSize(TypedValue.applyDimension(mSwipeLeftTextUnit, mSwipeLeftTextSize, recyclerView.getContext().getResources().getDisplayMetrics()));
317332
textPaint.setColor(mSwipeLeftTextColor);
318333
textPaint.setTypeface(mSwipeLeftTypeface);
319334

@@ -343,10 +358,26 @@ public static class Builder {
343358
* @param dY The amount of vertical displacement caused by user's action
344359
* @param actionState The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE.
345360
* @param isCurrentlyActive True if this view is currently being controlled by the user or false it is simply animating back to its original state
361+
*
362+
* @deprecated in RecyclerViewSwipeDecorator 1.2.2
363+
*/
364+
@Deprecated
365+
public Builder(Context context, Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
366+
this(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
367+
}
368+
369+
/**
370+
* Create a builder for a RecyclerViewsSwipeDecorator
371+
* @param canvas The canvas which RecyclerView is drawing its children
372+
* @param recyclerView The RecyclerView to which ItemTouchHelper is attached to
373+
* @param viewHolder The ViewHolder which is being interacted by the User or it was interacted and simply animating to its original position
374+
* @param dX The amount of horizontal displacement caused by user's action
375+
* @param dY The amount of vertical displacement caused by user's action
376+
* @param actionState The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE.
377+
* @param isCurrentlyActive True if this view is currently being controlled by the user or false it is simply animating back to its original state
346378
*/
347-
public Builder(Context context , Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
379+
public Builder(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
348380
mDecorator = new RecyclerViewSwipeDecorator(
349-
context,
350381
canvas,
351382
recyclerView,
352383
viewHolder,

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "it.xabaras.android.recyclerview.swipedecorator.sample"
77
minSdkVersion 15
88
targetSdkVersion 28
9-
versionCode 2
10-
versionName "1.1"
9+
versionCode 3
10+
versionName "1.1.1"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

sample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".MainActivity"
13+
android:screenOrientation="sensorPortrait">
1314
<intent-filter>
1415
<action android:name="android.intent.action.MAIN" />
1516

sample/src/main/java/it/xabaras/android/recyclerview/swipedecorator/sample/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import android.graphics.Canvas;
44
import android.graphics.Color;
5+
import android.os.Bundle;
56
import android.support.design.widget.Snackbar;
67
import android.support.v4.content.ContextCompat;
78
import android.support.v7.app.AppCompatActivity;
8-
import android.os.Bundle;
99
import android.support.v7.widget.DividerItemDecoration;
1010
import android.support.v7.widget.GridLayoutManager;
1111
import android.support.v7.widget.LinearLayoutManager;
@@ -71,7 +71,7 @@ public void onClick(View view) {
7171
// You must use @RecyclerViewSwipeDecorator inside the onChildDraw method
7272
@Override
7373
public void onChildDraw (Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,float dX, float dY,int actionState, boolean isCurrentlyActive){
74-
new RecyclerViewSwipeDecorator.Builder(MainActivity.this, c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
74+
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
7575
.addSwipeLeftBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.recycler_view_item_swipe_left_background))
7676
.addSwipeLeftActionIcon(R.drawable.ic_archive_white_24dp)
7777
.addSwipeRightBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.recycler_view_item_swipe_right_background))

0 commit comments

Comments
 (0)