使用 Item TouchHelper 实现 RecyclerView的 item 上下移动
作者:互联网
代码如下:
package com.example.temp;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView view = findViewById(R.id.rv_demo);
view.setLayoutManager(new LinearLayoutManager(this));
List<String> data = new ArrayList<>();
for (int i = 0; i < 14; i++) {
data.add("test::" + i);
}
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.DOWN | ItemTouchHelper.UP,//item drag的条件
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT //item swipe 的条件
) {
/**drag 时触发的事件*/
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
int targetPosition = target.getAdapterPosition();
int viewPosition = viewHolder.getAdapterPosition();
Collections.swap(data, targetPosition, viewPosition);
recyclerView.getAdapter().notifyItemMoved(viewPosition, targetPosition);
return true;
}
return false;
}
/**onMove 返回true 时触发的事件*/
@Override
public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, int fromPos, @NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
/**swipe 时触发的事件*/
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
/**是否可以swipe 默认为true*/
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
/**item 被选中时触发的条件,默认是长按时触发,也可以通过 ItemTouchHelper#startDrag 或者ItemTouchHelper#startSwipe 触发*/
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setBackgroundColor(Color.RED);
}
}
/**drag完成后调用*/
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.design_default_color_primary));
}
});
DemoAdapter demoAdapter = new DemoAdapter(data, itemTouchHelper);
view.setAdapter(demoAdapter);
itemTouchHelper.attachToRecyclerView(view);
}
static class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoHolder> {
private final List<String> data;
private final ItemTouchHelper itemTouchHelper;
public DemoAdapter(List<String> data, ItemTouchHelper itemTouchHelper) {
this.data = data;
this.itemTouchHelper = itemTouchHelper;
}
@NonNull
@Override
public DemoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new DemoHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_demo_item, parent, false), itemTouchHelper);
}
@Override
public void onBindViewHolder(@NonNull DemoHolder holder, int position) {
holder.bind(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
static class DemoHolder extends RecyclerView.ViewHolder {
private TextView textView;
private Button button;
@SuppressLint("ClickableViewAccessibility")
public DemoHolder(@NonNull View itemView, ItemTouchHelper itemTouchHelper) {
super(itemView);
textView = itemView.findViewById(R.id.tv_item);
button = itemView.findViewById(R.id.btn_move);
button.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_BUTTON_PRESS)
//触发 drag操作
itemTouchHelper.startDrag(DemoHolder.this);
return true;
});
}
public void bind(String data) {
textView.setText(data);
}
}
}
}
标签:NonNull,Item,int,TouchHelper,item,import,ItemTouchHelper,RecyclerView,public 来源: https://blog.csdn.net/genmenu/article/details/113108990