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)?
Or a horizontal scroll list (option B):
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.
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 { private View mFloatView; private Point mFloatLoc = new Point(); private Point mTouchLoc = new Point(); private int mFloatViewMid; private boolean mFloatViewOnMeasured = false; private DataSetObserver mObserver; private float mFloatAlpha = 1.0f; private float mCurrFloatAlpha = 1.0f; private int mFloatPos;