content: package com.stone.customview;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.stone.customview.widget.DragGridView;
  import com.stone.customview.widget.DragGridView.IDragOperation;
  
  public class DragGridActivity extends Activity {
  
  	private List<HashMap<String, Object>> dataSourceList = new ArrayList<HashMap<String, Object>>();
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.drag_gridview);
  
  		DragGridView mDragGridView = (DragGridView) findViewById(R.id.dragGridView);
  		for (int i = 0; i < 26; i++) {
  			HashMap<String, Object> itemHashMap = new HashMap<String, Object>();
  			itemHashMap.put("item_image", R.drawable.j11);
  			itemHashMap.put("item_text", "拖拽 " + Integer.toString(i));
  			dataSourceList.add(itemHashMap);
  		}
  
  		final DragAdapter mDragAdapter = new DragAdapter(this, dataSourceList);
  
  		mDragGridView.setAdapter(mDragAdapter);
  	}
  
  	class DragAdapter extends BaseAdapter implements IDragOperation {
  		private List<HashMap<String, Object>> list;
  		private LayoutInflater mInflater;
  		private int mHidePosition = -1;
  
  		public DragAdapter(Context context, List<HashMap<String, Object>> list) {
  			this.list = list;
  			mInflater = LayoutInflater.from(context);
  		}
  
  		@Override
  		public int getCount() {
  			return list.size();
  		}
  
  		@Override
  		public Object getItem(int position) {
  			return list.get(position);
  		}
  
  		@Override
  		public long getItemId(int position) {
  			return position;
  		}
  
  		/**
  		 * 由于复用convertView导致某些item消失了，所以这里不复用item，
  		 */
  		@Override
  		public View getView(int position, View convertView, ViewGroup parent) {
  			convertView = mInflater.inflate(R.layout.drag_gridview_item, null);
  			ImageView mImageView = (ImageView) convertView
  					.findViewById(R.id.item_image);
  			TextView mTextView = (TextView) convertView
  					.findViewById(R.id.item_text);
  
  			mImageView.setImageResource((Integer) list.get(position).get(
  					"item_image"));
  			mTextView.setText((CharSequence) list.get(position)
  					.get("item_text"));
  
  			if (position == mHidePosition) {
  				convertView.setVisibility(View.INVISIBLE);
  			}
  
  			return convertView;
  		}
  
  		@Override
  		public void reorderItems(int oldPosition, int newPosition) {
  			HashMap<String, Object> temp = list.get(oldPosition);
  			if (oldPosition < newPosition) {
  				for (int i = oldPosition; i < newPosition; i++) {
  					Collections.swap(list, i, i + 1);
  				}
  			} else if (oldPosition > newPosition) {
  				for (int i = oldPosition; i > newPosition; i--) {
  					Collections.swap(list, i, i - 1);
  				}
  			}
  
  			list.set(newPosition, temp);
  		}
  
  		@Override
  		public void setHideItem(int hidePosition) {
  			this.mHidePosition = hidePosition;
  			notifyDataSetChanged();
  		}
  	}
  }
  