当前位置: 首页 > web >正文

安卓基础(拖拽)

当用户长按或拖拽某个视图(如按钮、图片)时,需要提供视觉反馈(即阴影)。这行代码通常在拖拽事件的处理逻辑中,例如:

view.setOnLongClickListener(v -> {// 创建拖拽阴影DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);// 启动拖拽操作v.startDragAndDrop(null, shadowBuilder, null, 0);return true;
});

设置长按监听器(触发拖拽)​

View.OnLongClickListener longClickListener = new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {// 创建拖拽阴影(使用被长按的视图自身作为阴影)DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);// 启动拖拽操作,将视图自身作为本地数据传递(第三个参数)v.startDragAndDrop(null, shadowBuilder, v, 0);return true; // 表示已处理长按事件}
};
// 为三个可拖拽的TextView设置相同的长按监听器
draggable1.setOnLongClickListener(longClickListener);
draggable2.setOnLongClickListener(longClickListener);
draggable3.setOnLongClickListener(longClickListener);

完整代码

主活动代码 MainActivity.java

package com.example.draganddropdemo;import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView draggable1 = findViewById(R.id.draggable1);TextView draggable2 = findViewById(R.id.draggable2);TextView draggable3 = findViewById(R.id.draggable3);FrameLayout targetContainer = findViewById(R.id.target_container);// 为可拖拽组件设置长按监听View.OnLongClickListener longClickListener = new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);v.startDragAndDrop(null, shadowBuilder, v, 0);return true;}};draggable1.setOnLongClickListener(longClickListener);draggable2.setOnLongClickListener(longClickListener);draggable3.setOnLongClickListener(longClickListener);// 为目标容器设置拖拽监听targetContainer.setOnDragListener(new View.OnDragListener() {@Overridepublic boolean onDrag(View v, DragEvent event) {switch (event.getAction()) {case DragEvent.ACTION_DRAG_STARTED:return true;case DragEvent.ACTION_DRAG_ENTERED:v.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));return true;case DragEvent.ACTION_DRAG_LOCATION:return true;case DragEvent.ACTION_DRAG_EXITED:v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));return true;case DragEvent.ACTION_DROP:View draggedView = (View) event.getLocalState();// 从原父容器移除if (draggedView.getParent() instanceof FrameLayout) {((FrameLayout) draggedView.getParent()).removeView(draggedView);}// 添加到目标容器targetContainer.addView(draggedView);draggedView.setVisibility(View.VISIBLE);v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));// 为容器内组件设置可拖拽draggedView.setOnLongClickListener(longClickListener);return true;case DragEvent.ACTION_DRAG_ENDED:v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));return true;default:return false;}}});}
}

布局文件 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><!-- 可拖拽组件 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:id="@+id/draggable1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Component 1"android:padding="16dp"android:background="@android:color/holo_blue_light"android:layout_margin="8dp" /><TextViewandroid:id="@+id/draggable2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Component 2"android:padding="16dp"android:background="@android:color/holo_blue_light"android:layout_margin="8dp" /><TextViewandroid:id="@+id/draggable3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Component 3"android:padding="16dp"android:background="@android:color/holo_blue_light"android:layout_margin="8dp" /></LinearLayout><!-- 目标容器 --><FrameLayoutandroid:id="@+id/target_container"android:layout_width="match_parent"android:layout_height="300dp"android:background="@android:color/darker_gray"android:layout_marginTop="16dp" />
</LinearLayout>


把自定义的xml组件放入父布局的容器里面

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取 LayoutInflater 实例LayoutInflater inflater = LayoutInflater.from(this);// 用 inflater 把 XML 布局文件转换成 View 对象View view = inflater.inflate(R.layout.some_layout, null);// 找到一个父布局LinearLayout parentLayout = findViewById(R.id.parent_layout);// 把转换后的 View 对象添加到父布局中parentLayout.addView(view);}
}

http://www.xdnf.cn/news/4327.html

相关文章:

  • 前端知识-useState
  • 开启健康模式:养身新主张
  • Nginx 安全防护与Https 部署实战
  • 自定义SpringBoot Starter-笔记
  • Element-Plus-X开源程序是Vue3 + Element-Plus 开箱即用的企业级AI组件库前端的解决方案
  • 【言语理解】片段阅读之语句填入(7)
  • LeetCode 1781. 所有子字符串美丽值之和 题解
  • C++编程语言:从高效系统开发到现代编程范式的演进之路
  • python仓库库存管理系统-药房药品库存管理系统
  • 极简RT-Thread入门教程
  • 高等数学第六章---定积分(§6.1元素法6.2定积分在几何上的应用1)
  • XILINX原语之——xpm_fifo_async(异步FIFO灵活设置位宽、深度)
  • vscode远程服务器连接----过程尝试写入的管道不存在
  • javascript Map 和对象使用
  • echarts报错问题initialize failed:invalid dom
  • AI技术下研发体系重构
  • Vue项目Git提交流程集成
  • Leetcode 刷题记录 07 —— 链表
  • excel表数据导入数据库
  • Selenium模拟人类,操作网页的行为(全)
  • Pointpillars(三)工程实践
  • 新手SEO基础操作入门精要
  • Java学习手册:Base64 编码概念和应用场景
  • 解锁创意显示,强力巨彩软模组引领柔性显示技术创新
  • 随机快速排序算法
  • GAN模型
  • 总结七种提示优化方案的核心实现流程
  • 第15章 Python数据类型详解之分解理解:基础数据类型常见易错点和性能优化篇
  • Visual Studio 快捷键更改和设置
  • 【C++游戏引擎开发】第30篇:物理引擎(Bullet)—软体动力学系统