Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:58:44+00:00 2026-06-17T13:58:44+00:00

On Android, I use a ListView and I want to be able to reorder

  • 0

On Android, I use a ListView and I want to be able to reorder its items using drag and drop. I know there are different implementation of a “drag and drop listview”, however I want to use the Drag and Drop framework coming since API level 11.

It started very well until I wanted to scroll my ListView while doing a drag and drop. As it is written in the example below, for now, I check on top of which list element I am, so if its position is not between ListView.getLastVisiblePosition() and ListView.getFirstVisiblePosition() I use a ListView.smoothScrollToPosition() to view the other list items.

It is a first implementation but it works quite well.

The problem arises while scrolling: some elements do not answer to the drag and drop events – DragEvent.ACTION_DRAG_ENTERED and the others – when I am on top of them. It is due to the way the ListView manages its item views: it tries to recycle the item views that are not visible any more.

It is all right and it works, but sometimes the getView() of the ListAdapter returns a new object. Since it is new, this object missed the DragEvent.ACTION_DRAG_STARTED so it does not answer to the other DragEvent events!

Here is an example. In this case, if I start a drag and drop with a long click on a list item and if I drag it, the majority of items will have a green background if I am on top of them ; but some don’t.

Any idea about making them subscribe to the Drag and drop event mechanism even if they missed DragEvent.ACTION_DRAG_STARTED?

// Somewhere I have a ListView that use the MyViewAdapter
// MyListView _myListView = ...
// _myListView.setAdapter(new MyViewAdapter(getActivity(), ...));
_myListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, _myListView.getItemAtPosition(position), 0);
        return true;
    }
});

class MyViewAdapter extends ArrayAdapter<MyElement> {

    public MyViewAdapter(Context context, List<TimedElement> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View myElementView = convertView;
        if (myElementView == null) {
            /* If the code is executed here while scrolling with a drag and drop,
             * the new view is not associated to the current drag and drop events */
            Log.d("app", "Object created!");
            // Create view
            // myElementView = ...
            // Prepare drag and drop
            myElementView.setOnDragListener(new MyElementDragListener());
        }
        // Associates view and position in ListAdapter, needed for drag and drop
        myElementView.setTag(R.id.item_position, position);
        // Continue to prepare view
        // ...
        return timedElementView;
    }

    private class MyElementDragListener implements View.OnDragListener {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            final int action = event.getAction();
            switch(action) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundColor(Color.GREEN);
                v.invalidate();
                return true;
            case DragEvent.ACTION_DRAG_LOCATION:
                int targetPosition = (Integer)v.getTag(R.id.item_position);
                if (event.getY() < v.getHeight()/2 ) {
                    Log.i("app", "top "+targetPosition);        
                }
                else {
                    Log.i("app", "bottom "+targetPosition);
                }
                // To scroll in ListView while doing drag and drop
                if (targetPosition > _myListView.getLastVisiblePosition()-2) {
                    _myListView.smoothScrollToPosition(targetPosition+2);
                }
                else if (targetPosition < _myListView.getFirstVisiblePosition()+2) {
                    _myListView.smoothScrollToPosition(targetPosition-2);
                }
                return true;
            case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundColor(Color.BLUE);
                v.invalidate();
                return true;
            case DragEvent.ACTION_DROP:
            case DragEvent.ACTION_DRAG_ENDED:
            default:
               break;
            }
            return false;
        }       
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:58:46+00:00Added an answer on June 17, 2026 at 1:58 pm

    I did not solved this recycling problem, but I found a possible workaround still using the Drag & Drop framework. The idea is to change of perspective: instead of using a OnDragListener on each View in the list, it can be used on the ListView directly.

    Then the idea is to find on top of which item the finger is while doing the Drag & Drop, and to write the related display code in the ListAdapter of the ListView. The trick is then to find on top of which item view we are, and where the drop is done.

    In order to do that, I set as an id to each view created by the adapter its ListView position – with View.setId(), so I can find it later using a combination of ListView.pointToPosition() and ListView.findViewById().

    As a drag listener example (which is, I remind you, applied on the ListView), it can be something like that:

    // Initalize your ListView
    private ListView _myListView = new ListView(getContext());
    
    // Start drag when long click on a ListView item
    _myListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(null, shadowBuilder, _myListView.getItemAtPosition(position), 0);
            return true;
        }
    });
    
    // Set the adapter and drag listener
    _myListView.setOnDragListener(new MyListViewDragListener());
    _myListView.setAdapter(new MyViewAdapter(getActivity()));
    
    // Classes used above
    
    private class MyViewAdapter extends ArrayAdapter<Object> {
        public MyViewAdapter (Context context, List<TimedElement> objects) {
            super(context, 0, objects);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View myView = convertView;
            if (myView == null) {
                // Instanciate your view
            }
            // Associates view and position in ListAdapter, needed for drag and drop
            myView.setId(position);
            return myView;
        }
    }
    
    
    private class MyListViewDragListener implements View.OnDragListener {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            final int action = event.getAction();
            switch(action) {
                case DragEvent.ACTION_DRAG_STARTED:
                    return true;
                case DragEvent.ACTION_DRAG_DROP:
                    // We drag the item on top of the one which is at itemPosition
                    int itemPosition = _myListView.pointToPosition((int)event.getX(), (int)event.getY());
                    // We can even get the view at itemPosition thanks to get/setid
                    View itemView = _myListView.findViewById(itemPosition );
                    /* If you try the same thing in ACTION_DRAG_LOCATION, itemView
                     * is sometimes null; if you need this view, just return if null.
                     * As the same event is then fired later, only process the event
                     * when itemView is not null.
                     * It can be more problematic in ACTION_DRAG_DROP but for now
                     * I never had itemView null in this event. */
                    // Handle the drop as you like
                    return true;
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use a ListView with android.R.layout.simple_list_item_multiple_choice as list items. I want to sort the
I want to use ListView on earlier versions of Android within an AppWidget. RagnarRs
Is there anyway to use listview in a fragment? I am using ViewPager to
I'm developing a ListView app for Android. I want to use the default ListView
I will use quickaction for a listview. I use this example for quickaction: http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
i know how to proguard android project,but at present i use android do a
I write an Android Application and I use a ListView and it works fine,
I want built a Android App and use Eclipse for this. It is my
I am writing a app for android 1.5. I want to use a complex
I have a ListView in Android that I want to split in pages that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.