服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 仿IOS效果 带弹簧动画的ListView

仿IOS效果 带弹簧动画的ListView

2021-01-03 16:39于连林520wcf IOS

这篇文章主要介绍了仿IOS效果,带弹簧动画的ListView,感兴趣的小伙伴们可以参考一下

最近项目打算做一个界面,类似于dayone首页的界面效果,dayone 是一款付费应用,目前只有ios端。作为一个资深懒惰的程序员,奉行的宗旨是绝对不重复造一个轮子。于是乎,去网上找一大堆开源项目,发现没有找到合适的,然后,只能硬着头皮自己来了。先看看效果:

仿IOS效果 带弹簧动画的ListView

效果图

其实写起来也比较简单,就是控制listview的头部和底部的高度就可以了, 如果用recycleview实现起来也是一样,只是recycleview添加头和尾巴稍微麻烦一点,处理点击事件也不是很方便,所以就基于listview去实现了。实现的代码, 我已经上传到github上了。

1、使用方法

compile 'com.a520wcf.yllistview:yllistview:1.0.1

2、使用介绍:
1)、布局:
布局注意一个小细节android:layout_height 最好是match_parent, 否则listview每次滑动的时候都有可能需要重新计算条目高度,比较耗费cpu;

?
1
2
3
4
5
<com.a520wcf.yllistview.yllistview
android:divider="@android:color/transparent"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

2)、代码:

?
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
private yllistview listview;
@override
protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 listview = (yllistview) findviewbyid(r.id.listview);
 // 不添加也有默认的头和底
 view topview=view.inflate(this,r.layout.top,null);
 listview.addheaderview(topview);
 view bottomview=new view(getapplicationcontext());
 listview.addfooterview(bottomview);
 
 // 顶部和底部也可以固定最终的高度 不固定就使用布局本身的高度
 listview.setfinalbottomheight(100);
 listview.setfinaltopheight(100);
 
 listview.setadapter(new demoadapter());
 
 //yllistview默认有头和底 处理点击事件位置注意减去
 listview.setonitemclicklistener(new adapterview.onitemclicklistener() {
  @override
  public void onitemclick(adapterview<?> parent, view view, int position, long id) {
   position=position-listview.getheaderviewscount();
  }
 });
}


3、源码介绍
其实这个项目里面只有一个类,大家不需要依赖,直接把这个类复制到项目中就可以了,来看看源码:

?
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
247
248
249
package com.a520wcf.yllistview;
 
import android.content.context;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.view.viewtreeobserver.ongloballayoutlistener;
import android.view.animation.decelerateinterpolator;
import android.widget.abslistview;
import android.widget.listview;
import android.widget.scroller;
 
public class yllistview extends listview implements abslistview.onscrolllistener {
 private scroller mscroller; // used for scroll back
 private float mlasty = -1;
 
 private int mscrollback;
 private final static int scrollback_header = 0;
 private final static int scrollback_footer = 1;
 
 private final static int scroll_duration = 400; // scroll back duration
 private final static float offset_radio = 1.8f;
 // total list items, used to detect is at the bottom of listview.
 private int mtotalitemcount;
 private view mheaderview; // 顶部图片
 private view mfooterview; // 底部图片
 private int finaltopheight;
 private int finalbottomheight;
 
 public yllistview(context context) {
  super(context);
  initwithcontext(context);
 }
 
 public yllistview(context context, attributeset attrs) {
  super(context, attrs);
  initwithcontext(context);
 }
 
