首页 > 代码库 > Android高仿QQ消息滑动删除(附源码)

Android高仿QQ消息滑动删除(附源码)

大家都应该使用过QQ吧,他的消息中可以滑动删除功能,我觉得比较有意思,所以模仿写了一个,并且修改了其滑动算法.我先贴几个简单示范图吧

其实主要用的是算法以及对ListView的把控.

一下是适配器的类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.fay.adapter;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.fay.activity.MessageActivity;
import com.fay.message.R;
 
/**
 * the adapter of the ListView
 * @author Fay
 * {@link 1940125001@qq.com}
 */
public class ListMessageAdapter extends BaseAdapter {
    private String TAG = "ListMessageAdapter";
    private Context context = null;
    private Holder holder = null;
    private LayoutInflater inflater = null;
 
    // the last position clicked
    private int mLastPosition = -1;
 
    // check whether a touch action is finish
    private boolean loadFinish = false;
 
    // the position of click and move, start and end point
    private Point startPoint, endPoint;
 
    // the animation of removing the item
    private Animation animation = null;
 
    // the children item is common
    private final int TYPE_ITEM = 0;
 
    // the children item is searching
    private final int TYPE_SEARCH = 1;
 
    // the count of children item‘s type
    private final int TYPE_COUNT = TYPE_SEARCH + 1;
 
    // data container
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
    public ListMessageAdapter(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.startPoint = new Point();
        this.endPoint = new Point();
        animation = AnimationUtils.loadAnimation(context, R.anim.push_out);
    }
 
    public void setData(ArrayList<Map<String, Object>> list) {
        this.list = list;
        notifyDataSetChanged();
    }
 
    public void setDataTemp(List<Map<String, Object>> list) {
        this.list = list;
        notifyDataSetChanged();
    }
 
    public void clearData() {
        list.clear();
        notifyDataSetChanged();
    }
 
    public int getCount() {
        return list.size();
    }
 
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_SEARCH : TYPE_ITEM;
    }
 
    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        if (null == convertView) {
            holder = new Holder();
            switch (type) {
            case TYPE_SEARCH:
                convertView = inflater.inflate(R.layout.search, null);
                holder.searchview = (LinearLayout) convertView
                        .findViewById(R.id.search);
                break;
            case TYPE_ITEM:
                convertView = inflater
                        .inflate(R.layout.list_item_message, null);
                holder.linearlayout = (LinearLayout) convertView
                        .findViewById(R.id.message_linear);
                holder.content = (TextView) convertView
                        .findViewById(R.id.message_detail);
                holder.delete = (TextView) convertView
                        .findViewById(R.id.message_delete);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
 
        final int chickPosition = position;
 
        switch (type) {
        case TYPE_SEARCH:
            // hidden the top view
            holder.searchview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    MessageActivity.hiddenTop();
                }
            });
            break;
        case TYPE_ITEM:
            holder.content.setText("消息" + list.get(chickPosition).get("index") + ":你好哦,我想你咯");
            final int finalPosition = position;
            if (position == mLastPosition) {
                holder.delete.setVisibility(View.VISIBLE);
            } else {
                holder.delete.setVisibility(View.GONE);
            }
 
            // 删除
            final View view = holder.linearlayout;
 
            holder.delete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
 
                    view.startAnimation(animation);
                    animation.setAnimationListener(new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation arg0) {
                        }
 
                        @Override
                        public void onAnimationRepeat(Animation arg0) {
                        }
 
                        @Override
                        public void onAnimationEnd(Animation arg0) {
                            list.remove(chickPosition);
                            mLastPosition = -1;
                            notifyDataSetChanged();
                        }
                    });
                }
            });
 
            holder.linearlayout.setOnTouchListener(new OnTouchListener() {
 
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        loadFinish = false;
                        startPoint.set((int) event.getX(), (int) event.getY());
                        break;
                    case MotionEvent.ACTION_MOVE:
                        endPoint.set((int) event.getX(), (int) event.getY());
                        if (Math.abs(endPoint.x - startPoint.x) > 30) {
                            if (loadFinish == false) {
                                loadFinish = true;
                                if (finalPosition != mLastPosition) {
                                    mLastPosition = finalPosition;
                                } else {
                                    mLastPosition = -1;
                                }
                                notifyDataSetChanged();
                            } else {
                                return true;
                            }
                            return true;
                        }
                        if (Math.abs(endPoint.y - startPoint.y) > 30) {
                            return false;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    }
                    return false;
                }
            });
 
            holder.linearlayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mLastPosition != -1) {
                        mLastPosition = -1;
                        notifyDataSetChanged();
                    } else {
                        Toast.makeText(context, "您点击了 -> " + chickPosition, 2000).show();
                    }
                }
            });
            break;
        }
        return convertView;
    }
 
    private class Holder {
        TextView content;
        TextView delete;
        LinearLayout linearlayout;
        LinearLayout searchview;
    }
 
}

 多余的我不说了,你们直接看源码吧.

  源码下载地址:http://files.cnblogs.com/yinweiliang/Message.zip