拖放时列表无法正确滚动

时间:2016-08-17 12:10:23

标签: android listview

我可以剪辑Bye,但是当我想拖动列表视图时,无法正确滚动。

image

这是在列表视图上执行的事件。我们首先通过点到位来获取项目的位置,并根据位置移动列表。

public boolean onDrag(View v, DragEvent event)
        {
            final int action = event.getAction();
            switch (action) {          

                case DragEvent.ACTION_DRAG_LOCATION:

  //  get position of the selected item in the list 
                    int position = pointToPosition((int) event.getX(), (int) event.getY());

 //  method to move list up and down 
                    moveListViewUpOrDown(position);

                    return true;            
                default:
                    break;
            }
            return false;
        }



     private void moveListViewUpOrDown(int position) {
   // get first visible position of listview
            int firstPosition = getFirstVisiblePosition();

 // get last visible position of listview   
            int lastPosition = getLastVisiblePosition();        

  // to scroll listview up   
            if ((position == firstPosition || position == firstPosition + 1) && firstPosition != 0) {
                smoothScrollToPosition(position - 1);
            }

  // to scroll listview down   
            if ((position == lastPosition || position == lastPosition - 1) && lastPosition != getCount() - 1) {
                smoothScrollToPosition(position + 1);
            }
        }

0 个答案:

没有答案