ListView的Item中的图片拖拽功能的实现

    最近项目中要用到,所以做了个,刚开始做出来,可能有些地方还没有考虑完整,不过可以拿来参考一下。其实最主要的就是使用了一个自定义的BaseAdapter,在其中的getView方法中,为模板中的图片控件增加了onTouchListener事件即可实现。

    运行效果如下:

 
主布局文件:
1  <? xml version="1.0" encoding="utf-8" ?>
2  < LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
3      android:orientation ="vertical"  android:layout_width ="fill_parent"
4      android:layout_height ="fill_parent" >
5       < ListView  android:id ="@+id/lvDemo"  android:layout_width ="fill_parent"
6          android:layout_height ="wrap_content"   />
7  </ LinearLayout >

主布局文件中列表的项模板如下:

 1  <? xml version="1.0" encoding="utf-8" ?>
 2  < LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
 3      android:layout_width ="fill_parent"  android:layout_height ="wrap_content"
 4      android:orientation ="horizontal"  android:layout_gravity ="center"
 5      android:paddingTop ="5dp"  android:paddingBottom ="5dp" >
 6       < FrameLayout  android:id ="@+id/flContainer"
 7          android:layout_width ="fill_parent"  android:layout_height ="wrap_content" >
 8           < TextView  android:id ="@+id/tvShowTitle"  android:layout_width ="wrap_content"
 9              android:layout_height ="wrap_content"  android:paddingLeft ="110dp"
10              android:paddingTop ="10dp"  android:textSize ="16sp" >
11           </ TextView >
12           < ImageView  android:id ="@+id/ivLogo"  android:layout_width ="100dp"
13              android:layout_height ="80dp"   />
14           < ImageView  android:id ="@+id/ivAlphaImg"
15              android:layout_width ="80dp"  android:background ="@drawable/pi"
16              android:layout_height ="80dp"   />
17       </ FrameLayout >
18  </ LinearLayout >

自定义适配器:

  1  package  com.studio.basf.animationdemo;
  2 
  3  import  java.util.ArrayList;
  4  import  java.util.HashMap;
  5 
  6  import  android.content.Context;
  7  import  android.util.DisplayMetrics;
  8  import  android.view.LayoutInflater;
  9  import  android.view.MotionEvent;
 10  import  android.view.View;
 11  import  android.view.View.OnTouchListener;
 12  import  android.view.ViewGroup;
 13  import  android.widget.BaseAdapter;
 14  import  android.widget.ImageView;
 15  import  android.widget.TextView;
 16 
 17  public   class  CustomAdapter  extends  BaseAdapter  implements  OnTouchListener {
 18 
 19       private  LayoutInflater mInflater;
 20       private  ViewHolder holder;
 21       private  ArrayList < HashMap < String, String >>  list;
 22       private   int  resource, lastX, lastY, vLeft, vTop,vRight, vBottom;
 23       private  HashMap < String, Object >  hashMap;
 24       private  String[] from;
 25       private   int [] to;
 26       private  Context context;
 27 
 28       //  自定义容器类
 29       class  ViewHolder {
 30 
 31           public  ArrayList < ImageView >  getIvCollections() {
 32               return  ivCollections;
 33          }
 34 
 35           public   void  setIvCollections(ArrayList < ImageView >  ivCollections) {
 36               this .ivCollections  =  ivCollections;
 37          }
 38 
 39           public  TextView getTvShowTitle() {
 40               return  tvShowTitle;
 41          }
 42 
 43           public   void  setTvShowTitle(TextView tvShowTitle) {
 44               this .tvShowTitle  =  tvShowTitle;
 45          }
 46 
 47           private  ArrayList < ImageView >  ivCollections;
 48           private  TextView tvShowTitle;
 49      }
 50 
 51       public  CustomAdapter(Context context,
 52              ArrayList < HashMap < String, String >>  list,  int  resource,
 53              String[] from,  int [] to) {
 54           this .list  =  list;
 55           this .mInflater  =  LayoutInflater.from(context);
 56           this .resource  =  resource;
 57           this .from  =  from;
 58           this .to  =  to;
 59           this .hashMap  =   new  HashMap < String, Object > ();
 60           this .context  =  context;
 61      }
 62 
 63      @Override
 64       public   int  getCount() {
 65           //  TODO Auto-generated method stub
 66           return  list.size();
 67      }
 68 
 69      @Override
 70       public  Object getItem( int  position) {
 71           //  TODO Auto-generated method stub
 72           return  list.get(position);
 73      }
 74 
 75      @Override
 76       public   long  getItemId( int  position) {
 77           //  TODO Auto-generated method stub
 78           return  position;
 79      }
 80 
 81      @Override
 82       public  View getView( int  position, View convertView, ViewGroup parent) {
 83           //  TODO Auto-generated method stub
 84           if  (convertView  ==   null ) {
 85              holder  =   new  ViewHolder();
 86              convertView  =  mInflater.inflate(resource,  null );
 87 
 88              ArrayList < ImageView >  ivList  =   new  ArrayList < ImageView > ();
 89              ivList.add((ImageView) convertView.findViewById(R.id.ivLogo));
 90              ivList.add((ImageView) convertView.findViewById(R.id.ivAlphaImg));
 91               //  装载ImageView
 92              holder.setIvCollections(ivList);
 93               //  装载TextView
 94              holder.setTvShowTitle((TextView) convertView.findViewById(to[ 0 ]));
 95 
 96              convertView.setTag(holder);
 97          }  else  {
 98              holder  =  (ViewHolder) convertView.getTag();
 99          }
100 
101           //  获取单项数据
102          HashMap < String, String >  hashMap  =  list.get(position);
103 
104          holder.getIvCollections().get( 0 )
105                  .setBackgroundResource(R.drawable.anim2);
106          holder.getIvCollections().get( 0 ).setOnTouchListener( this );
107 
108          holder.getIvCollections().get( 1 ).getBackground().setAlpha( 200 );
109          holder.getTvShowTitle().setText(hashMap.get(from[ 0 ]));
110 
111           return  convertView;
112      }
113 
114      @Override
115       public   boolean  onTouch(View v, MotionEvent event) {
116           //  TODO Auto-generated method stub
117           switch  (event.getAction()) {
118           case  MotionEvent.ACTION_DOWN:
119              lastX  =  ( int ) event.getRawX();
120              lastY  =  ( int ) event.getRawY();
121 
122               if  (hashMap.isEmpty()) {
123                  hashMap.put( " l " , v.getLeft());
124                  hashMap.put( " t " , v.getTop());
125                  hashMap.put( " r " , GetScreenWidth());
126                  hashMap.put( " b " , v.getBottom());
127              }
128              
129               //  获取遮罩层的位置
130              vLeft  =  Integer.parseInt(hashMap.get( " l " ).toString());
131              vTop  =  Integer.parseInt(hashMap.get( " t " ).toString());
132              vRight  =  Integer.parseInt(hashMap.get( " r " ).toString());
133              vBottom  =  Integer.parseInt(hashMap.get( " b " ).toString());
134 
135               break ;
136           case  MotionEvent.ACTION_MOVE:
137               int  dx  =  ( int ) event.getRawX()  -  lastX;
138               int  dy  =  ( int ) event.getRawY()  -  lastY;
139 
140               int  left  =  v.getLeft()  +  dx;
141               int  top  =  v.getTop()  +  dy;
142               int  right  =  v.getRight()  +  dx;
143               int  bottom  =  v.getBottom()  +  dy;
144 
145               if  (left  <  vLeft) {
146                  left  =  vLeft;
147                  right  =  left  +  v.getWidth();
148              }
149 
150               if  (top  <  vTop) {
151                  top  =  vTop;
152                  bottom  =  top  +  v.getHeight();
153              }
154              
155               if (right  >  vRight){
156                  right  =  vRight;
157                  left  =  right  -  v.getWidth();
158              }
159              
160               if  (bottom  >  vBottom) {
161                  bottom  =  vBottom;
162                  top  =  bottom  -  v.getHeight();
163              }
164 
165              v.layout(left, top, right, bottom);
166 
167              lastX  =  ( int ) event.getRawX();
168              lastY  =  ( int ) event.getRawY();
169 
170               break ;
171           case  MotionEvent.ACTION_UP:
172               break ;
173          }
174           return   true ;
175      }
176 
177       /**
178       * 获取屏幕宽度,将其作为图片拖拽到屏幕最右端时的终止坐标
179       *  @return  屏幕宽度
180        */
181       private   int  GetScreenWidth(){
182          DisplayMetrics dm  =  context.getResources().getDisplayMetrics();
183           return  dm.widthPixels;
184      }
185  }

