根据我的理解,实现使用刷卡项目下方背景的RecyclerView的滑动到解除的一种可能性(如在许多谷歌应用程序中)是实现ItemTouchHelper的简单回调并在方法onChildDraw中绘制背景 .
这是我的实施:
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
// create objects once and avoid allocating them in the onChildDraw method
Drawable background;
Drawable icon;
int iconMargin;
boolean initialized;
private void init() {
background = new ColorDrawable(Color.RED);
icon = ContextCompat.getDrawable(mAppContext, R.drawable.ic_delete_white_24dp);
iconMargin = (int) mAppContext.getResources().getDimension(R.dimen.fab_margin);
initialized = true;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
// remove swiped item from the list and notify the adapter
int position = viewHolder.getAdapterPosition();
mItemList.remove(position);
mRecyclerViewAdapter.notifyDataSetChanged();
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if (dX > 0) {
View itemView = viewHolder.itemView;
// if viewHolder has been swiped away, don't do anything
if (viewHolder.getAdapterPosition() == -1) {
return;
}
if (!initialized) {
init();
}
// draw background
int dXSwipe = (int) (dX * 1.05); // increase slightly dX to avoid view flickering, compensating loss of precision due to int conversion
background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + dXSwipe, itemView.getBottom());
background.draw(c);
// draw icon
int top = (itemView.getTop() + itemView.getBottom() - icon.getIntrinsicHeight()) / 2;
int left = itemView.getLeft() + iconMargin;
icon.setBounds(left, top, left + icon.getIntrinsicWidth(), top + icon.getIntrinsicHeight());
icon.draw(c);
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(mRecyclerView);
现在,问题是如何在下面的背景中为视图设置动画 . 可以从Google日历中获取此动画的示例:当滑动事件或提醒时,左侧的图标将根据水平位移量进行放大 .
有谁知道如何实现这一目标?是否有必要采用不同的方法,可能在ViewHolder中有两个视图,如此处所提出的那样RecyclerView Swipe with a view below it?
1 回答
我发现了怎么做 .
对于那些感兴趣的人,解决方案是使用两个视图(前景和背景)并在滑动过程中为背景设置动画 .
布局:
观点持有人:
和ItemTouchHelper回调:
在此帖子之后完成了基本行为(视图持有者中的前景/背景)的实现:Use ItemTouchHelper for Swipe-To-Dismiss with another View displayed behind the swiped out .
希望这可能对某人有用 .