Android控件简单的拖放框 [英] Android control for simple drag and drop boxes

查看:297
本文介绍了Android控件简单的拖放框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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





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





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


  1. 水平列表实现

  2. 适当的拖放处理

步骤1



有几个水平列表实现可用,但其中一些是旧的和不受支持的, d建议检查是Android ListView的扩展,
启用列表项的拖放重新排序。这是一个重大的大修
完全重写TouchInterceptor(TI)意图给
拖拽一个抛光的感觉。一些主要功能是:




  • 清除拖放

  • 拖动时直观平滑滚动。 / li>
  • 支持异构项目高度。

  • public startDrag()和stopDrag()方法。

  • 自定义浮动视图的公共界面。



DragSortListView对于各种优先级列表:
收藏夹,播放列表,清单等。


似乎有很好的记录和易于理解。从垂直模式切换到水平模式不应该很难。

  public class DragSortListView extends ListView {


/ **
*在ListView上方浮动的视图,代表
*拖动的项目。
* /
private查看mFloatView;

/ **
*浮动视图位置。首先基于触摸位置
*和给定的deltaX和deltaY。然后通过回调
*限制到FloatViewManager.onDragFloatView()。最后通过DSLV的限制限制
*。
* /
private Point mFloatLoc = new Point();

private Point mTouchLoc = new Point();

/ **
*浮动视图的中间(在y方向)。
* /
private int mFloatViewMid;

/ **
*标记以确保浮动视图不被测量两次
* /
private boolean mFloatViewOnMeasured = false;

/ **
*观看适配器进行数据更改。如果
*与更改一致,请取消拖动。
* /
private DataSetObserver mObserver;

/ **
*浮动视图(XML属性)的透明度。
* /
private float mFloatAlpha = 1.0f;
private float mCurrFloatAlpha = 1.0f;

/ **
*拖动排序时,浮动
* View的当前位置。如果掉落,拖动的物品将落在此位置。
* /
private int mFloatPos;


I want to have 1 line of items at the top of the app, inside each box will have like an icon or a letter. You can arrange them in any order, horizontally. Really easy to swap, not like hold and then move. What kind of control would I use for this?

解决方案

First of all let me clarify the task a bit. Do you need just a predefined set of items in the horizontal list (option A)?

Or a horizontal list with scrolling (option B):

Let's assume for a moment option A is relevant, so you'll need:

  1. horizontal list implementation
  2. proper drag and drop handling

Step 1

There are several horizontal list implementations available, but some of them are old and unsupported, so I'd suggest to check Horizontal Variable ListView:

Horizontal ListView for Android. Based on the official ListView google code. It supports almost all the features of the ListView widget. There are minor differences in the attributes supported like "dividerWidth" instead of the default "dividerHeight".

BTW it supports Android 4.2.2, please also see demo example.

Step 2

What you actually need at this point is just to handle drag and drop actions properly.

The simplest solution is to follow standard and well-known example: TouchInterceptor class used in the Music application. It extends ListView so it should not be a problem to use the same approach with Horizontal Variable ListView.

Pay special attention to:

public boolean onInterceptTouchEvent(MotionEvent ev) {

and

public boolean onTouchEvent(MotionEvent ev) {

Step 3: Advanced implementation

Personally I think option A can only be used as a demo, so you have to address scrolling problem as well. Examples above show how to handle scrolling, but it might be a case you need a bit more.

There is another project (discontinued again) which can be used as an advanced example, because it solves several issues, supports animation, etc:

DragSortListView (DSLV) is an extension of the Android ListView that enables drag-and-drop reordering of list items. It is a major overhaul complete rewrite of the TouchInterceptor (TI) meant to give drag-sorting a polished feel. Some key features are:

  • Clean drag and drop
  • Intuitive and smooth scrolling while dragging.
  • Support for heterogeneous item heights.
  • Public startDrag() and stopDrag() methods.
  • Public interface for customizing the floating View.

DragSortListView is useful for all kinds of prioritized lists: favorites, playlists, checklists, etc.

It seems to be well-documented and easy to understand. Switch from vertical to horizontal mode should not be that hard.

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;

这篇关于Android控件简单的拖放框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