我希望应用程序顶部有一行项目,每个框内都有像图标或字母之类的东西。您可以按任意顺序水平排列它们。非常容易交换,不像需要长按才能移动。为此我该使用哪种控件?
我希望应用程序顶部有一行项目,每个框内都有像图标或字母之类的东西。您可以按任意顺序水平排列它们。非常容易交换,不像需要长按才能移动。为此我该使用哪种控件?
选项A
是相关的,因此您需要:
顺便提一下,它支持Android的水平ListView。 基于官方ListView google代码。 它支持ListView小部件的几乎所有功能。 支持的属性之间存在轻微差异,例如“dividerWidth”而不是默认值“dividerHeight”。
Android 4.2.2
,请参见演示示例。
步骤2
实际上,您此时需要做的就是适当地处理拖放操作。ListView
,因此使用水平可变ListView采用相同的方法不应该成为问题。public boolean onInterceptTouchEvent(MotionEvent ev) {
和
public boolean onTouchEvent(MotionEvent ev) {
步骤三:高级实现
个人认为选项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;