listview 拖动item效果实现
效果图如下:
拖拽前:
拖拽后:
首先参考源码中:TouchInterceptor 类,该类会在下面给出:
第一步:主类:
/**
*
*/
package com.example.draglistview;
/**
* @author JiaRH
*
* @date 2013-12-17 上午10:01:17
*/
import java.util.Arrays;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SortableListViewActivity extends ListActivity {
private TouchInterceptor mList;
private Object[] sArray = { "1", "2", "3", 4, 5,
"6", "7" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter adp = new ArrayAdapter(this, R.layout.list_layout, sArray);
setListAdapter(adp);
mList = (TouchInterceptor) getListView();
mList.setDropListener(mDropListener);
registerForContextMenu(mList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = sArray[position].toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
private TouchInterceptor.DropListener mDropListener =
new TouchInterceptor.DropListener() {
public void drop(int from, int to) {
System.out.println("Droplisten from:" + from + " to:" + to);
// Assuming that item is moved up the list
int direction = -1;
int loop_start = from;
int loop_end = to;
// For instance where the item is dragged down the list
if (from < to) {
direction = 1;
}
Object target = sArray[from];
for (int i = loop_start; i != loop_end; i = i + direction) {
sArray[i] = sArray[i + direction];
}
sArray[to] = target;
System.out.println("Changed array is:" + Arrays.toString(sArray));
((BaseAdapter) mList.getAdapter()).notifyDataSetChanged();
}
};
}
第二步: 主类布局文件;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.draglistview.TouchInterceptor
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="true"
android:textSize="18sp" >
</com.example.draglistview.TouchInterceptor>
</LinearLayout>
第三步; 引入TouchInterceptor 类,(注意:红色部分为要修改的地方)
/*
* 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.example.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.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.util.AttributeSet;
import android.util.Log;
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;
import android.widget.TextView;
public class TouchInterceptor extends ListView {
private ImageView mDragView;
private WindowManager mWindowManager;
private WindowManager.LayoutParams mWindowParams;