android listview拖拽,拖动item 改变位置

本文介绍如何在Android中实现ListView的拖动效果,通过touchIncepter.java类提供拖拽功能,并详细说明了实现源码音乐播放器般流畅拖动的两个关键点:item布局高度设置及在底部位置的要求。
摘要由CSDN通过智能技术生成

在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
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值