Android控件用于简单的拖放框

y2k*_*y2k 4 controls android drag-and-drop

我想在应用程序的顶部有一行项目,每个框内都有一个图标或一个字母.您可以按任何顺序水平排列它们.真的很容易交换,而不是像握住然后移动.我会用什么样的控制?

Ren*_*nov 7

首先让我澄清一下这个任务.您是否只需要水平列表中的一组预定义项目(选项A)

在此输入图像描述

或者滚动的水平列表(选项B):

在此输入图像描述

让我们暂时假设option A是相关的,所以你需要:

  1. 水平清单实施
  2. 适当的拖放处理

步骤1

有几个水平列表实现可用,但其中一些是旧的,不受支持,所以我建议检查水平变量ListView:

Android的水平ListView.基于官方ListView谷歌代码.它支持ListView小部件的几乎所有功能.支持的属性存在细微差别,例如"dividerWidth"而不是默认的"dividerHeight".

BTW它支持Android 4.2.2,请参阅演示示例.

第2步

此时您实际需要的只是正确处理拖放操作.

最简单的解决方案是遵循标准和众所周知的示例:Music应用程序中使用的TouchInterceptor类.它扩展,ListView因此使用与水平变量ListView相同的方法应该不是问题.

特别注意:

public boolean onInterceptTouchEvent(MotionEvent ev) {
Run Code Online (Sandbox Code Playgroud)

public boolean onTouchEvent(MotionEvent ev) {
Run Code Online (Sandbox Code Playgroud)

第3步:高级实施

我个人认为option A只能用作演示,所以你必须解决滚动问题.上面的示例显示了如何处理滚动,但可能需要更多一些.

在此输入图像描述

还有另一个项目(再次停产)可用作高级示例,因为它解决了几个问题,支持动画等:

DragSortListView(DSLV)是Android ListView的扩展,可以对列表项进行拖放重新排序.这是TouchInterceptor(TI)的重大改造,旨在为拖动排序提供抛光感.一些关键功能是:

  • 清洁拖放
  • 拖动时直观且平滑的滚动.
  • 支持异构项目高度.
  • 公共startDrag()和stopDrag()方法.
  • 用于自定义浮动视图的公共接口.

DragSortListView适用于所有类型的优先级列表:收藏夹,播放列表,清单等.

它似乎记录良好且易于理解.从垂直模式切换到水平模式不应该那么难.

public class DragSortListView extends ListView {


    /**
     * The View that floats above the ListView and represents
     * the dragged item.
     */
    private View mFloatView;

    /**
     * The float View location. First based on touch location
     * and given deltaX and deltaY. Then restricted by callback
     * to FloatViewManager.onDragFloatView(). Finally restricted
     * by bounds of DSLV.
     */
    private Point mFloatLoc = new Point();

    private Point mTouchLoc = new Point();

    /**
     * The middle (in the y-direction) of the floating View.
     */
    private int mFloatViewMid;

    /**
     * Flag to make sure float View isn't measured twice
     */
    private boolean mFloatViewOnMeasured = false;

    /**
     * Watch the Adapter for data changes. Cancel a drag if
     * coincident with a change.
     */ 
    private DataSetObserver mObserver;

    /**
     * Transparency for the floating View (XML attribute).
     */
    private float mFloatAlpha = 1.0f;
    private float mCurrFloatAlpha = 1.0f;

    /**
     * While drag-sorting, the current position of the floating
     * View. If dropped, the dragged item will land in this position.
     */
    private int mFloatPos;
Run Code Online (Sandbox Code Playgroud)