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

时间:2018-10-09 18:06:21

标签: android android-recyclerview swipe threshold

我有一个名字列表(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;
        }

这里有一些图片。

is delete not deleted

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

1 个答案:

答案 0 :(得分:0)

必须在getSwipeThreshold中返回0.5f。这是默认值,因此只需从您的班级中删除此替代项