滑动后更改recyclerview项目背景 [英] Change recyclerview item background when swiped
问题描述
我正在使用recycler view
显示垂直列表,并且正在使用recycler view
的渐变背景.当我使用ItemTouchHelper
向左或向右滑动项目时,我正在删除recycler view
中的项目.
Hi i am showing vertical list using recycler view
and i am using a gradient background for recycler view
.I am deleting items in recycler view
when they are swiped left or right using ItemTouchHelper
.
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mRecyclerAdapter.removeStock(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if(actionState!=ItemTouchHelper.ACTION_STATE_IDLE){
viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
在这里,我将物品轻扫时将回收者视图"项目背景更改为浅灰色,但是当回收者视图项目返回到空闲状态时,我想恢复原始背景,即我希望返回该渐变背景. Recylerview xml文件:
Here i am changing recycler view item background to light grey when item is swiped but i want the original background back when recycler view item goes back to idle state i.e i want that gradient background back. Recylerview xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
tools:context=".ui.StockFragment"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:startColor="#FF18181F"
android:endColor="#FF27354D"
android:angle="90"/>
</shape>
推荐答案
也许您可以在 clearView()
并在调用超类方法后在此处进行更改.
Perhaps you can try inside of onSelectedChanged()
. Alternatively, perhaps you can override clearView()
and do the change there after calling the superclass method.
编辑
阅读更多文档后,我现在认为onSelectedChanged()
是更改项目开始滑动和进入空闲状态时更改背景的正确位置.检查viewHolder
是否不为空,然后检查actionState
更改背景的内容.
Having read the documentation more, I now think onSelectedChanged()
is the correct place to change the background both for when the item starts swiping and when it returns to idle. Check that the viewHolder
is not null and then check the actionState
for what to change the background to.
我认为onChildDraw()
不适合这样做,因为它已经处于绘制阶段,并且更改视图背景可能会导致再次绘制.
I think onChildDraw()
is not the right place to do this because it is already in the drawing phase and changing the view's background there will probably cause another re-draw.
这篇关于滑动后更改recyclerview项目背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!