赞
踩
记录一下,免得之后忘了
DragEvent
用于处理拖拽事件。它提供了一组常量和方法,用于跟踪和响应用户的拖拽操作。当用户拖动视图或拖动手势在屏幕上发生时,会触发相应的DragEvent
事件。
拖拽操作的动作(Action):DragEvent
定义了一组常量来表示拖拽事件的不同动作(Action),例如拖拽开始(ACTION_DRAG_STARTED
)、进入拖拽区域(ACTION_DRAG_ENTERED
)、离开拖拽区域(ACTION_DRAG_EXITED
)、放置拖拽项(ACTION_DROP
)等。
拖拽事件的来源和目标视图:DragEvent
包含关于拖拽事件的信息,例如拖拽的视图(起始视图)、拖拽的数据(ClipData
对象)、拖拽的坐标位置等。可以使用getAction()
方法获取拖拽事件的动作,使用getView()
方法获取与拖拽事件相关联的视图。
拖拽事件的处理:可以通过在目标视图上注册OnDragListener
接口来监听和处理拖拽事件。OnDragListener
接口定义了一个回调方法onDrag()
,在该方法中可以根据拖拽事件的动作(Action)执行相应的逻辑。
处理拖拽事件的逻辑:在onDrag()
方法中,可以根据拖拽事件的动作(Action)进行不同的处理逻辑,例如在拖拽开始时执行特定操作、在拖拽进入某个区域时显示提示信息、在拖拽放置时处理拖拽项的逻辑等
需要拖拽的控件有 Card_A 和 Adapter 中的CardView
Card_A :(长按才拖拽)
- Card_A.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- //可以传参数,名字随意,这里写A,表示是A传来了OK这个参数
- ClipData clipData = ClipData.newPlainText("A", "OK");
- //设置拖拽阴影
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- v.startDragAndDrop(clipData, shadowBuilder, v, 0);
- return true;
- }
- });
Adapter 中的CardView (触碰就开始拖拽)
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- holder.TV_emp_no.setText("\t"+items.get(position).getEmp_no()+"\t");
- holder.cardView.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- // 触摸时开始拖拽操作 传个位置,后面或者这个位置做删除操作
- ClipData data = ClipData.newPlainText("List_Employee", position+"");
- // 定义一个拖拽阴影效果
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- // 开始拖拽
- v.startDrag(data, shadowBuilder, v, 0);
- return true;
- }
- return false;
- }
- });
-
- }
比如要拖拽到Card_B 响应就要在Card_B这个控件进行监听
- Card_B.setOnDragListener(new View.OnDragListener() {
- @Override
- public boolean onDrag(View v, DragEvent event) {
- switch (event.getAction()) {
- case DragEvent.ACTION_DRAG_ENTERED:
- System.out.println("已经拖拽到特定区域");
- return true;
- case DragEvent.ACTION_DROP:
- ClipData clipData = event.getClipData();
- if (clipData != null) {
- // 获取 ClipData 中的标签或描述信息
- String param = clipData.getDescription().getLabel().toString();
- if (param.equals("A"))
- Toast.makeText(LayoutActivity.this, "已到达,是A拖过来过来的: " + param + "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
- else if (param.equals("List_Employee"))
- Toast.makeText(LayoutActivity.this, "已到达,是Layout拖过来过来的: " + param+ "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(LayoutActivity.this, "不知道哪里拖过来的控件 " + param, Toast.LENGTH_SHORT).show();
- }
- return true;
- default:
- break;
- }
- return true;
- }
- });
用 ClipData clipData = event.getClipData();区分是从哪个控件拖拽来的
clipData.getDescription().getLabel().toString();//获取设定的控件标签名字 clipData.getItemAt(0).getText().toString();//获取参数
还有一个 拖拽到该区域删除 的监听
- Card_remove.setOnDragListener(new View.OnDragListener() {
- @Override
- public boolean onDrag(View v, DragEvent event) {
- switch (event.getAction()) {
- case DragEvent.ACTION_DROP:
- ClipData clipData = event.getClipData();
- if (clipData != null) {
- // 获取 ClipData 中的标签或描述信息
- String param = clipData.getDescription().getLabel().toString();
- if (param.equals("List_Employee"))
- {
- //拖来的元素在List<Employee>中的索引
- int position=Integer.parseInt(clipData.getItemAt(0).getText().toString()) ;
- //执行移除操作
- employeeList.remove(position);
- layoutAdapter.notifyDataSetChanged();
- }
- }
- return true;
- default:
- break;
- }
- return true;
- }
- });
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".LayoutActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_weight="2">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:background="@color/Color_Efficiencycolor"
- android:layout_weight="1">
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:layout_marginTop="1dp"
- >
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/RV"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </FrameLayout>
- <FrameLayout
- android:layout_width="0dp"
- android:id="@+id/FL"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:layout_marginTop="1dp"
- >
- <androidx.cardview.widget.CardView
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:id="@+id/Card_remove"
- android:layout_gravity="center"
- >
- <TextView
- android:layout_width="match_parent"
- android:background="@color/Color_Ngitem"
- android:layout_height="match_parent"
- android:text="拖拽到该区域删除"
- android:textSize="40dp"
- android:gravity="center"/>
- </androidx.cardview.widget.CardView>
- </FrameLayout>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:background="@color/Color_JX"
- android:layout_weight="1">
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:layout_marginTop="1dp"
- >
- <androidx.cardview.widget.CardView
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:id="@+id/Card_A"
- android:layout_gravity="center"
- >
- <TextView
- android:layout_width="match_parent"
- android:background="@color/Color_Efficiencycolor"
- android:layout_height="match_parent"
- android:text="A"
- android:textSize="40dp"
- android:gravity="center"/>
- </androidx.cardview.widget.CardView>
- </FrameLayout>
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:layout_marginTop="1dp"
- >
- <androidx.cardview.widget.CardView
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:id="@+id/Card_B"
- android:layout_gravity="center"
- >
- <TextView
- android:layout_width="match_parent"
- android:background="@color/colorAccent_select"
- android:layout_height="match_parent"
- android:text="B"
- android:textSize="40dp"
- android:gravity="center"/>
-
- </androidx.cardview.widget.CardView>
- </FrameLayout>
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
- private RecyclerView RV;
-
- private CardView Card_remove;
- private CardView Card_A;
- private CardView Card_B;
- private LayoutAdapter layoutAdapter;
- private List<Employee> employeeList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_layout);
- EventBus.getDefault().register(this);
-
- RV = findViewById(R.id.RV);
- Card_remove = findViewById(R.id.Card_remove);
-
- Card_A = findViewById(R.id.Card_A);
- Card_B = findViewById(R.id.Card_B);
-
-
- Card_A.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- ClipData clipData = ClipData.newPlainText("A", "OK");
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- v.startDragAndDrop(clipData, shadowBuilder, v, 0);
- return true;
- }
- });
-
- Card_remove.setOnDragListener(new View.OnDragListener() {
- @Override
- public boolean onDrag(View v, DragEvent event) {
- switch (event.getAction()) {
- case DragEvent.ACTION_DROP:
- ClipData clipData = event.getClipData();
- if (clipData != null) {
- // 获取 ClipData 中的标签或描述信息
- String param = clipData.getDescription().getLabel().toString();
- if (param.equals("List_Employee"))
- {
- int position=Integer.parseInt(clipData.getItemAt(0).getText().toString()) ;
- employeeList.remove(position);
- layoutAdapter.notifyDataSetChanged();
- }
- }
- return true;
- default:
- break;
- }
- return true;
- }
- });
-
- Card_B.setOnDragListener(new View.OnDragListener() {
- @Override
- public boolean onDrag(View v, DragEvent event) {
- switch (event.getAction()) {
- case DragEvent.ACTION_DRAG_ENTERED:
- System.out.println("已经拖拽到特定区域");
- return true;
- case DragEvent.ACTION_DROP:
- ClipData clipData = event.getClipData();
- if (clipData != null) {
- // 获取 ClipData 中的标签或描述信息
- String param = clipData.getDescription().getLabel().toString();
- if (param.equals("A"))
- Toast.makeText(LayoutActivity.this, "已到达,是A拖过来过来的: " + param + "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
- else if (param.equals("List_Employee"))
- Toast.makeText(LayoutActivity.this, "已到达,是Layout拖过来过来的: " + param+ "参数为" + clipData.getItemAt(0).getText().toString(), Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(LayoutActivity.this, "不知道哪里拖过来的控件 " + param, Toast.LENGTH_SHORT).show();
- }
- return true;
- default:
- break;
- }
- return true;
- }
- });
-
- //适配器布局
- GridLayoutManager m=new GridLayoutManager(this,3); //每行显示3个
- m.setOrientation(LinearLayoutManager.VERTICAL);
- RV.setLayoutManager(m);
- layoutAdapter = new LayoutAdapter(employeeList);
- RV.setAdapter(layoutAdapter);
-
- employeeList = new ArrayList<>();
- for (int i = 0; i < 20; i++) {
- Employee employee = new Employee();
- employee.setEmp_no("工号" + i);
- employee.setName("工名" + i);
- employeeList.add(employee);
- }
-
- }
- public class LayoutAdapter extends RecyclerView.Adapter<LayoutAdapter.ViewHolder>{
- private Context mContext;
- private RecyclerView recyclerView;
- private List<Employee> items;
-
- public LayoutAdapter(List<Employee> items) {
- this.items = items;
- this.mContext = mContext;
- }
-
- @Override
- public int getItemCount() {
- return items.size();
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_list,parent,false);
- ViewHolder vh=new ViewHolder(view);
- return vh;
- }
-
- private ItemClickListener mItemClickListener ;
- public interface ItemClickListener{
- public void onItemClick(int position) ;
- }
- public void setOnItemClickListener(ItemClickListener itemClickListener){
- this.mItemClickListener = itemClickListener ;
-
- }
- public static class ViewHolder extends RecyclerView.ViewHolder {
-
- public CardView cardView;
- public TextView TV_emp_no;
- public ViewHolder(View view){
- super(view);
- cardView=(CardView) view.findViewById(R.id.card_view);
- TV_emp_no=(TextView)view.findViewById(R.id.TV_emp_no);
-
- }
- }
- @SuppressLint("ClickableViewAccessibility")
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- holder.TV_emp_no.setText("\t"+items.get(position).getEmp_no()+"\t");
- holder.cardView.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- // 触摸时开始拖拽操作
- ClipData data = ClipData.newPlainText("List_Employee", position+"");
- // 定义一个拖拽阴影效果
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- // 开始拖拽
- v.startDrag(data, shadowBuilder, v, 0);
- return true;
- }
- return false;
- }
- });
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。