RecyclerView ItemTouchHelper滑动撤消

时间:2016-09-29 12:01:34

标签: android android-recyclerview swipe itemtouchhelper

我用2个按钮“删除”和“取消”滑动后显示隐藏的视图。 在我的xml itemrow中,我有2个内部Relativelayouts

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">

<include
    android:id="@+id/item_container"
    layout="@layout/layout_back" />

<RelativeLayout
    android:id="@+id/front"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/quest_background_blue"
    android:orientation="horizontal"
    android:tag="front"
    android:focusableInTouchMode="true">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:padding="8dp" />

    <include
        android:id="@+id/contprice"
        style="@style/wrap"
        layout="@layout/quest_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

    <LinearLayout
        style="@style/wrap.linear"
        android:layout_gravity="center_vertical"
        android:layout_toLeftOf="@id/contprice"
        android:layout_toRightOf="@id/icon"
        android:layout_weight="0.4">

        <TextView
            android:id="@+id/title"
            style="@style/quest_title"
            android:text="Рассказать друзьям" />

        <TextView
            android:id="@+id/details"
            style="@style/quest_detail" />
    </LinearLayout>
</RelativeLayout>

</FrameLayout>

具有id front的RelativeLayout是可刷卡的,在包含id的item_container布局中,其中有两个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent">
<TextView
    android:id="@+id/cancel"
    android:textColor="#0eb0a0"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center"
    android:padding="10dp"
    android:text="Отмена"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/delete"
    android:textColor="#e6007e"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center"
    android:padding="10dp"
    android:text="Скрыть"
    android:textStyle="bold"/>
</RelativeLayout>

问题是当隐藏的按钮布局显示时 按钮不可点击

这里是ItemTouchHelper实现

