RecyclerView使用以下视图滑动,但未检测到点击

时间:2016-08-28 07:59:22

标签: android android-recyclerview

我有这样的RecyclerView行布局

<Layout>
    <BackgroundView>        
    <ForegroundView>
</Layout>

我正在使用ItemTouchHelper处理前景视图上的滑动(部分),如

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    adapter.onItemSwiped(viewHolder);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                        float dX, float dY, int actionState, boolean isCurrentlyActive) {

    View foregroundView = ((myViewHolder)viewHolder).getForegroundView();
    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);

    //getSwipeLimit() used below returns the width of the delete icon
    float translationX = Math.min(-dX, ((myViewHolder) viewHolder).getSwipeLimit());
    foregroundView.setTranslationX(-translationX);
}

我已在我的适配器类的BindViewHolder中为backgroundview设置了一个单击侦听器。

@Override
public void onBindViewHolder(WhiteListViewHolder holder, Cursor cursor) {
    //get name and number from the cursor here

    holder.name.setText(name);
    holder.number.setText(number);

    holder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("whitelist", "yes clicked");
        }
    });
}

问题是,后台视图在未刷过视图时接受点击,但在视图被刷出后,后台视图停止接受点击。

enter image description here

参考上面的图片,如果我点击删除按钮,滑动的视图有时会被恢复,并且它不会捕获点击。

如果我将整个视图滑出,单击剩下的空白区域也会返回滑动视图。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我解决了任务的第一部分 - 现在没有滑动就检测不到。 但刷卡后无法检测到点击...

 @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) {
         backgroundView.setVisibility(View.VISIBLE);
     }
     else {
         backgroundView.setVisibility(View.GONE);
     }
   }
 }

答案 1 :(得分:0)

ItemTouchHelper 也有同样的问题。在 adapter.notifyDataSetChanged() 中调用 onSwiped(..) 为我解决了这个问题。

ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, LEFT) {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean = false

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            //calling my listeners here

            recycler_view.adapter?.notifyDataSetChanged()
        }

        override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
            if (viewHolder != null) {
                getDefaultUIUtil().onSelected(viewHolder.itemView.view_foreground)
            }
        }

        override fun onChildDrawOver(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder?,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {
            if (dX < 0 && viewHolder != null) {
                getDefaultUIUtil().onDrawOver(
                    c,
                    recyclerView,
                    viewHolder.itemView.view_foreground,
                    dX,
                    dY,
                    actionState,
                    isCurrentlyActive
                )
            }
        }

        override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {
            if (dX < 0) {
                getDefaultUIUtil().onDraw(
                    c,
                    recyclerView,
                    viewHolder.itemView.view_foreground,
                    dX,
                    dY,
                    actionState,
                    isCurrentlyActive
                )
            }
        }
    }).also {
        it.attachToRecyclerView(recycler_view)
    }