Android 控件用于简单的拖放框

2024-02-15

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


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

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

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

  1. 水平列表实现
  2. 正确的拖放处理

Step 1

有几个可用的水平列表实现,但其中一些是旧的并且不受支持,所以我建议检查水平变量列表视图 https://github.com/sephiroth74/HorizontalVariableListView:

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

顺便说一句它支持Android 4.2.2,另请参阅演示示例 https://github.com/sephiroth74/HorizontalVariableListView/blob/master/Demo/src/it/sephiroth/android/sample/horizontalvariablelistviewdemo/MainActivity.java.

Step 2

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

最简单的解决方案是遵循标准且众所周知的示例:触摸拦截器类 https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/TouchInterceptor.java用于音乐应用程序。它延伸ListView所以使用相同的方法应该不是问题水平变量列表视图 https://github.com/sephiroth74/HorizontalVariableListView.

特别注意:

public boolean onInterceptTouchEvent(MotionEvent ev) {

and

public boolean onTouchEvent(MotionEvent ev) {

第 3 步:高级实施

我个人认为option A只能用作演示,因此您还必须解决滚动问题。上面的示例展示了如何处理滚动,但在您可能需要更多的情况下。

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

拖动排序列表视图 (DSLV) https://github.com/bauerca/drag-sort-listview是 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 控件用于简单的拖放框 的相关文章

随机推荐