Android控制简单的拖放框。

4

我希望应用程序顶部有一行项目,每个框内都有像图标或字母之类的东西。您可以按任意顺序水平排列它们。非常容易交换,不像需要长按才能移动。为此我该使用哪种控件?


1
这个视频和这篇文章的结合应该可以让你入门。我从未做过类似的事情,所以我甚至不会尝试发布答案。 - rarp
@y2k 不够清晰。您能详细解释一下吗?(如果可能的话,请发布一些屏幕截图。) - TheFlash
这些项目被点击后会有动作吗?它们可以被点击吗? - Sherif elKhatib
你能分享一下你的组合示例代码吗?或者提供完整解决方案的演示链接吗? - Mayur R. Amipara
1个回答

7
首先让我稍微澄清一下任务。您只需要一个预定义的水平列表中的项目吗(选项A)?

enter image description here

或者是一个带有滚动条的水平列表(选项B):

enter image description here

假设此时选项A是相关的,因此您需要:
  1. 水平列表实现
  2. 适当的拖放处理
步骤1 有几种可用的水平列表实现,但其中一些已经过时且不受支持,因此建议检查Horizontal Variable ListView

Android的水平ListView。 基于官方ListView google代码。 它支持ListView小部件的几乎所有功能。 支持的属性之间存在轻微差异,例如“dividerWidth”而不是默认值“dividerHeight”。

顺便提一下,它支持Android 4.2.2,请参见演示示例步骤2 实际上,您此时需要做的就是适当地处理拖放操作。
最简单的解决方案是遵循标准和众所周知的示例:TouchInterceptor类用于音乐应用程序中。它扩展了ListView,因此使用水平可变ListView采用相同的方法不应该成为问题。
特别注意:
public boolean onInterceptTouchEvent(MotionEvent ev) {

public boolean onTouchEvent(MotionEvent ev) {

步骤三:高级实现

个人认为选项A只能用于演示,因此您还需要解决滚动问题。上面的示例展示了如何处理滚动,但有时您可能需要更多的解决方案。

enter image description here

这里有另一个项目(已经停止更新),可以作为高级示例,因为它解决了几个问题,支持动画等:

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;

对于DragSortListView (DSLV),如何实现垂直到水平模式? - Mayur R. Amipara
由于此链接指出不可能实现:http://stackoverflow.com/questions/17047336/drag-sort-listview-horizontal-items - Mayur R. Amipara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接