在Music中,播放界面点击列表发出intent跳转至TrackbrowserActivity,在该界面中,判断intent中的Action,如果是ectionmode,则可以随意拖动item的顺序,效果很顺滑。
((TouchInterceptor) mTrackList).setDropListener(mDropListener);
((TouchInterceptor) mTrackList).setRemoveListener(mRemoveListener);
mTrackList.setDivider(null);
mTrackList向下转型至TouchInterceptor。TouchInterceptor extends ListView。在这设置了监听器,如下:
private TouchInterceptor.DropListener mDropListener =
new TouchInterceptor.DropListener() {
public void drop(int from, int to) {
if (mTrackCursor instanceof NowPlayingCursor) {
// update the currently playing list
NowPlayingCursor c = (NowPlayingCursor) mTrackCursor;
c.moveItem(from, to);
((TrackListAdapter)getListAdapter()).notifyDataSetChanged();
getListView().invalidateViews();
mDeletedOneRow = true;
} else {
// update a saved playlist
MediaStore.Audio.Playlists.Members.moveItem(getContentResolver(),
Long.valueOf(mPlaylist), from, to);
当满足条件后就会调用drop方法。在TouchInterceptor中:首先将监听器赋值,
public void setDropListener(DropListener l) {
mDropListener = l; }
public boolean onTouchEvent(MotionEvent ev){
..............
if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) {
mDropListener.drop(mSrcDragPos, mDragPos);
}
当满足判断条件时,调用mDropListener的drop方法。而实际上这是一个接口回调,mDropListener是DropListener的一个引用。
public interface DropListener {
void drop(int from, int to);
}
最后回调TrackborwserActivity中的mDropListener 的drop实现方法,完成整个监听的过程。
在整个android系统中,存在着大量的设计模式,学习好设计模式对代码架构可以有很好的把握。
最后附上增加版的拖动item重新排序listview的列子