listview 拖动item效果实现

本文详细介绍了如何在Android中实现ListView的拖动效果。通过自定义TouchInterceptor类,结合手势检测,实现拖动列表项的功能。示例代码包括主类SortableListViewActivity、布局文件以及触摸拦截器类,展示了拖动、交换位置和动画效果。
摘要由CSDN通过智能技术生成

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值