在android中的gridview中拖放图像

2023-12-11

我正在开发一个示例 Android 应用程序来了解 Android 中的拖放操作。在应用程序启动时,我在网格视图上显示一些图像。现在我需要一次将一张图像拖到另一张图像的位置。将一个图像放到另一个图像上后,图像应该交换位置。我怎样才能实现它?请指导/帮助我。


您可以使用 thquinn 轻松实现这一点可拖动的网格视图

您可以添加自定义布局

public class DraggableGridViewSampleActivity extends Activity {
    static Random random = new Random();
    static String[] words = "the of and a to in is be that was he for it with as his I on have at by not they this had are but from or she an which you one we all were her would there their will when who him been has more if no out do so can what up said about other into than its time only could new them man some these then two first may any like now my such make over our even most me state after also made many did must before back see through way where get much go well your know should down work year because come people just say each those take day good how long Mr own too little use US very great still men here life both between old under last never place same another think house while high right might came off find states since used give against three himself look few general hand school part small American home during number again Mrs around thought went without however govern don't does got public United point end become head once course fact upon need system set every war put form water took".split(" ");
    DraggableGridView dgv;
    Button button1, button2;
    ArrayList<String> poem = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dgv = ((DraggableGridView)findViewById(R.id.vgv));
        button1 = ((Button)findViewById(R.id.button1));
        button2 = ((Button)findViewById(R.id.button2));

