赞
踩
1.先看效果图
这是拖动前的图片
这是拖动列表改变位置后的图片
这里放上视频演示
RecyclerView实现列表拖动交换数据位置
2.主活动MainActivity2类代码如下
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.annotation.SuppressLint; import android.os.Bundle; import com.example.myapplication001.R; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MainActivity2 extends AppCompatActivity { private HomeAdapter rv_1_homeAdapter; RecyclerView rv_1; LinearLayoutManager rv_1_Manager; ItemTouchHelper mItemTouchHelper; List<String> titles; @SuppressLint({ "MissingInflatedId", "WrongViewCast"}) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); rv_1=findViewById(R.id.rv_1); titles = new ArrayList<>(); for (int i = 0; i < 50; i++) { titles.add("中文"+i); } rv_1_homeAdapter = new HomeAdapter(getApplicationContext(),titles); rv_1_Manager=new LinearLayoutManager(getApplicationContext()); //这里使用垂直滑动 rv_1_Manager.setOrientation(LinearLayoutManager.VERTICAL); rv_1.setLayoutManager(rv_1_Manager); rv_1.setAdapter(rv_1_homeAdapter); //添加拖拽事件 longClickPosition:初始索引值 ActionUpPosition:结束索引值 mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelper(this,titles,rv_1_homeAdapter,(longClickPosition, ActionUpPosition)-> { if (longClickPosition==-1)retur
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。