Implementing Multi-Type RecyclerView Adapter with Drag-and-Scale Functionality in Android
This article explains how to use the MultiItem library to create a multi‑type RecyclerView adapter that supports cross‑RecyclerView item dragging, scaling, and custom control interfaces, providing code examples and a step‑by‑step walkthrough for Android developers.
After reading this article, readers will understand the basic usage of the MultiItem library for implementing a multi‑type RecyclerView adapter with cross‑RecyclerView item dragging and scaling, primarily used for office‑software style panels.
Effect screenshots (images omitted).
Usage – Drag functionality The drag process is handled via an ItemDragHelper and callbacks to ItemDragListener. Example initialization:
@Override
protected void onCreate(Bundle savedInstanceState) {
// …
dragHelper = new ItemDragHelper(horizontalRecycler);
dragHelper.setOnItemDragListener(new OnBaseDragListener());
}
...
public boolean dispatchTouchEvent(MotionEvent ev) {
return dragHelper.onTouch(ev) || super.dispatchTouchEvent(ev);
}Register a data source that implements ItemData:
baseItemAdapter.register(TextDragBean.class, new TextViewDragManager());Start dragging an item: dragHelper.startDrag(viewHolder); Implement a drag listener to receive callbacks, e.g., when dragging finishes:
class OnBaseDragListener extends OnItemDragListener {
@Override
public void onDragFinish(RecyclerView recyclerView, int itemRecyclerPos, int itemPos) {
super.onDragFinish(recyclerView, itemRecyclerPos, itemPos);
String text = String.format("Drag start list %s item %s end list %s item %s
Data:%s",
originalRecyclerPosition, originalItemPosition, itemRecyclerPos, itemPos, dragItemData);
Toast.makeText(PanelActivity.this, text, Toast.LENGTH_SHORT).show();
}
}General item drag control The data source must implement the ItemDrag interface, which defines methods isCanMove(), isCanChangeRecycler(), and isCanDrag() to control moving, switching between RecyclerViews, and enabling drag.
Double‑tap zoom Instantiate ViewScaleHelper and set the content, horizontal and vertical views, then toggle the scale mode:
// Instantiate scaling helper
ViewScaleHelper scaleHelper = new ViewScaleHelper();
scaleHelper.setContentView(contentView);
scaleHelper.setHorizontalView(horizontalRecycler);
scaleHelper.addVerticalView(verticalView);
// Toggle scaling
scaleHelper.toggleScaleModel(); // or startScaleModel()/stopScaleModel()Other customizations can be achieved by overriding methods in ItemDragListener, such as scale retrieval, move limits, scroll speeds, and floating view drawing.
Main process analysis The implementation follows the logic of Android’s ItemTouchHelper: generate a floating view using WindowManager and ImageView, calculate scrolling for both horizontal and vertical RecyclerViews, compute item position swaps based on touch coordinates, and apply view scaling.
In summary, this is a beta‑level implementation that provides a functional drag‑and‑scale feature for multi‑type RecyclerViews; readers are encouraged to explore the source code for deeper understanding and further refinement.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
