服务器之家

服务器之家 > 正文

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

时间:2021-06-23 15:15     来源/作者:shoneworn

我这里只是简单的用了两个listview来实现的,先上效果图。比较粗糙。预留了自定义的空间。

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

思路:

从上图应该可以看的出来。就是上下两个listview。点击下面的ltem。会动态的移动到上一个listview的最后。上面的listview 为listview1,下面的为listview2. 点击listview2,获取到view ,设置一个动画,移动到listview1 ,listview2中删除被点的item。listview1中新增一个。

上代码:

mainactivity.java 部分

?
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
package com.example.testlistanimator;
import java.util.arraylist;
import java.util.list;
import android.animation.animator;
import android.animation.animator.animatorlistener;
import android.animation.objectanimator;
import android.animation.valueanimator;
import android.annotation.suppresslint;
import android.annotation.targetapi;
import android.app.activity;
import android.graphics.bitmap;
import android.os.build;
import android.os.bundle;
import android.os.handler;
import android.view.view;
import android.view.viewgroup;
import android.view.viewgroup.layoutparams;
import android.view.animation.animation;
import android.view.animation.animation.animationlistener;
import android.view.animation.animationset;
import android.view.animation.translateanimation;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.textview;
@suppresslint("newapi")
@targetapi(build.version_codes.honeycomb)
public class mainactivity extends activity {
// listview1
private listview mlv1 = null;
// listview2
private listview mlv2 = null;
// list1的adapter
private lsadapter1 madapter1 = null;
// list2的adapter
private lsadapter2 madapter2 = null;
// 支持的刷卡头
string[] arrsupportshua = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期天"};
list<string> mlist1 = new arraylist<string>();
list<string> mlist2 = new arraylist<string>();
/** 是否在移动,由于这边是动画结束后才进行的数据更替,设置这个限制为了避免操作太频繁造成的数据错乱。 */
boolean ismove = false;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
initview();
initdata();
initlistener();
}
private void initview() {
mlv1 = (listview) findviewbyid(r.id.list1);
mlv2 = (listview) findviewbyid(r.id.list2);
}
private void makelist() {
for (string shua : arrsupportshua) {
mlist2.add(shua);
}
}
private void initdata() {
makelist();
madapter1 = new lsadapter1(mainactivity.this, mlist1);
madapter2 = new lsadapter2(mainactivity.this, mlist2);
mlv1.setadapter(madapter1);
mlv2.setadapter(madapter2);
}
private void initlistener() {
mlv1.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view view, final int location, long arg3) {
//如果点击的时候,之前动画还没结束,那么就让点击事件无效
if(ismove){
return;
}
final imageview img = getview(view);
textview mtv = (textview) view.findviewbyid(r.id.item_tv);
final int[] startlocation = new int[2];
mtv.getlocationinwindow(startlocation);
final string mshua = mlist1.get(location);
madapter2.setvisible(false);
madapter2.additem(mshua);
new handler().postdelayed(new runnable() {
public void run() {
try {
int[] endlocation = new int[2];
// 获取终点的坐标
mlv2.getchildat(mlv2.getlastvisibleposition()).getlocationinwindow(endlocation);
moveanim(img, startlocation, endlocation, mshua, 1);
madapter1.setremove(location);
} catch (exception localexception) {
}
}
}, 50l);
}
});
mlv2.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view view, final int location, long arg3) {
//如果点击的时候,之前动画还没结束,那么就让点击事件无效
if(ismove){
return;
}
final imageview img = getview(view);
textview mtv = (textview) view.findviewbyid(r.id.item_tv);
final int[] startlocation = new int[2];
mtv.getlocationinwindow(startlocation);
final string mshua = mlist2.get(location);
madapter1.setvisible(false);
madapter1.additem(mshua);
new handler().postdelayed(new runnable() {
public void run() {
try {
int[] endlocation = new int[2];
// 获取终点的坐标
mlv1.getchildat(mlv1.getlastvisibleposition()).getlocationinwindow(endlocation);
moveanim(img, startlocation, endlocation, mshua, 2);
madapter2.setremove(location);
} catch (exception localexception) {
}
}
}, 50l);
}
});
}
private void moveanim(imageview moveview, int[] startlocation, int[] endlocation, string mshua, final int code) {
int[] initlocation = new int[2];
// 获取传递过来的view的坐标
moveview.getlocationinwindow(initlocation);
// 得到要移动的view,并放入对应的容器中
final viewgroup moveviewgroup = getmoveviewgroup();
final view mmoveview = getmoveview(moveviewgroup, moveview, initlocation);
//使用objectanimator动画
objectanimator manimator = objectanimator.offloat(mmoveview, "translationy", startlocation[1],endlocation[1]);
manimator.setduration(300);
manimator.start();
ismove = true;
manimator.addlistener(new animatorlistener() {
@override
public void onanimationstart(animator animation) {
ismove = true;
}
@override
public void onanimationrepeat(animator animation) {
}
@override
public void onanimationend(animator animation) {
moveviewgroup.removeview(mmoveview);
if(code==1){
madapter2.setvisible(true);
madapter2.notifydatasetchanged();
madapter1.remove();
ismove = false;
}else{
madapter1.setvisible(true);
madapter1.notifydatasetchanged();
madapter2.remove();
ismove = false;
}
}
@override
public void onanimationcancel(animator animation) {
}
});
//使用translateanimation。上面部分可以用这部分替换
/* // 创建移动动画
translateanimation moveanimation = new translateanimation(startlocation[0], endlocation[0], startlocation[1],
endlocation[1]);
moveanimation.setduration(300l);// 动画时间
// 动画配置
animationset moveanimationset = new animationset(true);
moveanimationset.setfillafter(false);// 动画效果执行完毕后,view对象不保留在终止的位置
moveanimationset.addanimation(moveanimation);
mmoveview.startanimation(moveanimationset);
moveanimationset.setanimationlistener(new animationlistener() {
@override
public void onanimationstart(animation animation) {
ismove = true;
}
@override
public void onanimationrepeat(animation animation) {
}
@override
public void onanimationend(animation animation) {
moveviewgroup.removeview(mmoveview);
// instanceof 方法判断2边实例是不是一样,判断点击的是draggrid还是othergridview
if(code==1){
madapter2.setvisible(true);
madapter2.notifydatasetchanged();
madapter1.remove();
ismove = false;
}else{
madapter1.setvisible(true);
madapter1.notifydatasetchanged();
madapter2.remove();
ismove = false;
}
}
});*/
}
/**
* 创建移动的item对应的viewgroup布局容器
*/
private viewgroup getmoveviewgroup() {
viewgroup moveviewgroup = (viewgroup) getwindow().getdecorview();
linearlayout movelinearlayout = new linearlayout(this);
movelinearlayout
.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent));
moveviewgroup.addview(movelinearlayout);
return movelinearlayout;
}
/**
* 获取点击的item的对应view,
*
* @param view
* @return
*/
private imageview getview(view view) {
view.destroydrawingcache();
view.setdrawingcacheenabled(true);
bitmap cache = bitmap.createbitmap(view.getdrawingcache());
view.setdrawingcacheenabled(false);
imageview iv = new imageview(this);
iv.setimagebitmap(cache);
return iv;
}
/**
* 获取移动的view,放入对应viewgroup布局容器
*
* @param viewgroup
* @param view
* @param initlocation
* @return
*/
private view getmoveview(viewgroup viewgroup, view view, int[] initlocation) {
int x = initlocation[0];
int y = initlocation[1];
viewgroup.addview(view);
linearlayout.layoutparams mlayoutparams = new linearlayout.layoutparams(layoutparams.wrap_content,
layoutparams.wrap_content);
mlayoutparams.leftmargin = x;
mlayoutparams.topmargin = y;
view.setlayoutparams(mlayoutparams);
return view;
}
}

