在packages/apps/Music/src/touchIncepter.java中
该类提供了listview的拖动效果,并提供接口,在程序接口中实现数据的交换即可。
package com.and.DragListview;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
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;
public class DragListview extends ListActivity {
MyAdapter adapter;
TouchInterceptor list;
List<String> arrayText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (TouchInterceptor) getListView();//(TouchInterceptor)findViewById(android.R.id.list);
getText();
adapter = new MyAdapter(this);
setListAdapter(adapter);
list.setDropListener(mDropListener);
// list.setRemoveListener(mRemoveListener);
}
public void getText(){
arrayText = new ArrayList<String>();
arrayText.add("传奇");
arrayText.add("红豆");
arrayText.add("流年");
arrayText.add("棋子");
}
//交换listview的数据
private TouchInterceptor.DropListener mDropListener =
new TouchInterceptor.DropListener() {
public void drop(int from, int to) {
String item = arrayText.get(from);
arrayText.remove(item);//.remove(from);
arrayText.add(to, item);
adapter.notifyDataSetChanged();
}
};
private TouchInterceptor.RemoveListener mRemoveListener =
new TouchInterceptor.RemoveListener() {
public void remove(int which) {
}
};
class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
Context mContext;
public MyAdapter(Context c){
mInflater = LayoutInflater.from(c);
}
public int getCount() {
return arrayText.size();
}
public Object getItem(int arg0) {
return arrayText.get(arg0);
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View contentView, ViewGroup arg2) {
ImageView img;
TextView text;
if(contentView==null){
contentView = mInflater.inflate(R.layout.list_layout, null);
//contentView = mInflater.inflate(R.layout.list_layout,null);
}
img = (ImageView)contentView.findViewById(R.id.img);
img.setBackgroundResource(R.drawable.icon);
text = (TextView)contentView.findViewById(R.id.text);
text.setText(arrayText.get(arg0).toString());
return contentView;
}
}
}
源码中的类:
/*
* Copyright (C) 2008 The Android Open Source Project
*
* 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.and.DragListview;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
public class TouchInterceptor extends ListView {
private ImageView mDragView;
private WindowManager mWi