        setListeners();
    }
    private void setListeners()
    {
        dgv.setOnRearrangeListener(new OnRearrangeListener() {
            public void onRearrange(int oldIndex, int newIndex) {
                String word = poem.remove(oldIndex);
                if (oldIndex < newIndex)
                    poem.add(newIndex, word);
                else
                    poem.add(newIndex, word);
            }
        });
        dgv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
                Toast.makeText(getApplicationContext(), "On clicked" +position, Toast.LENGTH_SHORT).show();
                dgv.removeViewAt(position);
                poem.remove(position);
            }
        });
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                String word = words[random.nextInt(words.length)];
                addView();
                poem.add(word);
            }
        });
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                String finishedPoem = "";
                for (String s : poem)
                    finishedPoem += s + " ";
                new AlertDialog.Builder(DraggableGridViewSampleActivity.this)
                .setTitle("Here's your poem!")
                .setMessage(finishedPoem).show();
            }
        });
    }

    public void addView()
    {

        LayoutParams mLayoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        LinearLayout mLinearLayout= new LinearLayout(DraggableGridViewSampleActivity.this);
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

        ImageView mImageView = new ImageView(DraggableGridViewSampleActivity.this);

        if(dgv.getChildCount()%2==0)
            mImageView.setImageResource(R.drawable.child1);
        else
            mImageView.setImageResource(R.drawable.child2);

        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        mImageView.setLayoutParams(mLayoutParams);

        mImageView.setId(dgv.getChildCount());

        mImageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), v.getId()+"clicked ", Toast.LENGTH_SHORT).show();
                return dgv.onTouch(v, event);
            }
        });


            TextView mTextView = new TextView(DraggableGridViewSampleActivity.this);

            mTextView.setLayoutParams(mLayoutParams);

            mTextView.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
                    return dgv.onTouch(v, event);
                }
            });

            TextView mTextViewLabel = new TextView(DraggableGridViewSampleActivity.this);

            mTextViewLabel.setText(((dgv.getChildCount()+1)+""));
            mTextViewLabel.setLayoutParams(mLayoutParams);

            mTextViewLabel.setId(dgv.getChildCount());

            mTextViewLabel.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
                    return dgv.onTouch(v, event);
                }
            });

            mLinearLayout.setTag(mTextViewLabel);
            mLinearLayout.addView(mTextViewLabel);
            mLinearLayout.addView(mImageView);
            mLinearLayout.addView(mTextView);
        dgv.addView(mLinearLayout);

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在android中的gridview中拖放图像 的相关文章

  • 如何获取之前的碎片?

    为了在我的应用程序中重用某些片段 我需要知道哪个片段是返回堆栈上的第二个片段 为了做到这一点 我正在使用getFragmentManager getFragments 显示以下错误 但有效 FragmentManager getFragme
  • Twitter 登录说明

    我想在 Android 中创建一个 Twitter 应用程序 为此 我想创建一个登录页面并登录到 Twitter 为此 我们需要消费者密钥和消费者密钥 这是什么意思 要创建此登录页面 除了 Twitter 帐户之外 我们还需要其他任何东西吗
  • Service 和 IntentService,运行从服务器轮询数据库值的服务哪个更好?

    我读过很多关于Service and IntentService 然而 当做出决定时 我没有足够的信心选择使用哪种类型来创建一个后台服务 该服务将在一定时间间隔内从数据库轮询数据 并在获得所需数据时停止它 因为数据代表请求的状态 例如 订购
  • 安卓定位不准确

    我正在尝试获取当前用户的位置 我试图重构我的代码以获得更好的结果 但我只是不断得到关于准确度的荒谬位置 它在 900 600 米之间 如何才能得到更好的结果 使其精度达到50m以内 这是我的代码 package com agam mapsl
  • 调试:在 Android 1.0 中找不到文件

    今天我更新到 Android Studio v 1 0 在尝试编译任何项目时出现以下错误 app build intermediates classes debug 找不到文件 问题是在更新之前我没有任何问题 这是我实际尝试编译的代码 构建
  • 如何为发布而不是调试创建密钥库?扑

    我按照使用此网站部署 flutter 的步骤进行操作https flutter io android release https flutter io android release 当我运行 flutter build apk 时出现此错
  • 调整浮动操作按钮的图标大小(fab)

    The new floating action button should be 56dp x 56dp and the icon inside it should be 24dp x 24dp So the space between i
  • 使用 Retrofit2 和 Mockito 或 Robolectric 进行 Android 单元测试

    我可以测试 Retrofit2beta4 的真实响应吗 我需要 Mockito 或 Robolectic 吗 我的项目中没有活动 它将是一个库 我需要测试服务器是否正确响应 现在我有这样的代码并卡住了 Mock ApiManager api
  • 如何检查 Android 中连接的 wifi 网络是否处于活动状态

    如何自动检查android中连接的WiFi网络上的互联网是否处于活动状态 我可以检查 wifi 是否已启用或 wifi 网络是否已连接 但我不确定如何检查互联网是否已连接 这可能吗 private boolean connectionAva
  • 画透明圆,外面填充

    我有一个地图视图 我想在其上画一个圆圈以聚焦于给定区域 但我希望圆圈倒转 也就是说 圆的内部不是被填充 而是透明的 其他所有部分都被填充 请参阅这张图片了解我的意思 http i imgur com zxIMZ png 上半部分显示了我可以
  • 使用 PhoneGap 使 Android 应用程序易于访问(对于残障人士)

    有人有过使用 PhoneGap 使 Android 应用程序可访问的经验吗 至少我们需要使我们的应用程序符合第 508 条规定 我尝试实现一些标准的辅助功能 文本框标签 向 div 添加标题属性等 但是 当在 Android 中使用 Tal
  • PhoneStateListener 不调用

    这是我的完整代码 广播示例 java package com example broadcast gt import android app Activity import gt android content Context import
  • 在 Android 中调整可绘制对象的大小

    我正在为进度对话框设置一个可绘制对象 pbarDialog 但我的问题是我想每次调整可绘制的大小 但不知道如何调整 这是一些代码 Handler progressHandler new Handler public void handleM
  • 错误:无法创建新会话,因为找不到需要 HttpClient、InputStream 和 long 的“createSession”

    我正在尝试自动化 Android 混合应用程序 但出现以下错误 1 线程 main org openqa selenium WebDriverException中出现异常 无法创建新会话 因为未找到需要 HttpClient InputSt
  • Android studio - 如何查找哪个库正在使用危险权限?

    我正在尝试将 apk 上传到 google play 商店 但令我惊讶的是 我正在使用以下权限 Your APK is using permissions that require a privacy policy android perm
  • Android:AsyncTask ProgressDialog 将不会在 ActivityGroup 中打开

    我试图在轮询我的服务器时打开一个进度对话框 该类是一个 ActivityGroup 因为它嵌套在选项卡栏中 为了将视图保持在框架内 需要 ActivityGroup 这是我的 ActivityGroup 类的声明 public class
  • Android 中循环事件的星期几和时间选择器

    我想创建一个控件 允许用户在我的 Android 活动中选择一周中的某一天 星期一 和一天中的某个时间 下午 1 00 找不到任何关于此的好帖子 好吧 我想我已经明白了 我只是不喜欢这个解决方案 因为我在一周中的某一天使用的微调器与时间选择
  • 尝试将 SQLite DB 从数据复制到 SD 卡

    我正在使用以下代码 该代码发布在 Stack Overflow 上的某个位置 并根据我的目的进行了修改 try File sd Environment getExternalStorageDirectory File data Enviro
  • 使用单选按钮更改背景颜色 Android

    我试图通过从单选组中选择单选按钮来更改应用程序选项卡的背景 但是我不确定如何执行此操作 到目前为止我已经 收藏夹 java import android app Activity import android os Bundle publi
  • Android 中带有组的列表视图

    我有一个列表视图 每行都有一些日期和文本 我可以像 iPhone 中那样将这个 listView 分组 组之间有标题吗 在 android 中是否可能 请帮忙 即 我需要在 Listview 行之间有标题栏 以便如果我使用日期对其进行分组

