当前位置:   article > 正文

Android笔记 拖拽控件到指定区域_android 拖动控件

android 拖动控件

 记录一下,免得之后忘了

主要部分

DragEvent

DragEvent用于处理拖拽事件。它提供了一组常量和方法,用于跟踪和响应用户的拖拽操作。当用户拖动视图或拖动手势在屏幕上发生时,会触发相应的DragEvent事件。

  1. 拖拽操作的动作(Action)DragEvent定义了一组常量来表示拖拽事件的不同动作(Action),例如拖拽开始(ACTION_DRAG_STARTED)、进入拖拽区域(ACTION_DRAG_ENTERED)、离开拖拽区域(ACTION_DRAG_EXITED)、放置拖拽项(ACTION_DROP)等。

  2. 拖拽事件的来源和目标视图DragEvent包含关于拖拽事件的信息,例如拖拽的视图(起始视图)、拖拽的数据(ClipData对象)、拖拽的坐标位置等。可以使用getAction()方法获取拖拽事件的动作,使用getView()方法获取与拖拽事件相关联的视图。

  3. 拖拽事件的处理:可以通过在目标视图上注册OnDragListener接口来监听和处理拖拽事件。OnDragListener接口定义了一个回调方法onDrag(),在该方法中可以根据拖拽事件的动作(Action)执行相应的逻辑。

  4. 处理拖拽事件的逻辑:在onDrag()方法中,可以根据拖拽事件的动作(Action)进行不同的处理逻辑,例如在拖拽开始时执行特定操作、在拖拽进入某个区域时显示提示信息、在拖拽放置时处理拖拽项的逻辑等

编写

需要拖拽的控件有 Card_A 和 Adapter 中的CardView 

Card_A :(长按才拖拽)

  1. Card_A.setOnLongClickListener(new View.OnLongClickListener() {
  2. @Override
  3. public boolean onLongClick(View v) {
  4. //可以传参数,名字随意,这里写A,表示是A传来了OK这个参数
  5. ClipData clipData = ClipData.newPlainText("A", "OK");
  6. //设置拖拽阴影
  7. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  8. v.startDragAndDrop(clipData, shadowBuilder, v, 0);
  9. return true;
  10. }
  11. });

Adapter 中的CardView (触碰就开始拖拽)
 

  1. @Override
  2. public void onBindViewHolder(ViewHolder holder, int position) {
  3. holder.TV_emp_no.setText("\t"+items.get(position).getEmp_no()+"\t");
  4. holder.cardView.setOnTouchListener(new View.OnTouchListener() {
  5. @Override
  6. public boolean onTouch(View v, MotionEvent event) {
  7. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  8. // 触摸时开始拖拽操作 传个位置,后面或者这个位置做删除操作
  9. ClipData data = ClipData.newPlainText("List_Employee", position+"");
  10. // 定义一个拖拽阴影效果
  11. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  12. // 开始拖拽
  13. v.startDrag(data, shadowBuilder, v, 0);
  14. return true;
  15. }
  16. return false;
  17. }
  18. });
  19. }

监听拖拽

