Android Open Source - DragNDropList Drag N Drop Simple Adapter






From Project

Back to project page DragNDropList.

License

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
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 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;
    setup(data.size());
  }

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

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

  @Override
  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;
  }

  @Override
  public int getDragHandler() {
    return mHandler;
  }
}




Java Source Code List

com.terlici.dragndroplist.DragNDropAdapter.java
com.terlici.dragndroplist.DragNDropCursorAdapter.java
com.terlici.dragndroplist.DragNDropListView.java
com.terlici.dragndroplist.DragNDropSimpleAdapter.java