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 [email protected]}

 */

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

Android高仿QQ消息滑动删除(附源码),布布扣,bubuko.com

时间: 2024-10-07 06:32:49

Android高仿QQ消息滑动删除(附源码)的相关文章

android 下拉刷新上拉加载更多,高仿ios左滑动删除item,解决了众多手势问题

一.前言 老规矩,别的不说,这demo是找了很相关知识集合而成的,可以说对我这种小白来说是绞尽脑汁!程序员讲的是无图无真相!现在大家一睹为快! 二.比较关键的还是scroller这个类的 package com.icq.slideview.view; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; i

js高仿QQ消息列表左滑功能

该组件,主要功能类似于QQ消息列表左滑出现删除.标为已读等按钮的功能:现在的版本用的是纯javaScript编写:后续会跟进 angularJs 开发的类似组件以及jquery的; 下面,就让我们来认识下怎么使用该程序: 在该程序里,总共分为四个文件: 1 .css文件夹 2. img 图片文件夹 3. js文件 4. index.html  主页面: 稍后,你可以自行下载,打开运行观看效果: 二.代码讲解 1.此html结构,为不可修改结构 <ul class="list-ul"

Android 使用ViewPager真正实现左右无限滑动(附源码)

前言 网上有很多使用ViewPager实现左右滑动这一效果的资料,这些资料大多数都是将PagerAdapter中getCount()方法的返回值设为Integer.MAX_VALUE使用户看不到边界,然后在instantiateItem()方法中通过position%(要循环显示的数据集的长度)的方式取得对应的数据集.这样虽然可以做到无限循环,但是会有两个弊端:首先会创建大量对象,容易引起内存溢出(循环加载图片)从而影响性能:其次从第一页向右滑动的时候是无法滑动的. 今天给大家分享的是另一种实现

Android中常见Intent习惯用法-上篇(附源码下载)

Android中的Intent是一个非常重要的类,如果对Intent不是特别了解,可以参见博文<Android中Intent概述及使用>.如果对Intent Filter不是特别了解,可以参见博文<Android中Intent对象与Intent Filter过滤匹配过程详解>. 本文着重讲一下Android中一些常见的Intent的习惯用法,比如如何通过Intent发送短信.发送邮件.启动摄像机拍照录视频.设置闹铃.打开WIFI设置界面等等. 限于篇幅,本博文分为上下两篇,这是上篇

Android中AIDL实现进程通信(附源码下载)

AIDL概述 之前的博客<Android中通过Messenger与Service实现进程间双向通信>演示了如何通过Messenger实现与Service进行跨进程通信,即IPC.但用Messenger实现的IPC存在一点不足:Service内部维护着一个Messenger,Messenger内部又维护着一个Hanlder,当多个client向该Service发送Message时,这些Message需要依次进入Hanlder的消息队列中,Hanlder只能处理完一个Message之后,再从消息队

转载—— android 瀑布流的实现详解,附源码

介绍 参考自:https://github.com/dodola/android_waterfall,因为原来的代码封装不好,所以,我根据源码的思路,重新写了一遍,所以有了现在这个项目:https://github.com/youxilua/waterfall4android 原作者表示: 试过在1万张可以流畅的滑动,不出现内存溢出情况 设计思路 之前的作者的自定义view 只有主滑动一层,其他的设置要在相应的活动设置,个人觉得,重用起来比较麻烦,所以决定封装一层.现在定义一个默认的瀑布流只需5

【Android数据存储】SQLite使用实例(附源码)(转载)

原文地址:http://blog.csdn.net/wirelessqa/article/details/8583101 java当中提供了相当多的封装好的类来执行对SqlLite的操作,如: SQLiteDatabase 作为数据库操作的类,类似于C#当中我们自己写的helper,PS(java对此还真是高度封装) SQLiteOpenHelper 作为作为维护和管理数据库的基类实例: 会员信息管理 功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员

C#微信公众号开发之模板消息开发,附源码

个人觉得模板消息功能的增加对公众号的作用非常大,可以说是真正意义上的实现了所谓的轻app,商家可以通过模板消息给用户发送重要的信息,交易.预约.消费.邮件.物流等信息.之前我做过的系统通过邮件发送订单消息,但是对于一些不喜欢绑定手机邮箱(实时提现商家发货买家订单进程等)的人来说还是不够方便,有了模板消息,公众号得以独立的完成交易信息通知跟踪等. 开发文档:http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html C

高仿ios版美团框架项目源码

高仿美团框架基本已搭好.代码简单易懂,适合新人.适合新人.新人. <ignore_js_op>   源码你可以到ios教程网那里下载吧,这里我就不上传了,http://ios.662p.com/thread-2774-1-1.html