Add support for drawables in ReadropsItemTouchCallback
This commit is contained in:
parent
505fb5a7c4
commit
04eb496d9e
@ -109,8 +109,8 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
|
||||
new ItemTouchHelper(new ReadropsItemTouchCallback(this,
|
||||
new ReadropsItemTouchCallback.Config.Builder()
|
||||
.swipeDirs(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
|
||||
.leftDraw(Color.RED, R.drawable.ic_delete)
|
||||
.rightDraw(Color.RED, R.drawable.ic_delete)
|
||||
.leftDraw(Color.RED, R.drawable.ic_delete, null)
|
||||
.rightDraw(Color.RED, R.drawable.ic_delete, null)
|
||||
.swipeCallback((viewHolder, direction) -> {
|
||||
parseItemsAdapter.remove(viewHolder.getAdapterPosition());
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.readrops.app.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.graphics.drawable.DrawableCompat;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.paging.PagedList;
|
||||
@ -384,12 +385,16 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
||||
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
|
||||
Drawable readLater = getDrawable(R.drawable.ic_read_later);
|
||||
DrawableCompat.setTint(readLater, ContextCompat.getColor(this, android.R.color.white));
|
||||
|
||||
new ItemTouchHelper(new ReadropsItemTouchCallback(this,
|
||||
new ReadropsItemTouchCallback.Config.Builder()
|
||||
.swipeDirs(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
|
||||
.swipeCallback(this)
|
||||
.leftDraw(Color.DKGRAY, R.drawable.ic_read_later)
|
||||
.rightDraw(Color.DKGRAY, R.drawable.ic_read)
|
||||
.leftDraw(ContextCompat.getColor(this, R.color.colorAccent), R.drawable.ic_read_later, readLater)
|
||||
.rightDraw(ContextCompat.getColor(this, R.color.colorAccent), R.drawable.ic_read, null)
|
||||
.build()))
|
||||
.attachToRecyclerView(recyclerView);
|
||||
|
||||
@ -404,7 +409,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
||||
|
||||
@Override
|
||||
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
|
||||
if (scrollToTop) {
|
||||
;if (scrollToTop) {
|
||||
recyclerView.scrollToPosition(0);
|
||||
scrollToTop = false;
|
||||
} else
|
||||
|
@ -7,12 +7,14 @@ import android.graphics.drawable.Drawable
|
||||
import android.view.View
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.Nullable
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlin.math.abs
|
||||
|
||||
class ReadropsItemTouchCallback(private val context: Context, private val config: Config) : ItemTouchHelper.SimpleCallback(config.dragDirs, config.swipeDirs) {
|
||||
class ReadropsItemTouchCallback(private val context: Context, private val config: Config) :
|
||||
ItemTouchHelper.SimpleCallback(config.dragDirs, config.swipeDirs) {
|
||||
|
||||
private val iconHorizontalMargin = 40
|
||||
|
||||
@ -45,7 +47,8 @@ class ReadropsItemTouchCallback(private val context: Context, private val config
|
||||
background = ColorDrawable(config.leftDraw.bgColor)
|
||||
background.setBounds(itemView.left, itemView.top, dX.toInt(), itemView.bottom)
|
||||
|
||||
icon = ContextCompat.getDrawable(context, config.leftDraw.icon)!!
|
||||
icon = config.leftDraw.drawable
|
||||
?: ContextCompat.getDrawable(context, config.leftDraw.iconRes)!!
|
||||
val iconMargin = (itemView.height - icon.intrinsicHeight) / 2
|
||||
icon.setBounds(itemView.left + iconHorizontalMargin, itemView.top + iconMargin,
|
||||
itemView.left + iconHorizontalMargin + icon.intrinsicWidth, itemView.bottom - iconMargin)
|
||||
@ -54,7 +57,8 @@ class ReadropsItemTouchCallback(private val context: Context, private val config
|
||||
background = ColorDrawable(config.rightDraw.bgColor)
|
||||
background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
|
||||
|
||||
icon = ContextCompat.getDrawable(context, config.rightDraw.icon)!!
|
||||
icon = config.rightDraw.drawable
|
||||
?: ContextCompat.getDrawable(context, config.rightDraw.iconRes)!!
|
||||
val iconMargin = (itemView.height - icon.intrinsicHeight) / 2
|
||||
icon.setBounds(itemView.right - iconHorizontalMargin - icon.intrinsicWidth, itemView.top + iconMargin,
|
||||
itemView.right - iconHorizontalMargin, itemView.bottom - iconMargin)
|
||||
@ -88,7 +92,7 @@ class ReadropsItemTouchCallback(private val context: Context, private val config
|
||||
fun onSwipe(viewHolder: RecyclerView.ViewHolder, direction: Int)
|
||||
}
|
||||
|
||||
class SwipeDraw(@ColorInt val bgColor: Int, @DrawableRes val icon: Int)
|
||||
class SwipeDraw(@ColorInt val bgColor: Int, @DrawableRes val iconRes: Int = 0, val drawable: Drawable?)
|
||||
|
||||
class Config(val dragDirs: Int = 0, val swipeDirs: Int = 0, val moveCallback: MoveCallback? = null,
|
||||
val swipeCallback: SwipeCallback? = null, val leftDraw: SwipeDraw? = null, val rightDraw: SwipeDraw? = null) {
|
||||
@ -123,9 +127,9 @@ class ReadropsItemTouchCallback(private val context: Context, private val config
|
||||
|
||||
fun swipeCallback(swipeCallback: SwipeCallback) = apply { this.swipeCallback = swipeCallback }
|
||||
|
||||
fun leftDraw(@ColorInt bgColor: Int, @DrawableRes icon: Int) = apply { this.leftDraw = SwipeDraw(bgColor, icon) }
|
||||
fun leftDraw(@ColorInt bgColor: Int, @DrawableRes iconRes: Int, @Nullable icon: Drawable? = null) = apply { leftDraw = SwipeDraw(bgColor, iconRes, icon) }
|
||||
|
||||
fun rightDraw(@ColorInt bgColor: Int, @DrawableRes icon: Int) = apply { this.rightDraw = SwipeDraw(bgColor, icon) }
|
||||
fun rightDraw(@ColorInt bgColor: Int, @DrawableRes iconRes: Int, @Nullable icon: Drawable? = null) = apply { this.rightDraw = SwipeDraw(bgColor, iconRes, icon) }
|
||||
|
||||
fun build(): Config {
|
||||
return Config(this)
|
||||
|
Loading…
x
Reference in New Issue
Block a user