两个adapter部分。两个差不都。传一个

?
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
package com.example.testlistanimator;
import java.util.list;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.textview;
public class lsadapter1 extends baseadapter {
private context mcontext;
private list<string> mlist;
private layoutinflater minflater = null;
private boolean isvisible = true;
/** 要删除的position */
public int remove_position = -1;
private int[] bg = {r.drawable.a1,r.drawable.a2,r.drawable.a3,r.drawable.a4,r.drawable.a5,r.drawable.a6,r.drawable.a7};
public lsadapter1(context mcontext, list<string> mlist) {
this.mcontext = mcontext;
this.mlist = mlist;
minflater = layoutinflater.from(mcontext);
}
@override
public int getcount() {
if (mlist != null)
return mlist.size();
return 0;
}
@override
public object getitem(int position) {
if (mlist != null)
return mlist.get(position);
return null;
}
@override
public long getitemid(int position) {
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
view view = minflater.inflate(r.layout.list_item, null);
textview tv = (textview) view.findviewbyid(r.id.item_tv);
tv.setbackgroundresource(bg[position]);
tv.settext(mlist.get(position));
if (!isvisible && (position == -1 + mlist.size())) {
tv.settext("");
}
if (remove_position == position) {
tv.settext("");
}
return view;
}
public void additem(string mshua) {
mlist.add(mshua);
notifydatasetchanged();
}
public void setvisible(boolean isvisible) {
this.isvisible = isvisible;
}
/** 设置删除的position */
public void setremove(int position) {
remove_position = position;
notifydatasetchanged();
}
/** 删除频道列表 */
public void remove() {
// system.out.println("list1="+mlist.size()+" remove_position ="+remove_position);
if(remove_position>=0||remove_position<mlist.size())
mlist.remove(remove_position);
remove_position = -1;
notifydatasetchanged();
}
}

以上内容是小编给大家介绍的android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中的全部知识,希望对大家有所帮助!

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部