如何将滑动阈值设置为半屏?

How to set the swipe threshold to half the screen?

我有一个名称列表 (recycleView)。有些名字很短,有些名字很长。我想在滑动时删除它们,但我想确保用户滑动超过 屏幕 距离(或列表宽度)的一半以上。

目前是按单个项目的长度计算。

我知道如何滑动删除。我已尝试覆盖 getSwipeThreshold 但再次使用项目的大小作为其基础。

这是代码,因为 SO 需要代码:

     RecyclerView recyclerView = findViewById(R.id.edit_list);
     final MyRecyclerViewAdapter adapter = new 
     MyRecyclerViewAdapter(this, qnames);

    //set swipe behavior
    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;  //do not allow move
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            // Row is swiped from recycler view
            int position = viewHolder.getAdapterPosition(); //get position which is swipe
            qnames.remove(position);  //remove from display list
            adapter.notifyItemRemoved(viewHolder.getLayoutPosition());  //update the view

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

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

        @Override
        public float getSwipeThreshold( RecyclerView.ViewHolder viewHolder){

            return .9f;
        }

这是一些照片。

那么如何将滑动阈值设置为半屏?

您必须 return getSwipeThreshold 中的 0.5f。这是默认值,因此只需从 class&

中删除此覆盖