Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RecyclerView ItemTouchHelper滑动撤消_Android_Android Recyclerview_Swipe_Itemtouchhelper - Fatal编程技术网

Android RecyclerView ItemTouchHelper滑动撤消

Android RecyclerView ItemTouchHelper滑动撤消,android,android-recyclerview,swipe,itemtouchhelper,Android,Android Recyclerview,Swipe,Itemtouchhelper,我用两个按钮“删除”和“取消”滑动后显示隐藏视图。 在我的xml itemrow中,我有两个内部RelativeLayout <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_c

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

<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>
如果要获得更多正确的按钮,请单击,但不是第一次尝试 更新 刷卡后,点击项目,它仍然处于刷卡模式,根据日志显示,前3次点击是刷卡方向
Onswip Log.d(“滑动”,“方向=”+方向)


然后单击
项目视图持有者中的按钮“取消”或“删除”

,更改此设置

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;
class SwipeItemTouchHelper(私有val适配器:QuestsAdapter):ItemTouchHelper.Callback(){
爱的乐趣(
回收视图:回收视图,
viewHolder:RecyclerView.viewHolder,
目标:RecyclerView.ViewHolder)=false
覆盖(viewHolder:RecyclerView.viewHolder,方向:Int){
adapter.redrawViewHolder(viewHolder.adapterPosition)
}
覆盖有趣的getMovementFlags(recyclerView:recyclerView,viewHolder:recyclerView.viewHolder):Int{
val swipeView=viewHolder.itemView.findViewById(R.id.front)
如果(swipeView.visibility==View.INVISIBLE)0,则返回
else ItemTouchHelper.Callback.makeMovementFlags(0、ItemTouchHelper.START或ItemTouchHelper.END)
}
在Childraw上覆盖乐趣(
c:帆布,
回收视图:回收视图,
viewHolder:RecyclerView.viewHolder,
dX:浮动,
戴:浮球,
actionState:Int,
isCurrentlyActive:Boolean
) {
如果(viewHolder.adapterPosition==-1)返回
如果(actionState==ItemTouchHelper.ACTION\u STATE\u SWIPE){
viewHolder.itemView.findViewById(R.id.front)。应用{
ItemTouchHelper.Callback
.getDefaultUIUtil()
.onDraw(c、recyclerView、this、dX、dY、actionState、isCurrentlyActive)
}
}
}
覆盖有趣的clearView(recyclerView:recyclerView,viewHolder:recyclerView.viewHolder){
super.clearView(recyclerView、viewHolder)
viewHolder.itemView.findViewById(R.id.front)。应用{
ItemTouchHelper.Callback.getDefaultUIUtil().clearView(此)
}
}
}

这里是适配器,例如,让我解释一下这里发生了什么。 1.在itemtouch fun“OnSweep”中滑动后,我们将适配器fun称为“重画视图持有者” 2.在VH中,我有两个布局,前面有一些视图,后面有按钮:取消和删除 3.在任务对象中,我们有一个滑动动作“quest.isSwiped=false”的标志,这里是true-滑动显示backlayout,false-显示VH的正常视图

你们可以看看这里的代码,它有点旧了,我把它放低了,所以它需要重构

class QuestsAdapter(private val quests:MutableList):BaseRecyclerAdapter(){
趣味重绘视图支架(位置:Int){
if(list[position].model!=null)(list[position].model as BackQuest)。isSwiped=true
(任务[位置]为?回溯任务)?。让{
it.iss=正确
更改项目(位置)
}
}
有趣的onCancelClick(问题ID:String){
当前任务(任务,任务ID){索引,任务->
if(list[index].model!=null)(list[index].model as BackQuest.isSwiped=false)
quest.iss=false
notifyItemChanged(索引)
}
}
单击(问题ID:字符串){
当前任务(任务,任务ID){index,->
list.removeAt(索引)
任务移除(索引)
notifyItemRemoved(索引)
}
}

}

它是可点击的,但第一次尝试时仍然无法点击。这就像物品仍然处于刷卡模式。在这里,前3次点击后的一些日志显示仍在刷卡D/刷卡:方向=16 D/刷卡:方向=16 D/刷卡:方向=16 D/刷卡:方向=16公共无效OnSweed(final RecyclerView.ViewHolder ViewHolder,int direction){log.D(“刷卡”,“方向=+方向”)是的,大约三年前,我猜你不记得解决办法是什么了,对吧?:-)@Trunksssj@Sharas对不起,我通常忘记在这里登录。请检查我的代码Bellow。3年后,现在查看一下,这可以用更优雅的方式完成。)
 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;
    }
}
D/SWIPE:  direction = 16
D/SWIPE:  direction = 16
D/SWIPE:  direction = 16
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;
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)
    }
}
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)
    }
}