随机推荐

  • 从变量更新 pom 文件的 Shell 命令

    以前 我使用以下命令获取 pom xml 中的版本并将其从 1 递增 在增量快照版本之前 0 0 1 bin bash version grep ri
  • 如何读取 build.gradle 中 local.properties 中定义的属性

    我已经设定sdk dir and ndk dir in 本地属性 我如何读取值sdk dir and ndk dir in the 构建 gradle file 您可以通过以下方式做到这一点 Properties properties ne
  • Delphi、OLE 和 Microsoft Scheduler 的问题

    我有控制台 Delphi 应用程序 它可以通过 OLE 与 Excell 一起使用 代码示例非常简单 procedure SaveBaseRecordsToFile BaseName string PaymentRecords TPayme
  • 在 RCP 应用程序中添加包资源管理器会导致丢失一些图标

    在我的 eclispe rcp 应用程序中 我添加了一个包资源管理器 添加了 org eclipse jdt ui 当我使用 rcp app 时 当我通过 新建项目向导 创建一个新项目以添加 常规项目 时 该项目已正确创建 但包资源管理器视
  • 文档写入给出了奇怪的输出

    我正在编写一个脚本来检测 jQuery 如果它不存在 则插入 Google CDN 版本和本地后备 不要问为什么 这不是我的想法 问题是当我尝试执行文档时 write 我得到一个奇怪的输出 document write
  • git word-diff-regex 可以处理多行更改吗?

    考虑一个像这样的简单文件 承诺 foo Edited bar 现在 当我跑步时git diff word diff regex a z 它给了我 foo bar 所以 这表明这个词foo已被替换为bar 请注意 分号未标记为已更改 没关系
  • 无法解析:com.android.support:design-v7:27.1.1

    我使用的是Android Studio 3 1 3 Gradle 构建同步失败 我使用了以下方法 但没有用 如果有任何解决方案请告诉我 maven url https maven google com 使重启和缓存无效 multiDexEn
  • 如何在 Android 中的 Facebook 页面墙上发帖?

    您好 我想发布任何消息或链接到 Facebook 页面 我尝试过的是这个 void postInfo try Bundle parameters new Bundle parameters putString message message
  • 使用 angularjs 从服务器下载文件

    我正在寻找一种方法来让用户选择服务器上存在的文件并使用 angularjs 下载它 我发现这段代码不起作用 所以有人有一种正确的方法吗 var content file content var blob new Blob content t
  • PowerShell - 如何使用 $_.Key 作为 $object 属性?

    我有一个像这样的哈希表 hash One One Two Two Three Three 这样做是行不通的 hash getEnumerator foreach object Key Value 然而这句话 hash getEnumerat
  • 使用 gnu clisp 运行 shell 命令

    我正在尝试为 clisp 创建一个 系统 命令 其工作原理如下 setq result system pwd now result is equal to my path here 我有这样的事情 defun system cmd ext
  • json 序列化器 NullValueHandling 不使用 datamember 属性

    在我的 Web api 项目中 现在我正在跳过 null 值 因此 返回 json 会忽略 null 值并打印属性 在 Global asax 文件中 manage the null in the response var json Glo
  • 为Windows窗体中禁用的控件注册MouseEnter/MouseLeave事件?

    我想为禁用的按钮注册 MouseEnter MouseLeave 事件 尽管它确实适用于已启用的按钮 但它不起作用 Enable Disable controls on form load EnableDisableControls Loa
  • C# 中长时间运行任务的进度条

    我的应用程序运行一些可能需要很长时间的数据库查询 在执行这些查询时 我的应用程序似乎冻结了 并且看起来应用程序已停止工作 我需要使用进度条来避免这个问题 但我不确定如何预测查询执行所需的时间 运行查询的代码如下 private void C
  • MySQL加载数据文件-加速?

    有时 我必须为一个项目重新导入数据 从而将大约360万行读取到MySQL表中 目前是InnoDB 但我实际上并不局限于这个引擎 加载数据到文件 已被证明是最快的解决方案 但它有一个权衡 在没有密钥的情况下导入时 导入本身大约需要 45 秒
  • Laravel Controller 不存在,尽管它明显存在

    我收到的错误是控制器不存在 即使我知道它存在 这是代码 路线 php Route get mdpay template array uses gt templateController index templateController bl
  • Angular http get,从 spring mvc 服务器下载文件

    我正在使用 apache commons IOUtils 复制方法将文件从服务器发送到 angularjs 这是我的控制器 RequestMapping value download method RequestMethod GET pub
  • 如何将兰伯特圆锥等角光栅投影更改为纬度 R

    我有一个从 netcdf 获得的栅格 该栅格位于 兰伯特圆锥等角投影 library meteoForecast wrf temporary lt getRaster temp day Sys Date frames complete re
  • 静态下拉到动态下拉 Coldfusion

    我是堆栈溢出的新手 我需要创建一个静态下拉列表 然后根据在静态下拉列表中选择的值创建动态下拉列表 只是 Coldfusion 和 html 没有其他花哨的东西 因此 从第一个下拉菜单中 用户可以选择 颜色 身份证 官员 学校 然后点击 继续
  • 在android中的gridview中拖放图像

    我正在开发一个示例 Android 应用程序来了解 Android 中的拖放操作 在应用程序启动时 我在网格视图上显示一些图像 现在我需要一次将一张图像拖到另一张图像的位置 将一个图像放到另一个图像上后 图像应该交换位置 我怎样才能实现它