好像都是这里来的吧
https://blog.csdn.net/zxt0601/article/details/52956504
简单的卡片布局
效果图
![](https://img.haomeiwen.com/i4625080/fb76511fcc8aa62e.png)
![](https://img.haomeiwen.com/i4625080/170fa8e48f5527c1.png)
具体代码如下:
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
/**
* Created by charlie.song on 2018/5/10.
*/
class SwipCardLayoutManger:RecyclerView.LayoutManager(){
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
}
var MaxShow=5;//做多展示几个item
override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State?) {
detachAndScrapAttachedViews(recycler)
if(itemCount==0){
return
}
var end=MaxShow;
if(itemCount<MaxShow){
end=itemCount
}
for (i in 0 until end){
val position=end-1-i;//我们的数据0,1,2,3,4 ,我们需要的是0在最上方,那么肯定先add那个index是4的child拉。
var child=recycler.getViewForPosition(position)
addView(child)
measureChildWithMargins(child,0,0)
val childWidth=getDecoratedMeasuredWidth(child)
val childHeight=getDecoratedMeasuredHeight(child)
layoutDecoratedWithMargins(child,(width-childWidth)/2,(height-childHeight)/2,(width+childWidth)/2,(height+childHeight)/2)
child.translationY=17f*position
child.scaleY=1f-position*0.05f
child.scaleX=1f-position*0.05f
}
}
}
ItemTouchHelper需要的callback的代码
import android.graphics.Canvas
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
/**
* Created by charlie.song on 2018/5/10.
*/
class SwipCardCallBack:ItemTouchHelper.SimpleCallback{
var datas= arrayListOf<String>()
constructor(dragDirs: Int, swipeDirs: Int,data:ArrayList<String>) : super(dragDirs, swipeDirs){
datas=data
}
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
return false
}
var rv:RecyclerView?=null;
//拖动结束以后会走这里
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
datas.add(datas.removeAt(0))//这里数据不删除,就是把滑动的数据再放到最后。有具体需求再改
rv?.adapter?.notifyDataSetChanged()
}
override fun onChildDraw(c: Canvas?, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
rv=recyclerView;
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
val z=Math.sqrt((dX*dX+dY*dY).toDouble()).toFloat();
var factor=z/(recyclerView.width/2f);//移动的距离和宽度的一半 做比较
if(factor>1){
factor= 1f
}
for(i in 0 until recyclerView.childCount-1){
val child=recyclerView.getChildAt(i)
val realPosition=recyclerView.getChildAdapterPosition(child)
child.translationY=17f*(realPosition-factor)
child.scaleY=1f-(realPosition-factor)*0.05f
child.scaleX=1f-(realPosition-factor)*0.05f
}
}
}
自己正常设置一个recyclerView的adapter,完事如下代码,处理滑动事件
ItemTouchHelper(SwipCardCallBack(0,ItemTouchHelper.UP or ItemTouchHelper.DOWN
or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,datas)).attachToRecyclerView(rv_pic)
网友评论