Android实现可拖动层叠卡片布局
作者:a__sen 时间:2023-03-25 03:28:06
公司app要求做一个扭蛋功能,其实就是一个可拖动层叠卡片列表,原理还是由一个自定义Recyclerview和LayoutManager来实现
自定义RecyclerView很简单,只是修改touch事件,防止点击到卡片外还被处理的情况
@Override
public boolean onTouchEvent(MotionEvent e) {
if(e.getY()< UIUtil.dip2px(TutuApplication.getInstance().getContext(),95)||e.getY()>getHeight()-UIUtil.dip2px(TutuApplication.getInstance().getContext(),95)){
if(e.getAction()!=MotionEvent.ACTION_UP && e.getAction()!=MotionEvent.ACTION_MOVE) {
return false;
}
}
return super.onTouchEvent(e);
}
实际的层叠效果还是需要LayoutManager来实现
public class SwipeCardLayoutManager extends RecyclerView.LayoutManager {
Context context;
int TRANS_Y_GAP;
public SwipeCardLayoutManager(Context context){
TRANS_Y_GAP= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,15,
context.getResources().getDisplayMetrics());
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
//1.如何实现层叠效果--cardView.layout(l,t,r,b)
//2.如何让8个条目中的4个展示在RecylerView里面
//1在布局layout之前,将所有的子View先全部detach掉,然后放到Scrap集合里面缓存。
detachAndScrapAttachedViews(recycler);
//2)只将最上面4个view添加到RecylerView容器里面
int itemCount=getItemCount();//8个
int bottomPosition;
if(itemCount< CardConfig.MAX_SHOW_COUNT){
bottomPosition=0;
}else{
bottomPosition=itemCount-CardConfig.MAX_SHOW_COUNT;
}
for(int i=bottomPosition;i<itemCount;i++){
View view=recycler.getViewForPosition(i);
addView(view);
measureChildWithMargins(view,0,0);
int widthSpace=getWidth()-getDecoratedMeasuredWidth(view);
int heightSpace=getHeight()-getDecoratedMeasuredHeight(view);
//摆放cardView
//层叠效果--Scale+TranslationY
//层级的位置关系1/2/3/4
int level=itemCount-i-1;
layoutDecorated(view,
widthSpace/2,
heightSpace/2+StatusBarUtil.getStatusBarHeight(TutuApplication.getInstance().getContext()),
widthSpace/2+getDecoratedMeasuredWidth(view),
heightSpace/2+StatusBarUtil.getStatusBarHeight(TutuApplication.getInstance().getContext())+getDecoratedMeasuredHeight(view));
if(level>0){
if(level<CardConfig.MAX_SHOW_COUNT){
view.setTranslationY(CardConfig.TRANS_V_GAP*level*1.3f);
view.setScaleX(1-CardConfig.SCALE_GAP*level);
view.setScaleY(1-CardConfig.SCALE_GAP*level);
}
}else {
view.setTranslationY(CardConfig.TRANS_V_GAP*(level-1));
view.setScaleX(1-CardConfig.SCALE_GAP*(level-1));
view.setScaleY(1-CardConfig.SCALE_GAP*(level-1));
}
}
}
}
显示出来就是这个样子
对于滑动显示下一张,则使用自定义ItemTouchHelper.simpleCallBack来展示
自定义itemTouchHelper.simpleCallBack
public class SwipeCardCallBack extends ItemTouchHelper.SimpleCallback {
private GameGachaAdapter adapter;
private RecyclerView mRv;
private OnSwipeEndListener listener;
private TextView tv;
private int x = 1;
private Context context;
public void refresh(){
// x = 1;
// tv.setText(context.getResources().getString(R.string.explored)+(++x)+"/????");
removeCard();
}
public SwipeCardCallBack(GameGachaAdapter adapter, RecyclerView mRv, TextView view, Context context) {
super(0,
ItemTouchHelper.LEFT | ItemTouchHelper.UP |
ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN
);
this.adapter = adapter;
this.mRv = mRv;
this.tv = view;
this.context = context;
}
public void addGameGachaList(List<IMulTypeHelper> mDatas){
adapter.addAdapterData(0,mDatas);
adapter.notifyDataSetChanged();
listener.onSwipe();
}
public SwipeCardCallBack(int dragDirs, int swipeDirs) {
super(dragDirs, swipeDirs);
}
public SwipeCardCallBack() {
/*
* 即我们对哪些方向操作关心。如果我们关心用户向上拖动,可以将
填充swipeDirs参数为LEFT | RIGHT 。0表示从不关心。
* */
super(0,
ItemTouchHelper.LEFT | ItemTouchHelper.UP |
ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN
);
}
@Override
public boolean onMove(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//当已经滑动删除了的时候会被回掉--删除数据,循环的效果
removeCard();
}
public void removeCard() {
if(adapter!=null && adapter.getItemCount()>0) {
adapter.removeAdapterData(adapter.getItemCount() - 1);
// mDatas.add(0, remove);
adapter.notifyDataSetChanged();
listener.onSwipe();
if (adapter.getItemCount() == 6) {
listener.onSwipeEnd();
}
tv.setText(context.getResources().getString(R.string.explored) + (++x) + "/????");
}
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
//监听话滑动的距离--控制动画的执行程度
if(dY == 0f&&!isCurrentlyActive){
int itemcount = recyclerView.getChildCount();
for (int i = 0; i < itemcount; i++) {
//执行
View view = recyclerView.getChildAt(i);
//几个view层叠的效果,错开的效果--便宜动画+缩放动画
int level = itemcount - i - 1;
view.setRotation(0);
if(dX == 0) {
if(level>0){
if(level<CardConfig.MAX_SHOW_COUNT){
view.setTranslationY(CardConfig.TRANS_V_GAP*level*1.3f);
view.setScaleX(1-CardConfig.SCALE_GAP*level);
view.setScaleY(1-CardConfig.SCALE_GAP*level);
}
}else {
view.setTranslationY(CardConfig.TRANS_V_GAP*(level-1));
view.setScaleX(1-CardConfig.SCALE_GAP*(level-1));
view.setScaleY(1-CardConfig.SCALE_GAP*(level-1));
}
}
}
}else {
//灵界点
double maxDistance = recyclerView.getWidth() * 1f;
double distance = Math.sqrt(dX * dX)*2;
//动画执行的百分比
double fraction = distance / maxDistance;
if (fraction > 1) {
fraction = 1;
}
int itemcount = recyclerView.getChildCount();
for (int i = 0; i < itemcount; i++) {
//执行
View view = recyclerView.getChildAt(i);
//几个view层叠的效果,错开的效果--便宜动画+缩放动画
int level = itemcount - i - 1;
if(level == 0){//最外层动画
if(Math.abs(dX) == 1080f && dY == 0f&&!isCurrentlyActive){
view.setRotation(0);
}else {
if(dX<0){
view.setRotation((float) (360f - (30 * fraction)));
}else {
view.setRotation((float) (30 * fraction));
}
}
} else if(level ==CardConfig.MAX_SHOW_COUNT-1){//最内层动画
view.setTranslationY((float) (CardConfig.TRANS_V_GAP*(level-fraction)*1.3f));
view.setScaleX((float) (1-CardConfig.SCALE_GAP*(level-fraction)));
view.setScaleY((float) (1-CardConfig.SCALE_GAP*(level-fraction)));
}else if (level < CardConfig.MAX_SHOW_COUNT - 1) {
view.setTranslationY((float) ((level - (2*fraction)) * CardConfig.TRANS_V_GAP));
view.setScaleX((float) (1 - CardConfig.SCALE_GAP * level + fraction * (CardConfig.SCALE_GAP*2)));
view.setScaleY((float) (1 - CardConfig.SCALE_GAP * level + fraction * (CardConfig.SCALE_GAP*2)));
}
}
}
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
}
public interface OnSwipeEndListener{
void onSwipeEnd();
void onSwipe();
}
public void setOnSwipeEndListener(OnSwipeEndListener listener){
this.listener = listener;
}
}
在Activity中:
private SwipeCardCallBack callback;
private ItemTouchHelper helper;
...
helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(swipeFlingAdapterView);
callback.setOnSwipeEndListener(new SwipeCardCallBack.OnSwipeEndListener() {
@Override
public void onSwipeEnd() {
swipeFlingAdapterView.suppressLayout(true);
gameGachaRefresh.setClickable(false);
ToastUtils.createToast().showCenter(TutuGameGachaActivity.this,getString(R.string.wait_moment));
presenter.getGameGacha(PRESENTER_LOAD_STATE_REFRESH);
}
@Override
public void onSwipe() {
if(arrayAdapter.getItemCount()>0) {
swipe();
}
}
});
这样实际效果就差不多可以了。
来源:https://blog.csdn.net/a__sen/article/details/114656517
标签:Android,拖动,布局
0
投稿
猜你喜欢
springboot 中整合mybatis多数据源不使用JPA
2023-03-01 08:43:02
Effective Java (异常处理)
2022-09-24 12:17:29
C#反射在实际应用中的实例代码
2022-11-25 05:06:21
springboot 整合 sa-token简介及入门教程
2023-03-24 01:10:45
使用JPA主键@Id,@IdClass,@Embeddable,@EmbeddedId问题
2022-07-07 18:35:41
Android 多国语言value文件夹命名的方法
2022-04-19 00:43:40
SpringBoot+JSON+AJAX+ECharts+Fiddler实现前后端分离开发可视化
2021-11-12 14:49:17
C#判断访问来源是否为搜索引擎链接的方法
2021-11-01 09:15:53
SWT(JFace) Menu、Bar...体验代码
2022-01-29 08:34:46
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
Android实现简单时钟View的方法
2022-10-21 18:36:00
Android使用VideoView播放本地视频和网络视频的方法
2023-06-28 11:17:32
java序列化对象根据不同配置动态改变属性名的方法
2022-10-06 11:31:09
Spring如何解决单例bean线程不安全的问题
2023-12-18 23:50:20
C#延迟执行方法函数实例讲解
2022-06-17 19:23:50
Android之日期及时间选择对话框用法实例分析
2023-06-26 23:59:34
使用c#+IMap实现收取163邮件
2023-07-21 22:44:31
Android AndFix热修复原理详情
2023-03-02 09:17:07
springboot项目以jar包运行的操作方法
2023-11-10 07:47:43
浅谈JMeter engine启动原理
2022-02-22 03:06:47