android recycleview实现拖动排序
2020-05-25 本文已影响0人
hao_developer
实现以下的效果长按拖动,从而达到排序:
image.png
自定义以下接口:
interface ItemTouchHelperAdapter {
//数据交换
fun onItemMove(fromPosition: Int, toPosition: Int)
//数据删除
fun onItemDissmiss(position: Int)
}
自定义一个类实现实现ItemTouchHelper.Callback接口:
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback{
private ItemTouchHelperAdapter mAdapter;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter){
mAdapter = adapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.LEFT;
return makeMovementFlags(dragFlags,swipeFlags);
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDissmiss(viewHolder.getAdapterPosition());
}
}
adapter实现ItemTouchHelperAdapter 接口:
class StoreClassAdapter(var context: Context,var listData:ArrayList<String>)
: RecyclerView.Adapter<StoreClassAdapter.ViewHolder>(), ItemTouchHelperAdapter {
override fun onItemMove(fromPosition: Int, toPosition: Int) {
//交换位置
Collections.swap(listData,fromPosition,toPosition);
notifyItemMoved(fromPosition,toPosition)
}
override fun onItemDissmiss(position: Int) {
//移除数据
listData.removeAt(position)
notifyItemRemoved(position)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return StoreClassAdapter.ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.adapter_store_class_item_view,parent, false
)
)
}
override fun getItemCount(): Int {
return listData.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
with(holder) {
val itemBean = listData.get(position)
tvName?.text = itemBean
tvCount?.text = "商家数量:" + itemBean
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvName: TextView? = null
var tvCount: TextView? = null
init {
tvName = itemView.findViewById(R.id.tvName)
tvCount = itemView.findViewById(R.id.tvCount)
}
}
}
activity中的案例代码如下:
val linearLayoutManager = LinearLayoutManager(this)
val dividerItemDecoration =
DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
storeClassAdapter = StoreClassAdapter(this,listCard)
//先实例化Callback
val callback: ItemTouchHelper.Callback = SimpleItemTouchHelperCallback(storeClassAdapter)
//用Callback构造ItemtouchHelper
val touchHelper = ItemTouchHelper(callback)
//调用ItemTouchHelper的attachToRecyclerView方法建立联系
touchHelper.attachToRecyclerView(rvList)
with(rvList) {
layoutManager = linearLayoutManager
addItemDecoration(dividerItemDecoration)
adapter = storeClassAdapter
}
这个是整个效果的重要方法
通过Collections的swap方法来实现list集合中的item位置互换