Code Monkey home page Code Monkey logo

draglinearlayout's Introduction

DragLinearLayout

Dragging and swapping children views.

An Android LinearLayout that supports draggable and swappable child Views.

Why?

Why bother doing drag & swap in a LinearLayout when there are so many solutions for ListView?

  1. Simplicity - no need for ListAdapters. By default, works like a LinearLayout.
  2. Flexibility - supports heterogeneous, selectively draggable (or not draggable), children.
  3. Portability - can be used in any layout, e.g. as a child in a ScrollView container.

Usage

Add it to your project using Gradle:

compile 'com.jmedeisis:draglinearlayout:1.1.0'

The DragLinearLayout can be used in place of any LinearLayout. However, by default, children will not be draggable. To set an existing View as draggable, use DragLinearLayout#setViewDraggable(View, View), passing in the child View and a (non-null!) View that will act as the handle for dragging it (this can be the View itself).

XML layout file:

<com.jmedeisis.draglinearlayout.DragLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        android:src="@drawable/image"/>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_text"/>
        
</com.jmedeisis.draglinearlayout.DragLinearLayout>

Enabling drag & swap for all child views:

DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container);
for(int i = 0; i < dragLinearLayout.getChildCount(); i++){
    View child = dragLinearLayout.getChildAt(i);
    // the child will act as its own drag handle
    dragLinearLayout.setViewDraggable(child, child);
}

Use #addDragView(View, View),#addDragView(View, View, int) and #removeDragView(View) to manage draggable children dynamically:

final View view = View.inflate(context, R.layout.view_layout, null);
dragLinearLayout.addDragView(view, view.findViewById(R.id.view_drag_handle));

// ..

dragLinearLayout.removeDragView(view);

Attach an OnViewSwapListener with #setOnViewSwapListener(OnViewSwapListener) to detect changes to the ordering of child Views:

dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {
    @Override
    public void onSwap(View firstView, int firstPosition,
            View secondView, int secondPosition) {
        // update data, etc..
    }
});

When placing the DragLinearLayout inside a ScrollView, call #setContainerScrollView(ScrollView) to enable the user to scroll while dragging a child view.

For best visual results, use children that have opaque backgrounds. Furthermore, do not use horizontal padding for the DragLinearLayout; instead, let children apply their own horizontal padding.

Refer to the included sample activity project for a demonstration of the above usage techniques and more.

Limitations

  • Supports only the LinearLayout#VERTICAL orientation.

License

This project is licensed under the terms of the MIT license. You may find a copy of the license in the included LICENSE file.

draglinearlayout's People

Contributors

justasm avatar xuxucode avatar

Watchers

soorianarayanan avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.