Android Open Source - DragNDropList Drag N Drop Simple Adapter

From Project

Back to project page DragNDropList.


The source code is released under:

Apache License

If you think the Android project DragNDropList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 * Copyright 2012 Terlici Ltd.// ww w  .ja v a2s.  c  o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package com.terlici.dragndroplist;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class DragNDropSimpleAdapter extends SimpleAdapter implements DragNDropAdapter {
  int mPosition[];
  int mHandler;
  public DragNDropSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, int handler) {
    super(context, data, resource, from, to);
    mHandler = handler;

  private void setup(int size) {
    mPosition = new int[size];
    for (int i = 0; i < size; ++i) mPosition[i] = i;
  public View getDropDownView(int position, View view, ViewGroup group) {
    return super.getDropDownView(mPosition[position], view, group);
  public Object getItem(int position) {
    return super.getItem(mPosition[position]);
  public int getItemViewType(int position) {
    return super.getItemViewType(mPosition[position]);
  public long getItemId(int position) {
    return super.getItemId(mPosition[position]);
  public View getView(int position, View view, ViewGroup group) {
    return super.getView(mPosition[position], view, group);
  public boolean isEnabled(int position) {
    return super.isEnabled(mPosition[position]);

  public void onItemDrag(DragNDropListView parent, View view, int position, long id) {

  public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id) {
    int position = mPosition[startPosition];
    if (startPosition < endPosition)
      for(int i = startPosition; i < endPosition; ++i)
        mPosition[i] = mPosition[i + 1];
    else if (endPosition < startPosition)
      for(int i = startPosition; i > endPosition; --i)
        mPosition[i] = mPosition[i - 1];
    mPosition[endPosition] = position;

  public int getDragHandler() {
    return mHandler;

Java Source Code List