 public yllistview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
  initwithcontext(context);
 }
 
 private void initwithcontext(context context) {
  mscroller = new scroller(context, new decelerateinterpolator());
  super.setonscrolllistener(this);
 
  this.getviewtreeobserver().addongloballayoutlistener(
    new ongloballayoutlistener() {
     @override
     public void ongloballayout() {
      if(mheaderview==null){
       view view=new view(getcontext());
       addheaderview(view);
      }
      if(mfooterview==null){
       view view=new view(getcontext());
       addfooterview(view);
      }
      getviewtreeobserver()
        .removeglobalonlayoutlistener(this);
     }
    });
 }
 
 @override
 public boolean ontouchevent(motionevent ev) {
  if (mlasty == -1) {
   mlasty = ev.getrawy();
  }
  switch (ev.getaction()) {
   case motionevent.action_down:
    mlasty = ev.getrawy();
    break;
   case motionevent.action_move:
    final float deltay = ev.getrawy() - mlasty;
    mlasty = ev.getrawy();
    if (getfirstvisibleposition() == 0 && (mheaderview.getheight() > finaltopheight || deltay > 0)
      && mheaderview.gettop() >= 0) {
     // the first item is showing, header has shown or pull down.
     updateheaderheight(deltay / offset_radio);
    } else if (getlastvisibleposition() == mtotalitemcount - 1
      && (getfootheight() >finalbottomheight || deltay < 0)) {
     updatefooterheight(-deltay / offset_radio);
    }
    break;
   default:
    mlasty = -1; // reset
    if (getfirstvisibleposition() == 0 && getheaderheight() > finaltopheight) {
     resetheaderheight();
    }
    if (getlastvisibleposition() == mtotalitemcount - 1 ){
      if(getfootheight() > finalbottomheight) {
       resetfooterheight();
      }
    }
    break;
  }
  return super.ontouchevent(ev);
 }
 
 /**
  * 重置底部高度
  */
 private void resetfooterheight() {
  int bottomheight = getfootheight();
  if (bottomheight > finalbottomheight) {
   mscrollback = scrollback_footer;
   mscroller.startscroll(0, bottomheight, 0, -bottomheight+finalbottomheight,
     scroll_duration);
   invalidate();
  }
 }
 // 计算滑动 当invalidate()后 系统会自动调用
 @override
 public void computescroll() {
  if (mscroller.computescrolloffset()) {
   if (mscrollback == scrollback_header) {
    setheaderheight(mscroller.getcurry());
   } else {
    setfooterviewheight(mscroller.getcurry());
   }
   postinvalidate();
  }
  super.computescroll();
 }
 // 设置顶部高度
 private void setheaderheight(int height) {
  layoutparams layoutparams = (layoutparams) mheaderview.getlayoutparams();
  layoutparams.height = height;
  mheaderview.setlayoutparams(layoutparams);
 }
 // 设置底部高度
 private void setfooterviewheight(int height) {
  layoutparams layoutparams =
    (layoutparams) mfooterview.getlayoutparams();
  layoutparams.height =height;
  mfooterview.setlayoutparams(layoutparams);
 }
 // 获取顶部高度
 public int getheaderheight() {
  abslistview.layoutparams layoutparams =
    (abslistview.layoutparams) mheaderview.getlayoutparams();
  return layoutparams.height;
 }
 // 获取底部高度
 public int getfootheight() {
  abslistview.layoutparams layoutparams =
    (abslistview.layoutparams) mfooterview.getlayoutparams();
  return layoutparams.height;
 }
 
 private void resetheaderheight() {
  int height = getheaderheight();
  if (height == 0) // not visible.
   return;
  mscrollback = scrollback_header;
  mscroller.startscroll(0, height, 0, finaltopheight - height,
    scroll_duration);
  invalidate();
 }
 
 /**
  * 设置顶部高度 如果不设置高度,默认就是布局本身的高度
  * @param height 顶部高度
  */
 public void setfinaltopheight(int height) {
  this.finaltopheight = height;
 }
 /**
  * 设置底部高度 如果不设置高度,默认就是布局本身的高度
  * @param height 底部高度
  */
 public void setfinalbottomheight(int height){
  this.finalbottomheight=height;
 }
 @override
 public void addheaderview(view v) {
  mheaderview = v;
  super.addheaderview(mheaderview);
  mheaderview.getviewtreeobserver().addongloballayoutlistener(
    new ongloballayoutlistener() {
     @override
     public void ongloballayout() {
      if(finaltopheight==0) {
       finaltopheight = mheaderview.getmeasuredheight();
      }
      setheaderheight(finaltopheight);
      getviewtreeobserver()
        .removeglobalonlayoutlistener(this);
     }
    });
 }
 
 @override
 public void addfooterview(view v) {
  mfooterview = v;
  super.addfooterview(mfooterview);
 
  mfooterview.getviewtreeobserver().addongloballayoutlistener(
    new ongloballayoutlistener() {
     @override
     public void ongloballayout() {
      if(finalbottomheight==0) {
       finalbottomheight = mfooterview.getmeasuredheight();
      }
      setfooterviewheight(finalbottomheight);
      getviewtreeobserver()
        .removeglobalonlayoutlistener(this);
     }
    });
 }
 
 private onscrolllistener mscrolllistener; // user's scroll listener
 
 @override
 public void setonscrolllistener(onscrolllistener l) {
  mscrolllistener = l;
 }
 
 @override
 public void onscrollstatechanged(abslistview view, int scrollstate) {
  if (mscrolllistener != null) {
   mscrolllistener.onscrollstatechanged(view, scrollstate);
  }
 }
 
 @override
 public void onscroll(abslistview view, int firstvisibleitem,
       int visibleitemcount, int totalitemcount) {
  // send to user's listener
  mtotalitemcount = totalitemcount;
  if (mscrolllistener != null) {
   mscrolllistener.onscroll(view, firstvisibleitem, visibleitemcount,
     totalitemcount);
  }
 }
 
 private void updateheaderheight(float delta) {
  setheaderheight((int) (getheaderheight()+delta));
  setselection(0); // scroll to top each time
 }
 
 private void updatefooterheight(float delta) {
  setfooterviewheight((int) (getfootheight()+delta));
 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01