比如要拖拽到Card_B 响应就要在Card_B这个控件进行监听

  1. Card_B.setOnDragListener(new View.OnDragListener() {
  2. @Override
  3. public boolean onDrag(View v, DragEvent event) {
  4. switch (event.getAction()) {
  5. case DragEvent.ACTION_DRAG_ENTERED:
  6. System.out.println("已经拖拽到特定区域");
  7. return true;
  8. case DragEvent.ACTION_DROP:
  9. ClipData clipData = event.getClipData();
  10. if (clipData != null) {
  11. // 获取 ClipData 中的标签或描述信息
  12. String param = clipData.getDescription().getLabel().toString();
  13. if (param.equals("A"))
  14. Toast.makeText(LayoutActivity.this, "已到达,是A拖过来过来的: " + param + "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
  15. else if (param.equals("List_Employee"))
  16. Toast.makeText(LayoutActivity.this, "已到达,是Layout拖过来过来的: " + param+ "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
  17. else
  18. Toast.makeText(LayoutActivity.this, "不知道哪里拖过来的控件 " + param, Toast.LENGTH_SHORT).show();
  19. }
  20. return true;
  21. default:
  22. break;
  23. }
  24. return true;
  25. }
  26. });

用  ClipData clipData = event.getClipData();区分是从哪个控件拖拽来的

clipData.getDescription().getLabel().toString();//获取设定的控件标签名字  clipData.getItemAt(0).getText().toString();//获取参数

还有一个 拖拽到该区域删除 的监听

  1. Card_remove.setOnDragListener(new View.OnDragListener() {
  2. @Override
  3. public boolean onDrag(View v, DragEvent event) {
  4. switch (event.getAction()) {
  5. case DragEvent.ACTION_DROP:
  6. ClipData clipData = event.getClipData();
  7. if (clipData != null) {
  8. // 获取 ClipData 中的标签或描述信息
  9. String param = clipData.getDescription().getLabel().toString();
  10. if (param.equals("List_Employee"))
  11. {
  12. //拖来的元素在List<Employee>中的索引
  13. int position=Integer.parseInt(clipData.getItemAt(0).getText().toString()) ;
  14. //执行移除操作
  15. employeeList.remove(position);
  16. layoutAdapter.notifyDataSetChanged();
  17. }
  18. }
  19. return true;
  20. default:
  21. break;
  22. }
  23. return true;
  24. }
  25. });

整体代码

Activity的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".LayoutActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical"
  12. android:layout_weight="2">
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="0dp"
  16. android:background="@color/Color_Efficiencycolor"
  17. android:layout_weight="1">
  18. <FrameLayout
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"
  22. android:layout_marginTop="1dp"
  23. >
  24. <androidx.recyclerview.widget.RecyclerView
  25. android:id="@+id/RV"
  26. android:layout_width="match_parent"
  27. android:layout_height="match_parent"/>
  28. </FrameLayout>
  29. <FrameLayout
  30. android:layout_width="0dp"
  31. android:id="@+id/FL"
  32. android:layout_height="match_parent"
  33. android:layout_weight="1"
  34. android:layout_marginTop="1dp"
  35. >
  36. <androidx.cardview.widget.CardView
  37. android:layout_width="200dp"
  38. android:layout_height="200dp"
  39. android:id="@+id/Card_remove"
  40. android:layout_gravity="center"
  41. >
  42. <TextView
  43. android:layout_width="match_parent"
  44. android:background="@color/Color_Ngitem"
  45. android:layout_height="match_parent"
  46. android:text="拖拽到该区域删除"
  47. android:textSize="40dp"
  48. android:gravity="center"/>
  49. </androidx.cardview.widget.CardView>
  50. </FrameLayout>
  51. </LinearLayout>
  52. <LinearLayout
  53. android:layout_width="match_parent"
  54. android:layout_height="0dp"
  55. android:background="@color/Color_JX"
  56. android:layout_weight="1">
  57. <FrameLayout
  58. android:layout_width="0dp"
  59. android:layout_height="match_parent"
  60. android:layout_weight="1"
  61. android:layout_marginTop="1dp"
  62. >
  63. <androidx.cardview.widget.CardView
  64. android:layout_width="200dp"
  65. android:layout_height="200dp"
  66. android:id="@+id/Card_A"
  67. android:layout_gravity="center"
  68. >
  69. <TextView
  70. android:layout_width="match_parent"
  71. android:background="@color/Color_Efficiencycolor"
  72. android:layout_height="match_parent"
  73. android:text="A"
  74. android:textSize="40dp"
  75. android:gravity="center"/>
  76. </androidx.cardview.widget.CardView>
  77. </FrameLayout>
  78. <FrameLayout
  79. android:layout_width="0dp"
  80. android:layout_height="match_parent"
  81. android:layout_weight="1"
  82. android:layout_marginTop="1dp"
  83. >
  84. <androidx.cardview.widget.CardView
  85. android:layout_width="200dp"
  86. android:layout_height="200dp"
  87. android:id="@+id/Card_B"
  88. android:layout_gravity="center"
  89. >
  90. <TextView
  91. android:layout_width="match_parent"
  92. android:background="@color/colorAccent_select"
  93. android:layout_height="match_parent"
  94. android:text="B"
  95. android:textSize="40dp"
  96. android:gravity="center"/>
  97. </androidx.cardview.widget.CardView>
  98. </FrameLayout>
  99. </LinearLayout>
  100. </LinearLayout>
  101. </RelativeLayout>

Activity主要代码

  1. private RecyclerView RV;
  2. private CardView Card_remove;
  3. private CardView Card_A;
  4. private CardView Card_B;
  5. private LayoutAdapter layoutAdapter;
  6. private List<Employee> employeeList;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_layout);
  11. EventBus.getDefault().register(this);
  12. RV = findViewById(R.id.RV);
  13. Card_remove = findViewById(R.id.Card_remove);
  14. Card_A = findViewById(R.id.Card_A);
  15. Card_B = findViewById(R.id.Card_B);
  16. Card_A.setOnLongClickListener(new View.OnLongClickListener() {
  17. @Override
  18. public boolean onLongClick(View v) {
  19. ClipData clipData = ClipData.newPlainText("A", "OK");
  20. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  21. v.startDragAndDrop(clipData, shadowBuilder, v, 0);
  22. return true;
  23. }
  24. });
  25. Card_remove.setOnDragListener(new View.OnDragListener() {
  26. @Override
  27. public boolean onDrag(View v, DragEvent event) {
  28. switch (event.getAction()) {
  29. case DragEvent.ACTION_DROP:
  30. ClipData clipData = event.getClipData();
  31. if (clipData != null) {
  32. // 获取 ClipData 中的标签或描述信息
  33. String param = clipData.getDescription().getLabel().toString();
  34. if (param.equals("List_Employee"))
  35. {
  36. int position=Integer.parseInt(clipData.getItemAt(0).getText().toString()) ;
  37. employeeList.remove(position);
  38. layoutAdapter.notifyDataSetChanged();
  39. }
  40. }
  41. return true;
  42. default:
  43. break;
  44. }
  45. return true;
  46. }
  47. });
  48. Card_B.setOnDragListener(new View.OnDragListener() {
  49. @Override
  50. public boolean onDrag(View v, DragEvent event) {
  51. switch (event.getAction()) {
  52. case DragEvent.ACTION_DRAG_ENTERED:
  53. System.out.println("已经拖拽到特定区域");
  54. return true;
  55. case DragEvent.ACTION_DROP:
  56. ClipData clipData = event.getClipData();
  57. if (clipData != null) {
  58. // 获取 ClipData 中的标签或描述信息
  59. String param = clipData.getDescription().getLabel().toString();
  60. if (param.equals("A"))
  61. Toast.makeText(LayoutActivity.this, "已到达,是A拖过来过来的: " + param + "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
  62. else if (param.equals("List_Employee"))
  63. Toast.makeText(LayoutActivity.this, "已到达,是Layout拖过来过来的: " + param+ "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
  64. else
  65. Toast.makeText(LayoutActivity.this, "不知道哪里拖过来的控件 " + param, Toast.LENGTH_SHORT).show();
  66. }
  67. return true;
  68. default:
  69. break;
  70. }
  71. return true;
  72. }
  73. });
  74. //适配器布局
  75. GridLayoutManager m=new GridLayoutManager(this,3); //每行显示3
  76. m.setOrientation(LinearLayoutManager.VERTICAL);
  77. RV.setLayoutManager(m);
  78. layoutAdapter = new LayoutAdapter(employeeList);
  79. RV.setAdapter(layoutAdapter);
  80. employeeList = new ArrayList<>();
  81. for (int i = 0; i < 20; i++) {
  82. Employee employee = new Employee();
  83. employee.setEmp_no("工号" + i);
  84. employee.setName("工名" + i);
  85. employeeList.add(employee);
  86. }
  87. }

Adapter的代码

  1. public class LayoutAdapter extends RecyclerView.Adapter<LayoutAdapter.ViewHolder>{
  2. private Context mContext;
  3. private RecyclerView recyclerView;
  4. private List<Employee> items;
  5. public LayoutAdapter(List<Employee> items) {
  6. this.items = items;
  7. this.mContext = mContext;
  8. }
  9. @Override
  10. public int getItemCount() {
  11. return items.size();
  12. }
  13. @Override
  14. public long getItemId(int position) {
  15. return position;
  16. }
  17. @Override
  18. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  19. View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_list,parent,false);
  20. ViewHolder vh=new ViewHolder(view);
  21. return vh;
  22. }
  23. private ItemClickListener mItemClickListener ;
  24. public interface ItemClickListener{
  25. public void onItemClick(int position) ;
  26. }
  27. public void setOnItemClickListener(ItemClickListener itemClickListener){
  28. this.mItemClickListener = itemClickListener ;
  29. }
  30. public static class ViewHolder extends RecyclerView.ViewHolder {
  31. public CardView cardView;
  32. public TextView TV_emp_no;
  33. public ViewHolder(View view){
  34. super(view);
  35. cardView=(CardView) view.findViewById(R.id.card_view);
  36. TV_emp_no=(TextView)view.findViewById(R.id.TV_emp_no);
  37. }
  38. }
  39. @SuppressLint("ClickableViewAccessibility")
  40. @Override
  41. public void onBindViewHolder(ViewHolder holder, int position) {
  42. holder.TV_emp_no.setText("\t"+items.get(position).getEmp_no()+"\t");
  43. holder.cardView.setOnTouchListener(new View.OnTouchListener() {
  44. @Override
  45. public boolean onTouch(View v, MotionEvent event) {
  46. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  47. // 触摸时开始拖拽操作
  48. ClipData data = ClipData.newPlainText("List_Employee", position+"");
  49. // 定义一个拖拽阴影效果
  50. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  51. // 开始拖拽
  52. v.startDrag(data, shadowBuilder, v, 0);
  53. return true;
  54. }
  55. return false;
  56. }
  57. });
  58. }
  59. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/111343
推荐阅读
相关标签
  

闽ICP备14008679号