notifyItemMoved实现recyclerView拖拽动画
Posted 汤米粥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了notifyItemMoved实现recyclerView拖拽动画相关的知识,希望对你有一定的参考价值。
RecyclerView实现拖拽切换item位置,包括linearlayoutManager,GridLayoutManager两种效果都能实现。
本文链接:https://blog.csdn.net/u011932309/article/details/104893667/
代码很简单,直接上码
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView ;
private List<String> list ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData() ;
}
private void initView() {
recyclerView = findViewById(R.id.mRecyclerView);
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3) ;
recyclerView.setLayoutManager(gridLayoutManager);
}
private void initData() {
list = new ArrayList<>() ;
list.add("空但");
list.add("卡达菲");
list.add("独守空房");
list.add("卡萨丁");
list.add("卢卡斯的");
list.add("喀什");
recyclerView.setAdapter(myAdapter = new MyAdapter(list));
mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper());
mItemTouchHelper.attachToRecyclerView(recyclerView);
}
private MyAdapter myAdapter ;
private ItemTouchHelper mItemTouchHelper;
public class MyItemTouchHelper extends ItemTouchHelper.Callback{
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
//得到当拖拽的viewHolder的Position
int fromPosition = viewHolder.getAdapterPosition();
//拿到当前拖拽到的item的viewHolder
int toPosition = target.getAdapterPosition();
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(list, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(list, i, i - 1);
}
}
myAdapter.notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
/**
* 长按选中Item的时候开始调用
*
* @param viewHolder
* @param actionState
*/
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
}
super.onSelectedChanged(viewHolder, actionState);
}
/**
* 手指松开的时候还原
* @param recyclerView
* @param viewHolder
*/
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(getColor(R.color.colorPrimary));
}
/**
* 重写拖拽不可用
* @return
*/
@Override
public boolean isLongPressDragEnabled() {
return true;
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
List<String> list ;
public MyAdapter (List<String> list){
this.list = list ;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_view_layout,parent,false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv_content.setText(list.get(position));
holder.tv_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.startActivity(new Intent(MainActivity.this,VideoNomarlActivity.class));
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView tv_content ;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_content = itemView.findViewById(R.id.tv_content);
}
}
}
}
以上是关于notifyItemMoved实现recyclerView拖拽动画的主要内容,如果未能解决你的问题,请参考以下文章
ItemTouchHelper notifyItemMoved from to position 不工作
谁能建议如何在android Recycler View中按产品价格实现排序[关闭]
onClickListener 在 Recycler Adapter 类中不起作用