Android control for simple drag boxes

I want to have 1 line of elements at the top of the application, inside each window there will be a symbol or letter. You can arrange them in any order, horizontally. It is actually easy to exchange, not hold, and then move. What control would I use for this?

+4
source share
1 answer

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

enter image description here

Or a horizontal scroll list (option B):

enter image description here

Assume option A relevant for a moment, so you need:

  • horizontal list implementation
  • proper drag and drop management.

Step 1

There are several options for implementing horizontal lists, but some of them are old and unsupported, so I suggest checking the "Horizontal ListView variable"

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

BTW supports Android 4.2.2 , also see a demo .

Step 2

What you really need at this point is just to handle the drag and drop operations.

The easiest solution is to follow standard and well-known examples: the TouchInterceptor class used in the Music application. It extends the ListView , so you should not use the same approach with the Horizontal Variable ListView .

Pay particular attention to:

 public boolean onInterceptTouchEvent(MotionEvent ev) { 

and

 public boolean onTouchEvent(MotionEvent ev) { 

Step 3: Advanced Implementation

Personally, I think that option A can only be used as a demo version, so you also need to solve the scroll problem. The examples above show how to handle scrolling, but this may be the case when you need a little more.

enter image description here

There is another project (again discontinued) that can be used as an extended example, since it solves several problems, supports animation, etc.:

DragSortListView (DSLV) is an Android ListView extension that allows you to reorder and drag list items. This is a major revision of the complete rewriting of TouchInterceptor (TI) designed for drag and drop - a polished feel. Some key features:

  • Clear drag
  • Intuitive and smooth scrolling when dragging.
  • Support for heterogeneous element heights.
  • Public methods startDrag () and stopDrag ().
  • An open interface for setting up a floating view.

DragSortListView is useful for all types of priority lists: favorites, playlists, checklists, etc.

It is well documented and easy to understand. Switching from vertical to horizontal should not be so 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; 
+7
source

Source: https://habr.com/ru/post/1502535/


All Articles