赞
踩
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/rv_picture"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:transcriptMode="alwaysScroll"/>
- </RelativeLayout>
item_pic.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="124dp"
- android:layout_height="124dp"
- android:background="@color/white">
-
- <ImageView
- android:id="@+id/iv_picture"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:scaleType="fitXY"
- android:padding="2dp"/>
- </RelativeLayout>
适配器PicAdapter.class
- public class PicAdapter extends RecyclerView.Adapter<PicAdapter.ViewHolder> {
-
- private Context context;
- private List<PicBean> picBeans;
-
-
- public PicAdapter(Context context, List<PicBean> picBeans) {
- this.context = context;
- this.picBeans = picBeans;
- }
-
- @Override
- public PicAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false);
- return new ViewHolder(view);
- }
-
- @Override
- public void onBindViewHolder(PicAdapter.ViewHolder holder, int position) {
- Log.d("dddd","onBindViewHolder");
- holder.iv_picture.setImageResource(picBeans.get(position).getImage());
- }
-
- @Override
- public int getItemCount() {
- return picBeans.size();
- }
-
- public class ViewHolder extends RecyclerView.ViewHolder {
- public ImageView iv_picture;
- public ViewHolder(View itemView) {
- super(itemView);
- iv_picture = (ImageView) itemView.findViewById(R.id.iv_picture);
- }
- }
- }
编写实现拖拽的帮助类MyItemTouchHelper.class,继承父类ItemTouchHelper.Callback,重写onMove{}方法,处理长按拖拽操作,对位置进行更改
- public class MyItemTouchHelper extends ItemTouchHelper.Callback{
- private List<PicBean> pictureBeans;
- private PicAdapter recycleViewAdapter;
- MainActivity activity;
- public MyItemTouchHelper(MainActivity activity, List<PicBean> pictureBeans, PicAdapter recycleViewAdapter) {
- Log.d("dddd","into MyItemTouchHelper");
- this.pictureBeans = pictureBeans;
- this.recycleViewAdapter = recycleViewAdapter;
- this.activity = activity;
- }
-
- @Override
- public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
- final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- final int swipeFlags = 0;
- return makeMovementFlags(dragFlags, swipeFlags);
- }
-
- @Override
- public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
- recyclerView.getParent().requestDisallowInterceptTouchEvent(true);
- //得到当拖拽的viewHolder的Position
- int fromPosition = viewHolder.getAdapterPosition();
- //拿到当前拖拽到的item的viewHolder
- int toPosition = target.getAdapterPosition();
- if (fromPosition < toPosition) {
- for (int i = fromPosition; i < toPosition; i++) {
- Collections.swap(pictureBeans, i, i + 1);
- }
- } else {
- for (int i = fromPosition; i > toPosition; i--) {
- Collections.swap(pictureBeans, i, i - 1);
- }
- }
- recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
- Log.d("dddd"," fromPosition = "+fromPosition+" toPosition"+toPosition);
- return true;
- }
-
- @Override
- public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
-
- }
-
- /**
- * 长按选中Item时修改颜色
- *
- * @param viewHolder
- * @param actionState
- */
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- @Override
- public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
- //if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
- //viewHolder.itemView.setBackground(getDrawable(R.drawable.card_drag_selected));
- //}
- super.onSelectedChanged(viewHolder, actionState);
- }
-
- /**
- * 手指松开的时候还原颜色
- * @param recyclerView
- * @param viewHolder
- */
- @RequiresApi(api = Build.VERSION_CODES.M)
- @Override
- public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
- super.clearView(recyclerView, viewHolder);
- //viewHolder.itemView.setBackground(getDrawable(R.drawable.card));
- }
-
- /**
- * 重写拖拽不可用
- * @return
- */
- @Override
- public boolean isLongPressDragEnabled() {
- return true;
- }
- }
主界面MainActivity.class
- public class MainActivity extends AppCompatActivity {
- private RecyclerView recyclerView;
- private com.example.dragdemo.PicAdapter picAdapter;
- private List<com.example.dragdemo.PicBean> picBeans;
- private ItemTouchHelper mItemTouchHelper;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- //模拟数据
- picBeans = new ArrayList<>();
- int image = 0;
- for (int i = 0;i<12;i++){
- com.example.dragdemo.PicBean picBean = new com.example.dragdemo.PicBean();
- if (i % 3 == 0){
- image = R.color.black;
- }else if (i % 3 == 1){
- image = R.color.purple_200;
- }else if (i % 3 == 2){
- image = R.color.teal_700;
- }
- Log.d("dddd","image = "+image);
- picBean.setImage(image);
- picBeans.add(picBean);
- }
- Log.d("dddd","picBeans.size() = "+picBeans.size());
-
- recyclerView = (RecyclerView)findViewById(R.id.rv_picture);
-
- GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
- recyclerView.setLayoutManager(gridLayoutManager);
- recyclerView.setNestedScrollingEnabled(true);
-
- //创建并设置适配器
- picAdapter = new PicAdapter(this,picBeans);
- recyclerView.setAdapter(picAdapter);
-
- //添加拖拽事件
- mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper(this,picBeans,picAdapter));
- mItemTouchHelper.attachToRecyclerView(recyclerView);
- }
- }
到这里,我们的RecyclerView拖拽排序功能就完成啦
接下来可以看一下效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。