调用代码如下:

package  com.studio.basf.animationdemo;

import  java.util.ArrayList;
import  java.util.HashMap;

import  android.app.Activity;
import  android.os.Bundle;
import  android.widget.ListView;

public   class  MainActivity  extends  Activity {
    
/**  Called when the activity is first created.  */
    
private  ArrayList < HashMap < String,String >>  list;
    
private  ListView lvDemo;
    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        list 
=   new  ArrayList < HashMap < String,String >> ();
        lvDemo 
=  (ListView)findViewById(R.id.lvDemo);
        addDataTolvDemo();
    }

    
private   void  addDataTolvDemo() {
        
        HashMap
< String,String >  hashMap0  =   new  HashMap < String,String > ();
        hashMap0.put(
" title " " 不要乱猜哦,我是标题哈~~ " );
        list.add(hashMap0);
        
        HashMap
< String,String >  hashMap1  =   new  HashMap < String,String > ();
        hashMap1.put(
" title " " 不要乱猜哦,我是标题哈~~ " );
        list.add(hashMap1);
        
        HashMap
< String,String >  hashMap2  =   new  HashMap < String,String > ();
        hashMap2.put(
" title " " 不要乱猜哦,我是标题哈~~ " );
        list.add(hashMap2);
        
        HashMap
< String,String >  hashMap3  =   new  HashMap < String,String > ();
        hashMap3.put(
" title " " 不要乱猜哦,我是标题哈~~ " );
        list.add(hashMap3);
        
        HashMap
< String,String >  hashMap4  =   new  HashMap < String,String > ();
        hashMap4.put(
" title " " 不要乱猜哦,我是标题哈~~ " );
        list.add(hashMap4);
        
        CustomAdapter adapter 
=   new  CustomAdapter( this , list,
                R.layout.main_item, 
new  String[] {  " title "  },
                
new   int [] { R.id.tvShowTitle });
        lvDemo.setAdapter(adapter);
    }
}

转载于:https://www.cnblogs.com/jewleo/archive/2011/07/14/20110714_1.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值