public class MyItemTouchHelper extends ItemTouchHelper.SimpleCallback {

private ItemAdapter itemAdapter;
private RecyclerView recyclerView;

public MyItemTouchHelper(RecyclerView mRecyclerView, ItemAdapter itemAdapter) {
    super(0, ItemTouchHelper.START | ItemTouchHelper.END);
    this.itemAdapter = itemAdapter;
    this.recyclerView = mRecyclerView;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return false;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
    return makeMovementFlags(0, swipeFlags);
}

@Override
public boolean isItemViewSwipeEnabled() {
    return true;
}

@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
    Log.d("SWIPE", " direction = " + direction);
    final ItemAdapter.ItemViewHolder holder = (ItemAdapter.ItemViewHolder) viewHolder;
    holder.itemView.setOnClickListener(null);
    ((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.bringToFront();
    ((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.invalidate();
    ((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setEnabled(true);
    ((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setEnabled(true);
    ((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.setVisibility(View.VISIBLE);

    ((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "DELETE " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            itemAdapter.removeItem(viewHolder.getAdapterPosition());
            recyclerView.getAdapter().notifyItemRemoved(viewHolder.getAdapterPosition());
        }
    });
    ((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "CANCEL " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            ((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.setVisibility(View.INVISIBLE);
            holder.itemView.setOnClickListener(ItemAdapter.itemAdapterClickListener(viewHolder.getAdapterPosition()));
            recyclerView.getAdapter().notifyItemChanged(viewHolder.getAdapterPosition());
            clearView(recyclerView, viewHolder);
            ((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setEnabled(false);
            ((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setEnabled(false);
            ((ItemAdapter.ItemViewHolder) viewHolder).itemContext.setVisibility(View.VISIBLE);
            ((ItemAdapter.ItemViewHolder) viewHolder).itemContext.bringToFront();
            ((ItemAdapter.ItemViewHolder) viewHolder).itemContext.invalidate();
        }
    });
}


@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (viewHolder.getAdapterPosition() == -1) {
        return;
    }

    ItemAdapter.ItemViewHolder holder = (ItemAdapter.ItemViewHolder) viewHolder;
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        if (dX > 0 || dX < 0) {
            holder.itemContext.setTranslationX(dX);

        }
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY,
                actionState, isCurrentlyActive);
    }
}


@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    View foreground = ((ItemAdapter.ItemViewHolder) viewHolder).getSwipableView();
    getDefaultUIUtil().clearView(foreground);
}
}

和此处的观看者

 public class ItemViewHolder extends RecyclerView.ViewHolder {

    TextView itemText;
    RelativeLayout itemContainer;
    RelativeLayout itemContext;
    TextView itemDelete;
    TextView itemCancel;

    public ItemViewHolder(View itemView) {
        super(itemView);
        itemText = (TextView) itemView.findViewById(R.id.details);
        itemDelete = (TextView) itemView.findViewById(R.id.delete);
        itemCancel = (TextView) itemView.findViewById(R.id.cancel);
        itemContainer = (RelativeLayout) itemView.findViewById(R.id.item_container);
        itemContext = (RelativeLayout) itemView.findViewById(R.id.front);

    }

    public ViewGroup getSwipableView() {
        return itemContext;
    }

    public ViewGroup getItemContainer() {
        return itemContainer;
    }
}

如果是更正确的按钮点击,但不是第一次尝试 UPDATE 在滑动之后,然后点击仍然在滑动模式下的项目,并通过日志显示前3次点击是滑动方向
onSwipe Log.d(“SWIPE”,“direction =”+ direction);

D/SWIPE:  direction = 16
D/SWIPE:  direction = 16
D/SWIPE:  direction = 16

然后点击按钮取消或删除

2 个答案:

答案 0 :(得分:0)

ItemViewHolder更改此

item_container = itemView.findViewById(R.id.item_container);
itemDelete = (TextView) item_container.findViewById(R.id.delete);
itemCancel = (TextView) item_container.findViewById(R.id.cancel);

并添加

View item_container;

答案 1 :(得分:0)

class SwipeItemTouchHelper(private val adapter: QuestsAdapter) : ItemTouchHelper.Callback() {

override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder) = false

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    adapter.redrawViewHolder(viewHolder.adapterPosition)
}

override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
    val swipeView = viewHolder.itemView.findViewById<View>(R.id.front)
    return if (swipeView.visibility == View.INVISIBLE) 0
    else ItemTouchHelper.Callback.makeMovementFlags(0, ItemTouchHelper.START or ItemTouchHelper.END)
}

override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
) {
    if (viewHolder.adapterPosition == -1) return
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        viewHolder.itemView.findViewById<View>(R.id.front).apply {
            ItemTouchHelper.Callback
                    .getDefaultUIUtil()
                    .onDraw(c, recyclerView, this, dX, dY, actionState, isCurrentlyActive)
        }
    }
}

override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
    super.clearView(recyclerView, viewHolder)
    viewHolder.itemView.findViewById<View>(R.id.front).apply {
        ItemTouchHelper.Callback.getDefaultUIUtil().clearView(this)
    }
}

}

这里是适配器的例子,让我解释一下这里发生了什么。 1.在itemtouch乐趣“ onSwiped”中滑动后,我们将适配器乐趣称为“ redrawViewHolder” 2.在VH中,我有2种布局,正面带有一些视图,背面带有按钮:取消和删除 3.在任务对象中,我们有一个滑动动作的标志“ quest.isSwiped = false”,这是真的-滑动显示后退,false-显示VH的正常视图

好吧,您可以在这里查看代码,它有点旧了,我已经下垂了,因此需要对其进行重构https://github.com/wlTrunks/Swipe_and_delete

class QuestsAdapter(private val quests: MutableList<Quest>) : BaseRecyclerAdapter<RecyclerBaseViewModel<*, *>>() {

fun redrawViewHolder(position: Int) {
    if (list[position].model != null) (list[position].model as BackQuest).isSwiped = true
    (quests[position] as? BackQuest)?.let {
        it.isSwiped = true
        notifyItemChanged(position)
    }
}

fun onCancelClick(questId: String) {
    currentQuest(quests, questId) { index, quest ->
        if (list[index].model != null) (list[index].model as BackQuest).isSwiped = false
        quest.isSwiped = false
        notifyItemChanged(index)
    }
}

fun onDeleteClick(questId: String) {
    currentQuest(quests, questId) { index, _ ->
        list.removeAt(index)
        quests.removeAt(index)
        notifyItemRemoved(index)
    }
}

}