Change recyclerview item background when swiped

一个人想着一个人 提交于 2019-12-13 02:40:16

问题


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);

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>

Drawable file:

<?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>

回答1:


Perhaps you can try inside of onSelectedChanged(). Alternatively, perhaps you can override clearView() and do the change there after calling the superclass method.

EDIT

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.

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.



来源:https://stackoverflow.com/questions/37770913/change-recyclerview-item-background-when-swiped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!